0
class Point {
    private int xPos, yPos;

    public Point(int x, int y) {
        xPos = x;
        yPos = y;
    }
    public static void main(String[] args) {
        System.out.println(new Point(10,20));
    }
}

The output of above code returns the same ClassName@hex version of the object's hashcode, although the text I am referring to (OCPJP Guide by S G Ganesh and Tushar Sharma) states "The Hexadecimal value will be different for each instance." Am I not understanding this correctly?

WannaBeCoder
  • 1,280
  • 2
  • 17
  • 40
sk89
  • 51
  • 1
  • 9

5 Answers5

3

For hashCode #JavaDoc says

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer...

Here you are talking about ClassName@hex which will remain same during the execution of program but if you are running the program again and again you may get this representation different every time or same few times as it depends on hashcode and subject to memory allocation.

akash
  • 22,664
  • 11
  • 59
  • 87
2

Try making two instances,

System.out.println(new Point(10, 20));
System.out.println(new Point(10, 20));

And you'll get two distinct hashCodes.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Yes that did happen. But what I want to understand is, should running the original program, give me the same output each time? – sk89 Dec 31 '14 at 10:13
2

You can't predict hashCode that will be called on your reference but you can find that always hashcode will be unique. By default toString() prints hashCode() which your class inherited from Object class. First you should know when to use hash code. If you write following code you'll see two distinct hashcodes of Object's hashCode() implementation which you are inheriting.

public class Point {

    private int xPos, yPos;
    public Point(int x, int y) {
        xPos = x;
        yPos = y;
    }
    public static void main(String[] args) {
        System.out.println(new Point(10,20));
        System.out.println(new Point(10,20));
    }
}

But It won't work if you want to insert your object into HashSet, HashMap collections. For this to work you must override hashCode() in your Point class. I couldn't write that code but overriding hashCode() depends on your own requirement which I don't know...
So finally what I meant to say is It's a best practice to implement hashCode(), toString(), equals() methods in our each and every custom classe.

Kishore Kumar Korada
  • 1,204
  • 6
  • 22
  • 47
0

Yeah!... make two like above and try...

public static void main(String[] args) {System.out.println(new Point(10,20));}

the above code is creating one only..

Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
0

If you will run the same program multiple times so there is uncertainty that it will return same HashCode or different HashCode for same Object. that depends upon Your Hashcode() implementation.

This integer need not remain consistent from one execution of an application to another execution of the same application.

Prashant
  • 2,556
  • 2
  • 20
  • 26
  • I am using the default toString implementation. I just want to know if the output will remain same for multiple execution of the same program. – sk89 Dec 31 '14 at 10:16
  • Yes that might be same for several execution there is no certainty when it will give new hashCode. better you can override hashCode() and write some expression that will help you to check the output. – Prashant Dec 31 '14 at 12:25