Java.hashcode() I have a question!

Asked 2 years ago, Updated 2 years ago, 121 views

I want to see the memory address where the actual data is stored in Java. Like you use & in C. I looked it up and found a .hashcode(). I have a question about hashcode.

If there is such a code,

 public static void main (String[] args) {
        Car k = new Car();
        Car a = new Car();
        System.out.println(a); // Car.Car@15db9742
        System.out.println(k); // Car.Car@6d06d69c
    }
public static void main (String[] args) {
        Car k = new Car();
        Car a = new Car();
        System.out.println(a); // Car.Car@15db9742
    }
public static void main (String[] args) {
        Car k = new Car();
        Car a = new Car();
        System.out.println(k); // Car.Car@15db9742
    }

Annotated values are displayed.

I created different objects with 1> new, but why does k and a have the same hashcode value? Is it the same because it handles objects that are not used because of garbage collector?

2> Does hashcode refer to the address on the heap? I heard that objects made using new are usually stored and used in hip space. Does hashcode return the memory value where the object is located in the heap space?

3> If you objectify int using Integer and hashcode, you can see that it outputs exactly the number you entered, why is that? I want to see the address of the variable declared Integer. Is there any way?

java hashcode

2022-09-22 19:22

2 Answers

1) Use hash values to distinguish between individuals in classes such as HashMap. You can view hash as a value to determine the object. The same hash value is not the same object. For example, if you have the data of number values 30, 35, 40, and if you leave the hash formula n%10, 30 is 0, 35 is 5, and 40 is 0. 30 and 40 have the same hash value. Of course, it's a different data. The hash value is used to speed up the search of objects in classes such as maps. Even when comparing objects, if hash is wrong, it is judged to be wrong immediately without comparing the total value, which helps to improve speed.

2) hashcode is not an address value. There is no way to see the heap address directly in java. The object does not always have the same address because memory is managed internally by the JVM.

3) That's because the algorithm that makes the hashcode is like that. Typically, for objects you create, you can create your own hash algorithm.

Lastly, I want to know the reason why I'm curious about the address price. For Java, developers cannot be involved in gc (you can change the gc policy). It would be comfortable for you to think that it's a completely different language from C. If you want to know about Java's memory management, you can read the articles related to Gc. I think you can also understand why the memory value is unknown.


2022-09-22 19:22

Although classes (types) are the same, instances created based on that class are treated as individual. It's called an individual.

The System.out.println() method outputs the character returned by the toString() method owned by the object when the factor is an object type.

Since the toString() method is not overridden in the toString() class you created yourself, the toString() method of the Object class will be called, and Object.toString() will be written to output a hash code value of the instance.

You can think of the hash value as the unique value and ID of the instance.

I don't know much about memory...

System.out.println() invokes the toString() method of the factor. Integer.toString() is written to return the int value that the instance has, unlike Object.toString().


2022-09-22 19:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.