Coding in Java

by Sue Smith

Coding in Java allows developers to create many different types of application. Java projects appear widely in both Web and desktop contexts. The Java language benefits from a range of libraries dedicated to carrying out common tasks, such as connecting to data sources. This means that programmers can access this functionality without having to implement it manually. Java coding follows some basic rules and is accessible even to beginners.

Basic Syntax

Basic Java syntax creates code that is typically readable. Because the language is high level, it involves a good degree of abstraction and is therefore accessible to people who do not know much about computer hardware. The following example code demonstrates declaring and instantiating a variable in Java: String myText = "A few words"; To declare a variable, a Java program first indicates the type, in this case a text string, then the variable name. Java programmers are encouraged to use meaningful names to produce readable code. To instantiate the variable, giving it a value, the equals sign assignment operator is followed by the variable value.

Control

Java provides a variety of control structures, allowing programmers to tailor the flow of program execution. The following sample code demonstrates an "if" statement followed by an "else" statement: if(myText.length()>0) System.out.println("The string has text in it"); else System.out.println("The string is empty"); This code carries out a conditional test on the string variable, writing a different message to the output console depending on whether the test returns a true or false value. The following sample code demonstrates a loop: for(int i=0; i

Methods

Java methods allow programmers to define sections of processing that can be used from other points within a program. For example, if a program carries out a series of steps more than once, these steps can be contained within a method and called from there, reducing the amount of code repetition. The following sample code declares a method: public void printCharacters(String theText) { for(int i=0; i

Classes

Java projects use object-oriented development. This is a practice in which programmers divide the overall tasks of an application between objects. The technique is similar to creating methods to carry out well-defined processing steps, but it involves creating class declarations to define behavior of application objects. The Java language provides some classes that programmers can use by default, but they can also create their own. The following code demonstrates the outline of a class declaration: public class Assistant To create an object of this class, programs could use the following code: Assistant myHelper = new Assistant();

About the Author

Sue Smith started writing in 2000. She has produced tutorials for companies including Apex Computer Training Software and articles on computing topics for various websites. Smith has a Master of Arts in English language and literature, as well as a Master of Science in information technology, both from the University of Glasgow.

Photo Credits

  • photo_camera Photos.com/PhotoObjects.net/Getty Images