Q&A From Java


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

A
No. Although the programs that 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 is not generally recommended.

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

A
For many of the simple programs that 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. Therefore, you would not need to set up an initial value when creating a
result variable.

Q What's the specific range for the long variable type?

A
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.

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

A
It makes the variable easier to spot among all of 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.

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

A
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 like that make it into the finished product and are called logic errors. They must be caught by an attentive programmer during testing.

Quiz

Test your knowledge of variables, expressions, and the rest of the information in this hour by answering the following questions.

Questions

1. What do you call a group of statements contained with an opening bracket and a closing bracket?

(a) A block statement
(b) Groupware
(c) Bracketed statements

2.
A
boolean variable is used to store true-or-false values.

(a) True
(b) False
(c) No, thanks. I already ate.

3.
What characters cannot be used to start a variable name?

(a) A dollar sign
(b) Two forward slash marks (//)
(c) A letter

Answers

1. a. The grouped statements are called a block statement or a block.

2. a. True or false are the only answers a
boolean variable can store.

3. b. Variables can start with a letter, dollar sign ($), or an underscore character (
_). If you started a variable name with two slash marks, the rest of the line would be ignored because the slash marks are used to start a comment line.

Activities

You can review the topics of this hour more fully with the following activities:

·         Expand the Elvis program to track the weight if it were incremented by one pound for three consecutive days.

·         Create a short Java program that uses an x integer and a y integer and displays the result of x squared plus y squared.

 

Post a Comment

Previous Post Next Post