Java is passed by value, passed by reference

Source: Internet
Author: User

In general, we would say that Java primitives are passed by value, and objects and arrays are passed by reference. But in fact it's just a superficial phenomenon. Essentially, Java is passing references by value. (The concept of "reference" in Java is equivalent to a pointer in C + + and can change values continuously)

One, the object

the difference between an object and a variable (instance) :

The object is saved in the heap, and the variable is stored in the stack; the entry address of the object is unpredictable, so the program can only access the object through a variable, which is a reference to the object.

Example 1:

class Word {  String word;    Public Word (String word) {    this. Word = word;  }     Public void print () {    System.out.println (word);   New Word ("Every day"=new Word ("every night!") ); O1.print ();

The result is every day.

The first sentence O1 = new Word ("Every Day");

First create a Word instance, the object, and then assign the reference to O1.

The second sentence O2 = O1;

O1 assigns a reference to the object to O2, noting that the assigned value is a reference to the object and not to the O1 itself.

So, in the third sentence O2 = new Word ("every night!");

is to create a new object, and then assign a reference to the new object to O2.

Since O1 and O2 are value-transmitted, the change to O2 does not affect the O1 in the slightest.

Example 2:

We have added a new method to the Word class

 Public void Setword (String word) {  this.word =new Word ("Every day"= O1;o2. Set Word ("Every night!" ); O1.print ();

The result is "every night!".

This is not a variable, because O1 just holds a reference to the object, and after execution, O1 still holds the object's reference. So, the O1 is not changed, but the object that the O1 refers to is changed.

Two, basic type

Data storage for stacks

Data in the existing stack can be shared. such as: int a = 3; int b = 3;

The compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then finds the value of 3 in the stack, and if it does not, stores 3 in it and points a to 3. then the int b = 3 is processed, and a reference to B is created, and then, since there is already a value of 3 in the stack, point B directly to 3. At this point A = 4 is made; The compiler will then re-search the stack for 4, if not, store 4 in and point A to 4, and if so, point a directly at the address. So a change does not affect the value of B.

Thus, the basic type of Java is also passed by value.

Example 3:

 Public classtest{ Public Static voidMain (string[] args) {String str=NewString ("World"); CharCh[] = {' H ', ' e ', ' l ', ' l ', ' O '};        Change (STR,CH); System.out.print (str+ "and");    SYSTEM.OUT.PRINTLN (CH); }     Public Static voidChange (String str,Charch[]) {STR= "Change"; ch[0] = ' C '; }}

The result is world and cello

Analysis: String in Java is a special wrapper class data that can be created in the following two ways

1) String str = new String ("abc"); stored in the heap

2) String str = "ABC"; stored in the stack

Use the Equals () method when comparing values in a class, and test whether a reference to two classes is to the same object, using = =. So

String str1 = "abc"= "abc"; System.out.println (str1//truenew String ("abc"= newstring ("ABC  " // false

Java is passed by value, passed by 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.