- Data Types and Other Tokens
- Working with Variables
- The boolean Primitive
- The Flavors of Integer
- Operators
- Character Variables
- Floating-Point Variables
- Literals: Assigning Values
- Integer Literals
- Character Literals
- Floating-Point Literals
- String Literals
- Arrays
- Non-Token Input Elements
- Troubleshooting
Non-Token Input Elements
Now that you've seen the various types of tokens in Java, let's complete the discussion with a look at the only input elements in your source code that aren't classified as tokens. As was stated when tokens were defined previously, whitespace and comments are the two non-token element types.
Whitespace
Of some importance to most languages is the use of whitespace. Whitespace is any character that is used just to separate texta space, tab, form feed, line feed, or carriage return.
In Java, whitespace can be inserted almost anywhere within an application's source code without affecting the meaning of the code to the compiler. The only place that whitespace cannot be used is within an identifier, such as a variable or class name. Put simply, the following two lines are not the same to a Java compiler:
int myInt; int my Int; // compiler error
Proper use of indentation, blank lines, and spaces in your source code makes your code easier to read and understand. Little or inconsistent use of whitespace makes your code harder to maintain than necessary. Take a look at the ever-popular HelloWorld application written with minimal use of whitespace:
public class HelloWorld{public static void main(String args [ ]){System.out.println("Hello World!!");}}
Clearly, it is a little harder to determine what this application does or even if the code is correct. You should choose a consistent pattern of whitespace use and stick with it.
Comments
Comments are an important part of any language. Comments enable you to describe the purpose of your code, any assumptions it relies on, and the rationale behind its implementation approach. You should always comment the aspects of your code that aren't obvious, even if it's only for your own use later. When you do add comments, however, be certain to keep them up-to-date with changes to the surrounding code. Perhaps the only situation worse than maintaining code without comments is maintaining code with comments that are obsolete or just plain wrong.
Java supports three styles of comments:
-
Traditional (the type found in C)
-
C++ style
-
javadoc (a unique approach for Java class documentation)
Traditional Comments
A traditional comment is a C-style comment that begins with a slash-star (/*) and ends with a star-slash (*/). Take a look at Listing 3.4, which shows four traditional comments.
Listing 3.4 An Example Containing Four Traditional Comments
/* Set up the desired loan parameters * and call the method that computes * the monthly payment amount */ int numPayments = 3 * 12; /* 3 years of monthly payments */ double interestRate = 0.08 / 12; /* 8% annual interest paid monthly */ double payment = computePayment( interestRate, numPayments, 5000.0 /* hard code the loan amount for now */ );
As you can see, comments of this sort can span many lines or can be contained within a single line (outside a token). Traditional comments cannot be nested. Thus, if you try to nest them, the opening /* of the inner one is not detected by the compiler and the closing */ of the inner one ends the comment. The subsequent text, including the */ of the outer comment, is then interpreted as part of your source code and causes a compiler error. Listing 3.5 shows how this can become very confusing.
Listing 3.5 An Example of a Single Comment That Looks Like Two
/* This opens the comment /* That looked like it opened another comment but it is the same one* This will close the entire comment */
Traditional comments are extremely useful when you are debugging your code. This style of comment makes it easy to quickly comment out blocks of code as you work to isolate the source of an error.
C++ Style Comments
The second style of comment begins with a slash-slash (//) and ends when the current source code line ends. These comments are especially useful for describing the intended meaning of a single line of code. Listing 3.6 demonstrates the use of this style of comment.
Listing 3.6 An Example Using C++ Style Comments
// entry point for the application public static void main(String[ ] args) { if (args.length < 1) { // application requires a command-line argument System.out.println("<usage> java ClassName InputString"); System.exit(-1); // exit the program } String inputString = args[0]; // get command-line argument System.out.println(inputString);}
javadoc Comments
The final style of comment in Java is a special type specifically intended for defining descriptions of your code that can be read by the javadoc tool (see Appendix B, "SDK Tools"). These comments look very much like traditional comments except that they begin with /** instead of /*. These comments have the properties of traditional comments, including the capability to span multiple lines. By using these comments in an appropriate manner, you can use javadoc to automatically create documentation pages similar to those of the Java API. Listing 3.7 shows a javadoc comment (see Appendix B for a description of the javadoc tags @param, @return, and @throws used in this example).
Listing 3.7 An Example of a javadoc Comment
/** * This method computes the payment for a loan based on * the interest rate, the number of payment periods, and * the loan amount. * * @param rate The interest rate per payment period * @param numPayments The total number of payment periods * @param loanAmount The present value of the loan * @return The computed payment amount * * @throws InvalidLoanParameterException If one of the * arguments is <= 0 */ public static double computePayment( double rate, int numPayments, double loanAmount ) throws InvalidLoanParameterException { // compute and return the payment amount ...}
Caution - This style of comment should only be used for information that should be extracted by javadoc. Other use will produce confusing documentation of your classes.