Primitive Type Variables and Reference Variables

2.2 Primitive Type Variables and Reference Variables

In previous chapter, you learned how to declare a primitive type variable, and string variable.
Consider the following statements:
int rollno;
This notifies the compiler that you will use rollno to refer to data whose type is int. this declaration also reserves the proper amount of memory for the variable.
rollno = 19;
This statement tells the compiler to store 19 to memory space.
Following figure illustrate that a primitive type variable holds the data with which it is associated.
Consider the following statements:
String msg;
This statement allocates memory space for the variable msg. However, unlike the variable rollno, the variable str cannot directly store data in its memory space. You must assign an String object to msg before you use it in your code.
For example:
   msg = "Programming is fun";
the statement stores the string object "Programming is fun" in this memory space, and then stores the starting address of string in the memory space of msg. msg is called as reference variable.

No comments:

Post a Comment

Pages