Java is passed as a value instead of a reference

Source: Internet
Author: User
Tags getcolor

--reference Java is pass by Value and not pass by reference

Actually this question is a very elementary question, the related concept beginner already mastered, but the time is long or is easy to confuse, hereby summarizes

First, value passing and reference passing

First here we look at the similarities and differences between the two:

    • Value passing: When a method is called, the actual parameter passes its value to the corresponding formal parameter, and the change of the formal parameter value in the method execution does not affect the value of the actual parameter.
    • Reference delivery: Also known as a pass-through address. When a method is called, the reference to the actual parameter (the address, not the value of the parameter) is passed to the corresponding formal parameter in the method, and in the execution of the method, the operation of the formal parameter is actually the operation of the actual parameter, and the change of the parameter value in the execution of the method will affect the value of the actual parameter.
Second, Java is always value-passing rather than reference-passing

Example:

Balloon.java

1  Packagecom.journaldev.test;2  3  Public classBalloon {4  5     PrivateString color;6  7      PublicBalloon () {}8      9      PublicBalloon (String c) {Ten          This. color=C; One     } A       -      PublicString GetColor () { -         returncolor; the     } -   -      Public voidsetcolor (String color) { -          This. color =color; +     } -}

Then we do a simple test, swapping two objects

Test.java

1  Packagecom.journaldev.test;2  3  Public classTest {4  5      Public Static voidMain (string[] args) {6  7Balloon red =NewBalloon ("Red");//Memory Reference8Balloon Blue =NewBalloon ("Blue");//Memory Reference9          Ten swap (red, blue); OneSystem.out.println ("Red color=" +Red.getcolor ()); ASystem.out.println ("Blue color=" +Blue.getcolor ()); -           - foo (blue); theSystem.out.println ("Blue color=" +Blue.getcolor ()); -           -     } -   +     Private Static voidFoo (Balloon Balloon) {//baloon=100 -Balloon.setcolor ("Red");//baloon=100 +Balloon =NewBalloon ("Green");//baloon=200 ABalloon.setcolor ("Blue");//Baloon = at     } -   -     //Generic Swap Method -      Public Static voidSwap (Object O1, Object O2) { -Object temp =O1; -o1=O2; inO2=temp; -     } to}

The output is as follows:

1 Red color=red2 blue color=Blue3 Blue color=red

From the above code can be drawn:

    • Two objects exchanged in a function cannot affect the two objects in the keynote function
    • But the object is modified in the modulated function (a field of the object), so it can be reflected in the keynote function.

1th, we can see that Java is really the value of the pass, but the 2nd let us wonder why the modification of the object will affect the keynote function

This involves the way data is stored in Java, and the stack store mentions in another blog post

Java Summary One Java basic type and wrapper type resolution

In addition to the basic types in Java, other type declarations hold a reference to the heap in the stack, and the object's data is stored in the heap.

So when the function is called, if the object is passed, Java is actually a value pass, opening a new storage space for the parameter, and passing the value of the argument, but the two point to the same heap of storage space, which makes the above interchange useless, but change the data in the object useful results

Java is passed as a value instead of a reference

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.