In Java there is a variable called a class type that differs from how the underlying type variable stores the value. Whether it is a basic variable or a class-type variable, it is implemented as a memory location. However, because the amount of memory required for the underlying variable is the same, the system can give it a fixed space to keep the named object variable. The class type variable is different, because its length is indeterminate, which makes it difficult for the system to allocate a fixed space to save the variable of the named object. Therefore, for a class-type variable, it stores the memory address of the object, not the object itself. Here's an example to prove that:
/**
* Comments: Examples of characteristics of assignment between class types
* @author Korean internal division of labor
* Create time:2013-09-12
*
*/
Public class Stringtest {
private String str;
Public String Getstr () {
return str;
}
Public void Setstr (String str) {
this.str = str;
}
Public static void Main (string[] args) {
stringtest str1=new stringtest ();
stringtest str2=new stringtest ();
str1.setstr ("111111");
str2.setstr ("222222");
//The assignment between the class types is to assign the memory address of the STR1 to the str2, which points to the same memory address when the value is assigned
str2=str1;
str2.setstr ("333333");
System.out.println (Str1.getstr ()); Output is 333333
System.out.println (Str2.getstr ()); Output is 333333
}
}
As can be seen from the output, the class type variable is not simply stored in the named object, but the address is stored in the class type variable. So assignment statements between class-type variables are assigned a memory address, and the two variables point to the same address .
Java: Class type variables