Java: Class type variables

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.