Skill-Building Exercises


Summary

A Java application is created by adding a main() method to an ordinary class definition. The keywords public and class are used in the class definition. The name of the class is formulated according to the Java identifier naming rules. The body of the class definition appears between the opening and closing braces. Everything in the class body belongs to the class.

The main() method appears in the class body. The name of the main() method is proceeded by the keywords public, static, and void. The main() method takes a String array as its only parameter. The body of the main() method is contained between opening and closing braces.

There are several rules you must follow when formulating identifier names: 1) begin with a valid Java identifier start letter, 2) followed by any number of valid Java identifier part letters or digits, 3) your identifier cannot spell a Java reserved keyword, and 4) your identifier cannot spell the words true, false, or null.

The most difficult part of identifier naming involves deciding on what names to use given the context of your program. The best help you can give yourself in your early programming career is to put some thought into your identifier names. Well-named identifiers will make your source code easy to read. Easy-to-read code is easier to understand. Code that’s easy to understand is easy to fix should it contain an error or two. Problems can be quickly spotted when your program reads like a story.

A variable’s value can be changed during program runtime. A constant’s value must be initialized at the point of declaration and cannot be changed during program runtime.

The Java language supports two distinct type categories: primitive and reference. Arrays are a special case of reference type and can be considered to belong to their own type category. Primitive type variables are assigned their values directly. Reference variables hold the address of an object that is dynamically created in the heap memory with the new operator.

The main() method can directly access class (static) variables and constants. However, a reference variable is required to access instance variables and constants.

The main() method’s String array can be used to pass command-line String arguments to the main() method when the program is executed.

The new operator is used to allocate system memory for an object and assign the location value to a reference variable. You can reuse a reference variable to hold the address of a new object by simply using the new operator to create another object. The Java garbage collector will manage the memory issues automatically.

Objects created with the new operator reside in unique memory locations. This memory location may be used by an implementation of the Java virtual machine to generate a unique object identifier value called a hashcode. An object’s hashcode value can be discovered by calling an object’s hashCode() method. (The Object.toString() method calls the Object.hashCode() method.)

Java programs are comprised of statements and expressions. Most statements and expressions contain operators. Operators have precedence, but it is a good idea to use parentheses in a complex expression to ensure it is evaluated as you expect.

All the Java primitive data types have corresponding wrapper classes which provide lots of handy functionality.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net