Java Variables
Java Basics - Part 4
Forward: In this part of the series, I explain how to identifier objects.
By: Chrysanthus Date Published: 1 Sep 2012
Introduction
Recall
The memory of a computer is a long series of cells. Imagine the cells placed next to one another in one long line. A short consecutive group of these cells to store a value is called, a region. The group should have a minimum of one cell. If this region has been designated to hold a datum, such as an int or double or any of the other data types, it is called an object. Such a designated region is called an object whether or not it is actually holding datum. So, an object can consist of one or more consecutive cells; an object can hold a datum. This datum is the value of the object. There are different types of data. In the previous part of the series I talked about the int, double, char, and boolean object (data) types. These are primitive data types (object types).
The series of cells of the memory are numbered numerically, consecutively. These numbers are called addresses. So each memory cell has a unique memory address.
In these tutorials, “object type” and “data type” mean the same thing.
Before I continue, note this: An object is a region is memory. In Java parlance, this region or object is called, a field.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
If an object (field) is to hold an integer, say 45, you can write that in a statement as follows:
int myInt = 45;
The statement begins with the type of value the field (object) would hold. This is followed by a space and then a name that identifies the object. This name is called the variable. You are the one who decides on which name to give as the variable. Then you have the = symbol. Here the = symbol is called the assignment operator. Do not call it the equal sign as you do in mathematics; that would be misleading. Then you have the value of the integer. Finally you have the semicolon, which makes the line a statement. Now, we say the value, 45 is assigned to the integer field identified by myInt (that is Java parlance).
Try the following code with the class name, IntVar:
class IntVar
{
public static void main(String[] args)
{
int myInt = 45;
System.out.println(myInt);
}
}
If you have forgotten how to compile and execute a Java program, then refer to part 1 or part 2 of this series. Remember, the name of the class file (and as should be for the source file) is the name just after the word, class, in the code. For the output of the code, you should see 45 displayed in the command prompt window. I will talk about just the first two lines inside the inner block (curly brackets) of the code.
The first statement is,
int myInt = 45;
The explanation was given previously. The second statement (ending with a semicolon) is:
System.out.println(myInt);
Here, System is the variable of a special object. It is followed by a dot. Then out, which is another special object. It is followed by a dot. Then println() with parentheses is called a method of the object, out. Within the parentheses you type what will be printed (sent to) at the output. If what you want to go to the output is text, such as Hello World!, you type the text in quotes (""). If a variable such as myInt is the name of the value to be printed, you type the variable in the parentheses without quotes (otherwise it is the variable name that will be printed and not the value of the variable); in this way, the value of the variable is printed. I will come back to the expression, System.out.println(), later on. For now just know that you need it in order to print something to the output.
If a field is to hold a double, say 56.74, you can write that in a statement as follows:
double myDouble = 56.74;
The statement begins with the type of value the field holds; the object holds a double. This is followed by a space and then a name that identifies the field. This name is called the variable. You are the one who decides on what name to give as the variable. Then you have the assignment operator. Do not call it the equal sign as you do in mathematics; that would be misleading. Then you have the value of the double. Finally you have the semicolon, which makes the line a statement. And so, we say the value 56.74 is assigned to the double field, whose variable is myDouble.
Try the following code:
class DoubleVar
{
public static void main(String[] args)
{
double myDouble = 56.74;
System.out.println(myDouble);
}
}
The output should be 56.74.
The first statement in the inner block should be self-explanatory, as it is similar to the first statement of the previous code. The second statement displays the value held by the field identified by myDouble.
The char Variable
If a field is to hold a char, say 'W', you can write that in a statement as follows:
char myChar = 'W';
The statement begins with the type of value the field would hold; the field holds a char. This is followed by a space and then the variable of the field. Then you have the assignment operator. Then you have the value of the character (char). Finally you have the semicolon, which makes the line a statement. We say the value 'W' is assigned to the char field, whose variable is myChar.
With the char object, the value assigned has to be in single quotes as with 'W'. Double quotes is used for strings (see later).
Try the following code:
class CharVar
{
public static void main(String[] args)
{
char myChar = 'W';
System.out.println(myChar);
}
}
The value, ‘W’ should be printed at the command prompt window, without the quotes. The first and second statements in the block should be self-explanatory.
In the above examples, the variables are said to be declared and initialized at the same time. We shall now look at the meaning of declaring variables and initializing fields (variables).
A variable identifiers a field (object). A field is a region in memory. Declaring a field (variable) means declaring what type of datum (value) the field would hold. The following statement defines a field to be of type, int:
int hisInt;
When the program is being executed, and it sees the above statement, a number of consecutive memory cells are allocated somewhere in memory. This group of cells form a field and will have to hold only an integer, not a double or char or some other value type. This field is identified by the variable, hisInt. It can only hold an integer because of the preceding word, int. At the moment it is holding nothing. Above is an independent statement that ends with a semicolon.
You can define double and char variables in a similar way. The following variable (herDouble) identifiers a field that can only hold a double:
double herDouble;
The following variable (theChar) identifiers a field that can only hold a character (char):
char theChar;
Variables are declared this way and the syntax is:
Type var;
where var means variable, and Type stands for int, or double, or char, etc.
You can declare a variable and then assign a value to the variable’s field later in the program. The following code segment illustrates this:
int myInt;
myInt = 45;
The above code segment has two statements. The first one declares the variable, myInt. So, if the field identified by myInt will have any value, it will be an integer, and nothing else. The second statement assigns the integer value, 45 to the field identified by myInt. That is, the second statement gives the field a value (of type int). In other words, the second statement makes the value of 45 be kept in the region (group of cells) identified by myInt.
Read and try the following code:
class HerChar
{
public static void main(String[] args)
{
char herChar;
herChar = 'q';
System.out.println(herChar);
}
}
You should have the character, q at the output.
Note: After declaring, you use the variable without the preceding type, as I have done in the second and third statements in the inner block above.
Declaring more than one variable
You can declare more than one variable in one statement. Read and try the following code that illustrates this:
class MoreVar
{
public static void main(String[] args)
{
int myInt, yourInt, hisInt;
yourInt = 702;
System.out.println(yourInt);
}
}
Your result should be 702. Three variables are declared in one statement. Only one is used. That is allowed. Nothing stops you from using two or all the three for different things (assignments).
When you declare a variable and assign a value at the same time, that is initialization. Initialization is declaration and assignment at the same time. That is what I did in the top part of this tutorial. After initialization, you use the variable without the preceding type. Read and try the following code:
class InitEx
{
public static void main(String[] args)
{
double theDble = 25.63;
System.out.println(theDble);
}
}
Your output should be 25.63.
Note that in the second statement of the inner block, I have used the variable without the preceding type (double). That is, after initialization or declaration of the field, you use the variable without the preceding type.
Changing the Value of a Field
Whether you start a field by declaring and later on assign a value to it or you start by initialization where the value is assigned at the declaration stage, the value (content) of the field can be changed, later in the code. Read and try the following that illustrates this:
class FieldVal
{
public static void main(String[] args)
{
int myInt = 99;
myInt = 88;
System.out.println(myInt);
}
}
Your output should be 88, the changed value for the field identified by the same variable, myInt. Remember, after declaration or initialization, you do not need to precede the variable with the Type indicator, even when you are changing the value.
Case Sensitivity
Java is said to be case sensitive. This means for example that the variable, myInt is not the same as MyInt or MYINT or myint, and so on.
Well, we have done a lot. We should take a break here. We continue in the next part of the series.
Chrys