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

Discussion of the simple1 program

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

What is a variable?

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

Difference between zero and '0' - Unicode characters

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.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask