<< Chapter < Page | Chapter >> Page > |
Program output
The output produced by compiling and running this program is shown in Figure 1 . The second line of text in Figure 1 ending with the # characterwas typed by the user.
Figure 1 . Screen output from the program named simple1. |
---|
Enter some text, terminate with #
abcde#The char before the # was e |
Purpose
I will use the program shown in Listing 1 to discuss several important aspects of the structure of a Java program. I will also providetwo additional sample programs that illustrate specific points not illustrated in the above program later in this module.
Variables are used in a Java program to contain data that changes during the execution of the program.
Declaring a variable
To use a variable, you must first notify the compiler of the name and the type of the variable. This is known as declaring a variable .
The syntax for declaring a variable is to precede the name of the variable with the name of the type of the variable as shown in Listing 2 . It is also possible (but not always required) to initialize a variable in Java when it is declared as shown in Listing 2 .
Listing 2 . Declaring and initializing two variables named ch1 and ch2. |
---|
int ch1, ch2 = '0'; |
The statement in Listing 2 declares two variables of type int , initializing the second variable (ch2) to the value of the zero character (0). (Note that I didn't say initialized to the value zero.)
The value of the zero character is not the same as the numeric value of zero, but hopefully you already knew that.
As an aside, characters in Java are 16-bit entities called Unicode characters instead of 8-bit entities as is the case with many programming languages. The purpose is to provide many more possible characters including characters used in alphabets other than the one used in the United States.
Initialization of the variable
Initialization of the variable named ch2 in this case was necessary to prevent a compiler error. Without initialization of this variable,the compiler would recognize and balk at the possibility that an attempt might be made to execute the statement shown in Listing 3 with a variable named ch2 that had not been initialized
Listing 3 . Display the character. |
---|
System.out.println("The char before the # was "
+ (char)ch2); |
Error checking by the compiler
The strong error-checking capability of the Java compiler would refuse to compile this program until that possibility was eliminated by initializing thevariable.
Using the cast operator
You should also note that the contents of the variable ch2 is being cast as type char in Listing 3 .
(A cast is used to change the type of something to a different type.)
Recall that ch2 is a variable of type int , containing the numeric value that represents a character.
We want to display the character that the numeric value represents and not the numeric value itself. Therefore, we must cast it (purposely change its type for the evaluation of the expression) . Otherwise, we would not see the character on the screen. Rather, we would see the numeric value that representsthat character.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?