Java class type variables

Source: Internet
Author: User

In java, there is a variable called the class type, which is different from the method for storing the value of the basic type variable. Both basic variables and class variables are implemented as a memory location. However, because the amount of memory required for the basic variables is the same, the system can set a fixed space for the variables to keep the named object. Variables of the class type are different. Due to its uncertain length, it is difficult for the system to allocate a fixed space to save the variables of the named object. Therefore, for a class type variable, it stores the memory address of the object rather than the object itself. The following is an example:

/*** Comments: special instance for assigning values between classes * @ author plug-in name * 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"); // value assignment between class types, which is to assign the memory address of str1 to str2. After the value is assigned, it points to the same memory address str2 = str1; str2.setStr ("333333"); System. out. println (str1.getStr (); // The output result is 333333 System. out. println (str2.getStr (); // The output result is 333333 }}

From the output, we can see that the class type variable does not simply store the assigned value in the naming object, but stores the address in the class type variable. Therefore, the value assignment statement between class variables assigns the memory address, and the last two variables point to the same address.

This article is from the blog "plug on the name of pigs", please be sure to keep this source http://zuohao1990.blog.51cto.com/6057850/1296239

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.