The Java Programming Language

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Object-Oriented Programming Languages

Development stage: 00% Java

Development stage: 00% PHP

Development stage: 00% Python

Development stage: 00% C++

Java is an object-oriented language with some imperative features. Java was originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.

Anatomy of a Java program[edit]

There are two basic parts to a Java program: (1) instructions to the Java compiler and (2) instructions that describe the processing to be done. However, before we can describe these instructions, we must describe how Java names things; that is, how Java defines identifiers. An identifier is made up of letters, numbers, underscores, and dollar signs, but must begin with a letter or an underscore. Beware: Java is case sensitive. This means that Value, VALUE, value, and vaLue are four separate identifiers. In fact, we can construct 32 distinct identifiers from these five letters by vary the capitalisation.

Java programmers use certain conventions to provide visual cues about what an identifier is naming. The language does not require that these conventions be followed, but good programming practice dictates that we should. It is far easier for programmers to understand each other's code when we all follow the same naming conventions. Class identifiers begin with an uppercase letter, object and action (method) identifiers begin with a lowercase letter, and constant identifiers are all uppercase.

Program structure[edit]

Let's examine the following Java program. As we go through this program, the syntax and semantics of the statements will be pointed out.

 1 // Program Rhyme prints out a nursery rhyme
 2 public class Rhyme
 3 {
 4 	public static void main(String[] args)
 5 	{
 6 		final char SEMI_COLON	= ';';
 7 		final String VERB1	= "went up ";
 8 		final String VERB2	= "down came ";
 9 		final String VERB3	= "washed ";
10 		final String VERB4	= "out came ";
11 		final String VERB5	= "dried up ";
12 
13 		String firstLine;
14 		String secondLine;
15 		String thirdLine;
16 		String fourthLine;
17 
18 		firstLine		= "The itsy bitsy spider " + VERB1 + "the water spout";
19 		secondLine		= VERB2 + "the rain and " + VERB3 + "the spider out";
20 		thirdLine		= VERB4 + "the sun and " + VERB5 + "all the rain";
21 		fourthLine		= "and the itsy bitsy spider " + VERB1 + "the spout again";
22 
23 		System.out.println(firstLine + SEMI_COLON);
24 		System.out.println(secondLine + SEMI_COLON);
25 		System.out.println(thirdLine + SEMI_COLON);
26 		System.out.println();
27 		System.out.println(fourthLine + ".");
28 	}
29 }

Line 1 begins with a double slash (//) and is ignored by the translation system because it is a comment. Comments begin with // and extend to the end of the line. Another way of entering comments into the program is to insert them between /* and */. Comments that begin with /* and */ can extend across any number of lines.

Line 2 names the program or application class. In Java programs are called applications or application classes. Many Java systems require that the file name in which the application is stored be the same as the class name with an extension attached. Line 3 is the beginning brace that encloses the class.

Java calls subprograms methods. Every Java application must have a method named main. Its heading must look exactly like line 4. For now, do not worry about the details of that line, just copy it exactly when you write programs. Line 5 contains only one character: the left brace ({). Like C++, Java uses this character to begin a block and the right brace (}) on line 28 to close the block. The body of the method contains the statements that are translated by the compiler and executed when the program is run.

Line 6 through 11 instruct the compiler to assign the named constant on the left of the equal sign a place in memory and to store the value on the right of the equal sign in that place. In Java, a named constant is made up of the reserved word final followed by a data type identifier or a class. In this case char or String. char indicates that the value to be stored in the constant is one alphanumeric character. String indicates that the value to be stored in the constant is a string of characters. The data type identifier or class name is followed by the name to be given to the constant. An equal sign and the value to be stored there follow the constant name. In line 6, a semicolon is stored in constant SEMI_COLON. In line 7, the string made up of characters 'w', 'e', 'n', 't', ' ' (space), 'u', 'p', and ' ' (space) is stored in VERB1. Line 8 through 11 define for more String constants. By convention, most programmers use all uppercase for constant identifiers.

Output of the application Rhyme:

The itsy bitsy spider went up the water spout;
down came the rain and washed the spider out;
out came the sun and dried up all the rain;

and the itsy bitsy spider went up the spout again.

Object-oriented features[edit]

Atomic data types[edit]

Input/Output structures[edit]

Asynchronous processing[edit]

Composite data types[edit]

Other good resources[edit]