Java object references and object-worthy behavior

Source: Internet
Author: User

About the Java object and pass the problem, occasionally in the look at JS, found an invalid conversion object data problems, I feel a little puzzled, think is the characteristics of JS, randomly want to confirm in Java above, the results found not JS characteristics, Java is the same and then looked up some information to find their own learning negligence, at that time did surprise a cold sweat ah, now the specific question:

When you do value conversion, you encounter the following situation

Example 1:

public static void Intvaluechange (int a,int b) {int temp = A;a = B;b = temp; System.out.println (A + "," +b);}
public static void Main (string[] args) {int a = 1; int b = 2;intvaluechange (A, b); SYSTEM.OUT.PRINTLN ("In Main method:" +a+ "," +b);}

The output is:

2,1in Main method:1,2

Found within Intvaluechange (), the value of the variable A, a, was changed, but the A/b in the main method did not change

Let's look at the following example

Example 2:

Public static void main (String[] args) {User user1 = new user ("Man", "20 "); User user2 = new user ("Woman", "up"); Objchange (User1,user2); System.out.println ("in main method : " +user1.tostring ()); System.out.println ("in main method : " +user2.tostring ());} Public static void objchange (USER NEW1, USER NEW2) {User temp =  new1;new1 = new2;new2 = temp; System.out.println (New1.tostring ()); System.out.println (New2.tostring ());} Static class user{public user () {}public user (string name,string age) {  = name;this.age = age;} private string name;private string age; @Overridepublic  string tostring () {return +  ","  + this.age;} Public string getname ()  {return name;} Public void setname (String name) &NBSp { = name;} Public string getage ()  {return age;} Public void setage (String age)  {this.age = age;}}

Output Result:

woman,18man,20in main method:man,20in Main method:woman,18

Again, we can see that the value of the variable user1,user2 has changed within the Objchange (), but the user1,user2 in the main method has not changed, so let's look at an example:

Example 3:

public static void Main (string[] args) {User User1 = new User ("Man", ""); Changeuser (user1); System.out.println (user1);} public static void Changeuser (User user1) { = "test1"; user1.age = "100";}

Input Result:

In main method:test1,100

This time we find that the value of the object in the main method has changed, the reason for this change is because Java in the passing of the argument, passing is the pointer of this argument, in the process of passing, the pointer is copied can be understood

User1--->user object 1

User2--->user object 2

In the execution of the method Objchange (User new1,user new2), User1 and User2 are not passed directly, because the passing object is not so simple, but instead passes a copy of the User1 and User2 objects (i.e. their pointers):

User1--->user object 1 <---New1

User2--->user object 2 <---new2

After entering the function we change the reference to the copy, not the User1 and User2 references:

User1--->user object 1 <---new2

User2--->user object 2 <---new1

So that we can clearly see that the changes are User1 and user2 copies, but User1 and user2 themselves do not change, so it is possible to explain why the value has always changed in the function;

The next example 3 is because within the function, the passed copy modifies the value of the pointer itself, so we'll see in main that the value of the User1 has been modified

Next we'll look at Example 4, based on Example 2 and instance 3.

public static void Main (string[] args) {User User1 = new User ("Man", "20"); User User2 = new User ("Woman", ""); user1 = Objchange (user1,user2); SYSTEM.OUT.PRINTLN ("In Main method:" +user1.tostring ());    SYSTEM.OUT.PRINTLN ("In Main method:" +user2.tostring ());} public static user Objchange (user new1, user new2) {User Temp = New1;new1 = New2;new2 = temp; System.out.println (New1.tostring ()); System.out.println (New2.tostring ()); return new1;}

Output Result:

woman,18man,20in main method:woman,18in Main method:woman,18

We can see that the value of User1 in the main method has also changed, because in Objchange (), we pass the reference to his pointer changed and we return the changed New1 as a new user object in the function, And in main he points to User1, so the value of User1 in main has changed.

As a result, when passing an object in Java we do not pass the object itself, but rather a copy of the pointer to the object that we are manipulating in the function is a copy of the object instead of the object itself. This is my understanding of the Java transitive object, if there is a wrong place you can correct each other to discuss.

Original reference:

Java object references and object-worthy behavior

Related Article

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: 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.