Java Reference
In-Depth Information
The hexadecimal number “2F” represents the binary number 0010 1111. If we convert
this to an integer, we will get the number 47. Therefore, the program will print 47.
The
float
and
double
types can be used to represent a real number. What Table 2.1
does not show is that precision can be lost as the number is saved in main memory. For
example, a
float
stores only the first few digits after the decimal dot. If we care about
precision, then we are better off using a
double
(called double because it uses twice the
size). Consider the following code snippet.
float
x = 1/3F;
System. out . println (x) ;
It prints 0.33333334. Note that dividing an integer by a float gives us a float. As you can
see, the result is not exactly 1/3. Alternatively, suppose we save the value of the division in
a
double
.
double
x = 1/3.0;
System. out . println (x) ;
We will now get 0.3333333333333333, which shows much better precision. Note that 1/3
is an irrational number and as such cannot be precisely stored using a
float
or a
double
.
Throughout the topic, we will not be concerned in saving main-memory space and therefore
we will always use a
double
instead of a
float
. Similarly, we will always use an
int
instead
of a
short
.
The second-to-last primitive type is
boolean
. It uses only one bit that can take the
values
true
or
false
. The last primitive type
char
represents a character. It is used to
store a character. About sixty-five thousand characters can be stored, which allows support
for different international alphabets. Let us go back to our four-step algorithm. The first
step involves printing a message to the screen. Here is one possible implementation.
System. out . print (
"Please enter temperature is Celsius: "
);
The instruction
System.out.print("
...
")
prints the string in the double quotes.
If we want to print a string and then go to a new line, then we should use the syntax:
System.out.println("
...
")
.
The second step of the algorithm requires several commands. First, we need to allocate
space to save the keyboard input. We can do that using the following syntax.
int
x;
The statement allocates four bytes in main memory (i.e., enough space to store an
integer) and makes
x
reference the location; see Figure 2.5. Next, we want to read an
integer from the keyboard and save it in the variable
x
. We need to perform the following
two operations in order to do that.
Scanner keyboard =
new
Scanner(System. in) ;
x = keyboard . nextInt () ;
The first line creates the
keyboard
object. The syntax for creating a new object is a
little involved and is discussed in detail in Chapter 6. Note that
keyboard
is the name of
a variable and it can be replaced with a variable name of our choice. The second line calls
the
nextInt
method on the
keyboard
object. Note that a method is a routine that one can
call. It is similar to calling a friend of yours and asking them to do you a favor. In this case,
the “favor” will be reading an integer from the keyboard and returning the result. There are
different methods that can be called on a
Scanner
object. For example,
nextDouble
reads