Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

Q&A

  1. Is a line in a Java program the same thing as a statement?

    No. Although the programs you will create in this book put one statement on each line, this is done to make the programs easier to understand; it's not required. The Java compiler does not consider lines, spacing, or other formatting issues when compiling a program. The compiler just wants to see semicolons at the end of each statement. You can put more than one statement on a line, although this makes a program more difficult for humans to understand when they read its source code. For this reason, it is not generally recommended.

  2. Is there a reason to set up a variable without giving it a value right away?

    For many of the simple programs you will be creating in the first several hours, no. However, there are many circumstances where it makes more sense to give a variable a value at some point later in the program. One example would be a calculator program. The variable that stores the result of a calculation will not be needed until a user tries out the program's calculator buttons. You will not need to set up an initial value when a result variable is created.

  3. What's the specific range for the long variable type?

    In Java, a long integer variable can be anything from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This range ought to give your mathematical expressions plenty of breathing room when you can't use int, which has a range of –2,147,483,648 to 2,147,483,647.

  4. Why should the first letter of a variable name be lowercase, as in gameOver ?

    It makes the variable easier to spot among all the other elements of a Java program. Also, by following a consistent style in the naming of variables, you eliminate errors that can occur when you use a variable in several different places in a program. The style of naming used in this book has become popular since Java's release.

  5. Can two variables have the same letters but different capitalization, as in highScore and HighScore ?

    Each of the differently capitalized names would be treated as its own variable, so it's possible to use the same name twice in this way. However, it seems likely to cause a lot of confusion when you or someone else is attempting to figure out how the program works. It also increases the likelihood of using the wrong variable name somewhere in your program, which is an error that will not be caught during compilation. Errors such as that that make it into the finished product are called logic errors. They must be caught by an attentive programmer during testing.

Share ThisShare This

Informit Network