Java value and pass-through references

Source: Internet
Author: User

Is it a pass-through or a reference in Java? In fact, regardless of the type of Java parameters, all are copies of the passed parameters. If Java is a value, then a copy of the value is passed, and if Java is a reference, then a reference copy is passed.
In Java, variables fall into the following two categories:

    1. Basic type variable (INT,LONG,DOUBLE,FLOAT,BYTE,BOOLEAN,CHAR), a copy of the value passed
    2. The object type, which is a copy of the reference. A pointer to an address is actually copied. (in C + +, when a parameter is a reference, a real reference is passed instead of a reference copy).

String is also an object-type variable in Java, so it passes a reference copy. Just a string is a non-mutable class, making it no different to pass a value or pass a reference.
For the basic type, the value of the transfer is to copy one of their own copies, even if their own copy of the change, you do not change. For an object type, it passes a reference copy to its own address, rather than a copy of its own actual value. Why? Because objects are placed on the heap, on the one hand, the speed is slow, on the other hand, the object itself is relatively large, re-copying wasted memory. Just like you want to Zhang San (function) to open the warehouse and check the contents of the library (warehouse = address), it is necessary to create a new warehouse to Zhang San it? No need, just copy the key (reference) to Zhang San.
Example:

publicclass test {    publicstaticvoidtest(boolean test) {        test = !test;        System.out.println(test);    }    publicstaticvoidmain(String[] args) {        booleantrue;        System.out.println(test);        test(test);        System.out.println(test);    }}

True
False
True

The base type, passed by value. In fact, the value of the parameter is passed as a copy into the function, and the function changes the value of the copy, not the original value, so the original value in the main function does not change.
  

 Public  class test {     Public Static void test1(StringBuffer str) {Str.append ("World"); } Public Static void test2(String str) {str ="World"; } Public Static void Main(string[] args) {StringBuffer S1 =NewStringBuffer ("Hello");        Test1 (S1);        System.out.println (S1); String s2 ="Hello";        Test2 (S2);    SYSTEM.OUT.PRINTLN (S2); }}

HelloWorld
Hello

Test1 (S1), S1 is a reference to a referenced copy, the address of an object. The object is modified by referencing the copy to find the address and modifying the value in the address. StringBuffer is to produce a piece of memory space, the related operations are still in the same memory, Str pointed to the reference has not changed.
Str= "World", the system automatically generates a new string object, sets the new object value to "World", and assigns a reference to the object to Str. The equivalent of STR's original point to "Hello" is now the address that points to "world". The object is new, unrelated to the original. When the function is finished, the STR function disappears, and the value on the original memory address is not changed, so print or hello.
  

 Public  class Example {String str =NewString ("Good");Char[] ch = {' A ',' B ',' C '}; Public Static void Main(String args[]) {Example ex =NewExample ();        Ex.change (Ex.str, ex.ch); System.out.print (Ex.str +"and");    System.out.print (ex.ch); } Public void  Change(String str,CharCh[]) {str ="Test OK"; ch[0] =' G '; }}

Good and GBC

In the change method, the newborn becomes a Str object, which is independent of ex.str in Main, the method ends, and the new object message, so ex.str or good.
But the nature of the value of the array is also a copy of the address value. Well, for example, a warehouse has a key A, the copy is equivalent to now re-equipped with an identical key B, but still point to the warehouse. When ch[0]= ' G ', it is equivalent to changing the resources in the repository through this alternate key B. Wait for the backup key B to end, that is, when the function ends, and then open the warehouse with the original key A, the value has changed.
For an array, the meaning of the ch[0]= ' G ' statement in the function is to change the contents of the in-memory offset of CH to G, that is, the content of the object pointed to by CH has changed, but CH itself has not changed.

Java value and pass-through references

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.