Question: Pick a topic from the ElementsOfJava section that you feel is good review for you. Take the code segment from the reading or write your
Pick a topic from the ElementsOfJava section that you feel is good review for you. Take the code segment from the reading or write your own segment that demonstrates the lesson and embed it into a main() Java program so it works. Please comment the page and segment you are reviewing. Use JavaDoc comments.
- Attach the .java source file and the
- copyAndPaste the output into WriteSubmissionBox
Primitive Types
S1.16 A whole number without a decimal point, such as 0, 1, or -2, is called an integer. A number with a decimal point, such as 3.14159, -8.63, or 5.0, is called a floating-point number. Notice that 5.0 is a floating-point number, not an integer. If a number has a fractional part, even if the fractional part is zero, it is a floating-point number.
All the Java primitive types appear inside the cover of this book. Notice that there are four types for integersnamely byte, short, int, and long. The only difference among the various integer types is the range of integers they can store and the amount of computer memory they use. If you cannot decide which integer type to use, use the type int. It has a large enough range for most purposes and does not use as much memory as the type long.
Java has two types for floating-point numbers, float and double. If you cannot decide between the types float and double, use double. It allows a wider range of values and is used as a default type for floating-point numbers.
You use the primitive type char for single characters, such as letters, digits, or punctuation. For example, the following declares the variable symbol to be of type char, stores the character for uppercase A in symbol, and then displays that valuethe Aon the screen:
char symbol; symbol = 'A'; System.out.println(symbol);
Notice that we enclose the character A in single quotes. Again note that uppercase letters and lower- case letters are different characters. For example, 'a' and 'A' represent two different characters.
Finally, the primitive type boolean has two values, true and false. You can use a variable of type boolean to store the answer to a true/false question such as Is myTime less than yourTime?
Constant
S1.17 A variable can have its value changed; its value varies. A literal number like 2 cannot change. It is always 2. It is never 3. Values like 2 or 4.8 are called constants, or literals, because their values do not change.
You write constants of integer types with an optional plus sign or minus sign, but without commas or decimal points. Floating-point constants have an optional plus sign or a minus sign and no commas. You can write a floating-point constant in one of two ways. One way looks like the everyday way of writing numbers. For example, 9.8, 3.14, and 5.0 are floating-point constants, because they contain a decimal point. The second way is to include a multiplier that is a power of 10. You use the letter e to represent both the multiplication sign and the 10. For example, the number 8.65 * 108 appears in Java as 8.65e8 (or in the less convenient form 865000000.0). The two forms, 8.65e8 and 865000000.0, are equivalent in a Java program. Similarly, the number 4.83 * 10-4, which is equal to 0.000483, can be written as 4.83e4 in Java.
The e stands for exponent, since it is followed by a number that is thought of as an expo- nent of 10. The number before the e can be a number with or without a decimal point. The number after the e cannot contain a decimal point.
Other types of literal expressions are also called constants. You write constants of type char by placing the character within single quotes. For example, 'Y' is a constant of type char. A string constant is a sequence of characters enclosed in double quotes, as in "Java".
Assignment Statements
S1.18 You can use an assignment statement to give a value to a variable. For example, if answer is a variable of type int and we want to give it the value 42, we could use the following assignment statement:
answer = 42;
An assignment statement always consists of a single variable on the left-hand side of an equal sign and an expression on the right-hand side followed by a semicolon. The expression can be another variable, a constant, or a more complicated expression made up by combining operators, such as + and *, with variables and constants. The value of the expression is assigned to the variable on the left of the equal sign.
For example, the following are all examples of assignment statements:
amount = 3.99; firstInitial = 'B'; score = numberOfCards + handicap;
Here we assume that amount is a variable of type double, firstInitial is of type char, and the rest of the variables are of type int. If the variable numberOfCards has the value 7 and handicap has the value 2, the value of the variable score is 9.
The equal sign, =, which is called the assignment operator, does not mean equality. You can think of the assignment operator as saying, Make the value of the variable equal to what fol- lows. For example, in the statement
eggsPerBasket = eggsPerBasket 2;
the variable eggsPerBasket occurs on both sides of the assignment operator. This statement subtracts 2 from the present value of eggsPerBasket and assigns the new value to eggsPerBasket. In effect, the statement decreases the value of eggsPerBasket by 2.
S1.19 A variable that has been declared but that has not yet been given a value by the program is uninitialized. Such a variable might literally have no value, or it might have some default value. For example, an integer variable might have a default value of zero, and a reference variable might have a default value of null, which is a predefined constant in Java. However, your program will be clearer if you explicitly give the variable a value, even if you are simply reassigning it the default value. (The exact details on default values have been known to change and should not be counted on.)
One easy way to ensure that you do not have an uninitialized variable is to initialize it within the declaration. Simply combine the declaration and an assignment statement, as in the follow- ing examples:
int count = 0; double taxRate = 0.075; char grade = 'A'; int balance = 1000, newBalance;
Note that a single declaration, such as the last statement, can initialize some variables and not others.
Sometimes the compiler may complain that you have failed to initialize a variable. In most cases, this is indeed true. Occasionally, the compiler is mistaken about this. However, the compiler will not compile your program until you convince it that the variable in question is initialized. To make the compiler happy, initialize the variable when it is declared, even if the variable will be given a different value before you use it for anything. In such cases, you cannot argue with the compiler.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
