IB/Group 4/Computer Science/Object-Oriented Programming/Java

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
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,  ,  , and   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
Let's examine the following Java program. As we go through this program, the syntax and semantics of the statements will be pointed out.

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. 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  followed by a data type identifier or a class. In this case  or. indicates that the value to be stored in the constant is one alphanumeric character. 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. In line 7, the string made up of characters ' ', ' ', ' ', ' ', ' ' (space), ' ', ' ', and ' ' (space) is stored in. Line 8 through 11 define for more String constants. By convention, most programmers use all uppercase for constant identifiers.

Output of the application Rhyme:

Other good resources

 * Essentials of the Java Programming Language (As provided by Oracle)