Deep understanding of the meaning and rationale of references in Java

Source: Internet
Author: User

Bottom line: References are pointing to that pair of images.

String str="aa";str有本身的内存,是一个变量,而对像"aa"也有它自己的内存空间。这时候说str引用"aa"这个对像.String tt=null;这里的tt也是一个引用变量,但是它没有引用具体的对像.String str="aa";String tt=str;这里str与tt都是一个引用变量(属于String类型的,只能引用String类型对像),记住:str它的内存空间里的值是一个地址值,tt的内存空间也是一个地址值.它们的值就是具体对像的首地址(首地址也就是内存地址)

The value of STR is ad12ee51 an address value
The "AA" two characters (that is, the specific pair of images) allocate space in memory when the first address is ad12ee51

The JVM will know that they are reference variables (as opposed to the original type int, long, etc.), and the JVM knows that a variable is the original variable and then accesses the specific memory space based on its value--an address value--that is, access to the specific image. Because Tt=str; is to copy the value of STR and an address value to the TT, so at run time the JVM found that TT is also a reference variable, according to its value (as with the value of STR) to access the specific memory space, because the TT and STR value equal, so said: TT and STR reference to a pair of image.

About reference passing values:

1 stringbuffer  new stringbuffer ();

SB is a reference variable that, when executed to new StringBuffer (), allocates a memory space for the pair to use and returns the first address of the space, that is, to return the first address to the SB variable, forming the "SB to quote this pair".

There is one way:

void addstring (stringbuffer sbe)  {Sbe.append ("function");}
Below this code:
New StringBuffer (); Sb.append ("hh"); addstring (SB); System.out.println (Sb.tostring ());
The result is: hhfunction principle: SB refers to a specific pair of images, its value is a stringbuffer to the first address of the image, execution to Sb.append ("HH"), in that specific image of the place will be added this "HH"  String, and then executes to the
AddString (SB); The actual value of SB (what is the value?)
Think of the parameter passed to the method, the parameter of that method StringBuffer SBE is just a reference variable, when executing this code is to pass SB's value to SBE,
Then the code that executes the method, SBE and SB both refer to the same pair, because their values are the same. So accessing SBE in a method is actually a visit to the same image that the original SB points to.
Quite with this code below
New StringBuffer (); Sb.append ("hh"); StringBuffer SBE=null; // here quite with the parameter of the method SBE=SB;

This is done when the method is called, and the value of SB is given to the SBE sbe.append ("function");//This sentence is equivalent to the code in the method. The result is hhfunction.

Sometimes it's the right image that SB refers to.

Deep understanding of the meaning and rationale of references in Java

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.