Java Reference
In-Depth Information
Now, if your friend wants you to tell him the difference of the same two numbers, he does not need to tell you
those two numbers again. This is because these two numbers are stored in your memory, and your brain can recall
and use them again. However, whether your brain can perform addition of those two numbers depends on many
factors, for example, how big those two numbers are; whether your brain can memorize (or store) those big numbers;
whether your brain is trained to do addition, etc. The process of adding two numbers also depends on the type of
these two numbers. Your brain will use different logic to add them depending on whether two numbers are whole
numbers (e.g. 10 and 20), real numbers (e.g. 12.4 and 19.1), or the mix of whole and real numbers (e.g. 10 and 69.9).
The entire process takes place in your brain without you noticing it (maybe because you are so accustomed to doing
these additions). However, when you want to do any kind of manipulation on numbers or any other types of values
in a Java program, you need to specify the details about the values you want to manipulate and the procedure for
manipulating those values.
Let's discuss the same example of adding two numbers in a Java program. The first thing you need to tell Java is
the type of the two numbers you want to add. Let's assume that you want to add two integers, 50 and 70. When you
added two numbers by yourself, your brain gave each number a name (maybe as the first number and the second
number). You did not notice the naming of those numbers by your brain. However, in a Java program, you have to
explicitly give names (also known an identifier ) to both numbers. Let's name the two numbers as num1 and num2 ,
respectively. The following two lines in a Java program indicate the fact that there are two integers, num1 and num2 :
int num1;
int num2;
The int keyword is used to indicate that the name that follows represents an integer value, for example, 10, 15,
70, 1000, etc. When the above two lines of code are executed, Java allocates two memory locations and associates the
name num1 with the first memory location and the name num2 with the second memory location. The memory states
after the execution have been depicted in Figure 3-1 as Memory State - I.
Figure 3-1. Memory states in the process of adding two numbers
These memory locations are called variables. The names num1 and num2 are associated with these two memory
locations. Strictly speaking, num1 and num2 are two names associated with two memory locations. However, roughly
speaking, you say
num1 and num2 are two variables, or
num1 and num2 are two variables of int data type, or
num1 and num2 are two int variables.
 
Search WWH ::




Custom Search