About hashcode

Asked 2 years ago, Updated 2 years ago, 97 views

I'm a beginner who is studying by self-study and online lectures.

As I studied hashcode for the first time, I was confused, but there was no right place to ask questions, so I wrote like this. The source I'm curious about is as follows. (There are many comments because there are many things to understand and move on by annotating each line rather than roughly crossing the line.) We ask for your understanding, and we would appreciate it if you could find anything wrong and give us advice.)

package ObjectExs;

public class EqualsEx1 {

    public static void main(String[] args) {
        int aa = 100;
        int bb = 100;

        String str1 = new String ("High Java"); // String str1 = "High Java".
        String str2 = "High Java";

        System.out.println(str1.hashCode()); // Output value: 422206472
        System.out.println(str2.hashCode()); // Output value: 422206472


        If (aa==bb){// Output value: aa and bb are the same
            System.out.println ("aa and bb are the same");
        }else {
            System.out.println ("aa and bb are different");
        }
        System.out.println("/////////////str1 = str2;/////////////");
        If (str1==str2){//output value: str1 and str2 are different
            System.out.println ("str1 and str2 are the same");
        }else {
            System.out.println ("str1 and str2 are different");
        }


        // == When comparing basic data types, the comparison operator compares the values.
        // The reference type comparison compares the address values.
        // equals (return value: true or false)


        System.out.println("/////////////str1.equals(str2)/////////////");
        //override box.
        If (str1.equals(str2){//output values: str1 and str2 are the same
            System.out.println ("str1 and str2 are the same");
        }else {
            System.out.println ("str1 and str2 are different");
        }

        Value va1 = new Value(10);
        Value va2 = new Value(10);

        System.out.println(va1.hashCode()); // Output value: 366712642
        System.out.println(va2.hashCode()); // Output value: 1829164700 with different address values
        //hashCode() is inherited by the object, and in principle, the address value is different

        //va1 = va2;
        //No Overriding.
        If (va1.equals(va2)){// Output value: va1 and va2 are different 
            //-> but 66 to 72 lines of equalization. 
            System.out.println ("va1 and va2 are the same");
        }else {
            System.out.println ("va1 and va2 are different");
        }
        // The comparison differs depending on the presence or absence of overriding.
        // If the ->Equals method is in the Object class, compare the address values
        // ->Equals method of String object is an overridden method, so the content is compared.

    }
} } //end of EqualsEx1
class Value{
    int a;
        // Overriding// Overriding to compare the contents
        public boolean equals(Object obj){ //Overriding
        if(obj !=null &&obj instance of Value){ // ensuring that obj is of value type
            return a == ((Value)obj).a;
        } } else {
            return false;
        }
    }
    public Value(int a){
        this.a = a;
    }
} } // end of Value

Q.1 Str1 and str2 str1.equals(str2) show that the output values of str1 and str2 are the same For va1.equals(va2), the output values are different for va1 and va2 (if not overridden). I don't know why it's different. In the course, the string is said to have been overridden, but based on this source, I'm not sure if it was overridden.

Q.2 I have a question about the override of the equals method with the bottom boolean of the source as the return type. I don't know if the parameter is object obj here. Why object? And obj as a condition of if statement!I don't know if I used =null &&obj instance of Value.

I'm sorry to ask you this question after watching the class. There is no place to ask questions, and I asked you because it is difficult to understand at my level even if I search on Google and Naver.

hashcode

2022-09-22 21:57

1 Answers

Q.1 Answer This is a question about Java's equivalent operations, == and equals.

== Operation is primitive type (int, char..) Lowercase types) compare the values themselves. However, other types of references (except for the raw type in Java, all are reference types as objects) will compare memory addresses.

The equals function is defined in the Object, the top-level object of all objects in the Java object model, which means an operation to determine whether it is equivalent to another object. Unfortunately, this function must be implemented directly from the inherited object (all objects in Java inherit the object by default).

So aa==bb is a comparison to int type and it's the same thing.

The second str1==str2 is given a new address by using newString(...) in str1, and str2 is substituted with a string constant, so it does not have the same address. So the other one is right.

The third str1.equals(str2) is the basic String implementation of Java (see the source of the Java library for this part). You can refer to it through OpenJDK, etc.) and the equality is overridden and implemented. It has the same string meaningfully, so it has to be the same. (Equals are well implemented, so it's really the same.)

Q.2 Answer As mentioned earlier, the equals function is a function of the top-level object, the Object. It is a function that is provided as a function that compares whether an object is the same as another object, and the factor becomes an object. Since all objects (classes) in Java are eventually bound to inherit the object, to be comparable to any object, the object, which is a supertype of all objects, must be printed.

And obj!=null &&obj instance of Value is not necessarily comparable to the object corresponding to this if obj Therefore, you can think of it as checking it first. The second instance of is to check whether obj is a value type, and if obj is a different type, of course it's not the same. So we're only trying to compare the equivalence of the internal variables if we're going through both of them.


2022-09-22 21:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.