A question about Java Pass & Pass Reference

Source: Internet
Author: User

<pre name= "code" class= "Java" >public class JavaTestDemo1 {public static void main (string[] args) {Integer V1 = new I Nteger (1); Integer v2 = v1;dosomething (v2); System.out.println (v2); System.out.println (v1 = = v2); System.out.println (V1.equals (v2));} public static void DoSomething (Integer i) {i = new Integer (10);}}


The result of the execution of the above code is:

1 True True


Analysis:

Java objects are passed by reference in the parameters, and the initialization of V1 and V2 points to the same address space.

The following 2 questions are raised:

1.Java function arguments, the object is a reference, why the value of V2 is still 1 instead of 10

2. In DoSomething, V2 should point to the memory address where the integer (10) is located, so why is the value of V1.equals (v2) true?


Let's first look at the role of hashcode and equals in the Java base class object:

The Hashcode () method is used to obtain a unique integer for the given object. This integer is used to determine where the object is stored in a similar structure in Hashtable. By default, the Hashcode () method of the object class returns the number of memory addresses stored by the objects.

That is, if two objects point to a different address, then the value of equals must return FALSE.


Answer the first question first:

Why the value of V2 is 1 instead of 10: Because of what is done in dosomething: Create a new I object, point to V2, and then let the I object re-point to the address of the new Integer (10), leaving it completely out of the relationship with V1,v2. So v1, the value of V2 is still 1 (note, integer,double, and so on itself, value is also declared with final, immutable).


Second question:

V1,v2 always points to the same object, which is sure to return true with equals. And let's look at the original source of the integer to find that the integer has overridden the equals function in Obejct, as long as the value is the same, even if the integer object that specifies a different address is compared to the equals comparison, the return result is true

public boolean equals (Object obj) {    if (obj instanceof Integer) {        return value = = ((Integer) obj). Intvalue ();    }    return false;}









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A question about Java Pass & Pass 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.