What actually happens when passing objects in java?

I know when we pass objects, we pass its link as a value. But is this value you get using the hashcode()right method (according to my tests it is the same)? Since hashcode()it is not a memory address and is not guaranteed to receive unique values ​​all the time, can there be strange things, for example, collisions during the transfer of objects?

(Assuming it is hashcode()not overridden, i.e. returns the same value as System.identityHashCode())

Three are many that are similar to these questions, but I can’t find the appropriate resource that tells what is passed to this value and how do you get it?

EDIT: Here is my test. By default, toSting()uses hashcode()internally and converts it to a hexadecimal value. So when we pass objects, is this value passed? Or what does java do to track all the objects (being transferred), so there will be no link collisions?

Object o = new Object();
System.out.println(o);
System.out.println(o.toString()); //both prints same thing - java.lang.Object@10385c1
+5
source share
5 answers

hashcode()It has nothing to do with the Java internal storage. This is just a method that should return a unique value that an object can represent.

, . , hashcode() . , hashcode() , , hashcode() .

+6

, , hashcode() ( )?

.

JSL:

( ) , .

Java

+2

hashcode Object java , .

, hashCode() . , . (new Whatever()). , hashcode() - .

+2

, hashCode() . .

JDK , a.equals(b), a.hashCode() == b.hashCode().

; .. !a.equals(b), , a.hashCode() != b.hashCode(). , . , , hashCode() .

hashCode() Object hashCode(), , .

+2

Java Objects , Object . - , . hashcode memory address., , hashcode. , , - , -.

+1

All Articles