Oop 04
Oop 04
In Lesson 3, we learnt how to write a Java program in the Java code window and how to Run a Java
program. In this lesson, we shall learn how to work with variables in Java and how to manipulate
computer memory using variables.
Data is an important part of programming and programs work by operating on data. Data can be text,
numbers, pointers, objects or some specified memory location.
Data is accessed by giving it a name and then assigning a value to it. The name of the data and its
value is referred to as a variable. Now, let’s see how we can work with number variables in Java.
To use a number variable in Java, you must specify what type it is i.e. its data type. Whole numbers
e.g. 5, 13, 28 etc. have a data type of int (for integer) while floating point numbers e.g. 1.6, 7.34, 0.8
etc. use a double data type. To store a value into a variable we use the equal sign (=) operator. For
example, to store a value 20 to a variable called age we write the following:-
int age;
age = 20;
To workout with a practical example, let’s open the program we had in Lesson 3 and modify inside
the “main” part to look like shown below:
int age;
age = 20;
So, to store a whole number, you first type the word int, followed by a space and then the name of
your integer variable. To assign the value, you type the name of the variable followed by a space,
then the equal sign (=) and finally the value itself, don’t forget the semicolon.
What the above code mean is that we want to store a value of 20 into an integer variable called “age”.
Notice the above code can also be written using one line like shown below:
To see the output when you run your program, we have to include the Java function for displaying the
output on the output window. So, modify the code again to include the output function println(). Your
code now should look like shown below:
What we have between the round brackets of println is some direct text enclosed in double quotes:
+ age );
The plus sign tells Java to "join together" the output. So we're joining together the direct text, "My age
= ", and the name of our variable, age. The joining together is known as concatenation. Notice each
line of the code ends with a semicolon (;). Now, run the program and you should be able to see the
following output.
The value of age that we stored in the variable called “age” is output after the equals sign.
Variables in Java can be called any name but Java has some rules to be followed when naming your
variables. Here are some of the rules:-
A variable name cannot start with a number. So you cannot call a variable 1stsalary or 20years.
A variable name cannot be the same as a keyword used in Java. Keywords in Java NetBeans appear in blue in
the code window, like int, so you can’t miss them.
A variable name cannot have a space e.g. my Age. If you want to have two words for a variable name, just join
them e.g. myAge or use an underscore e.g my_age.
Variable names are case sensitive. So Age and age are different variable names.
Java uses the commonly known computer arithmetic operators for calculations:
+ (the plus sign) for addition
- (the minus sign) for subtraction
* (the asterisk sign) for multiplication and
/ (the forward slash) for division
Let's try some simple calculations. Modify your code and add three int variables. One to
store basicSalary, another to store allowances and another to store grossSalary. Go ahead and
assign 20000 to basicSalary and 12000 to allowances. What we want to do here is to
add basicSalary and allowances and store the sum value in the variable called grossSalary and
then output the grossSalary as the sum value. Your code now should look like shown below:
If you run the program at this stage, you won’t be able to see the expected output. This is because we
have not included the function to display the output. Now, include the following line of code in your
program to enable you to see the sum output. We have modified the output statement to be more
meaningful just like we did with age output.
Java can store quite large numbers in the int variable type. The maximum value is 2147483647. If
you want a minus number the lowest value you can have is -2147483648. If you want larger or
smaller numbers than that then you can use a number variable of type double.
The double variable type is also used to hold floating point numbers i.e. numbers with a decimal
point like 18.7, 10.3,154.108 etc. If you store a floating point value in an int variable, NetBeans will
underline the faulty code and if you run the program, the compiler will display an error message.
Up to this point you’ve been working with variables of int type. Let us now try to work with variables of
type double. Modify your code to make your variables to be of type double. Then assign double type
values to these variables like shown below:
Notice that because we expect a double variable as the output, we also need to change the type of
our output variable (grossSalary) from int to double type. Run the program again, this time you
should be able to get a double output.
Working with variable of float type
The double values can store really big numbers of the floating point type. So, Instead of
using double, float type can be used. When storing a value of a float type, we add letter "f" at the
end of that value but before the semicolon. See below:
To experiment with a float data type values, modify your code again like shown below and run it.
The order in which arithmetic’s operations are executed in Java is very important to understand. This
order is called Operator Precedence.
Multiplication and Division are treated equally, but have high priority than Addition and Subtraction
Addition and Subtraction are treated equally but have a lower priority than multiplication and division
If you are going to use more than one arithmetic operator in one statement, it's a good practice to
always use some brackets so as to avoid some output errors derived from operators’ precedence.
As well as storing number values, variables can also store text values. Some text type variables can
only store one character while others can store lots of characters. To store just one character,
the char type variable is used, but to store more than one character String type variable is used.
Mostly you will use String type variable as you may need to store lots of characters. We’ll create
two String variables to hold the name of a person.
To define a string variable, type the word String followed by the name for your variable. Note that
there is an uppercase "S" for String and the line ends with a semicolon.
String sir_name;
String other_name;
To assign values to the string variables, type an equal sign and then the text which should be
between two sets of double quotes:
sir_name = “Benta”;
other_name = “Smith”;
We are telling Java to display the two names together but with a space in between. Now, modify your
code like shown below and run once again.
If you want to store a single character, use a variable of type char (lowercase "c") and then surround
the character with single quotes and not double quotes:
Remember: A String variable has double quotes and a char variable has single quotes.