2

I am debugging my java program, and I have some instance "instance1" of class called "SomeClass". When I evaluate the variable "instance1", it says result = {SomeClass@816}.

What does "@816" actually mean?

I know it is not for sure the hashCode(), is it the instance memory address? If so, how can I "see" the instance address in code? Which method to call on the object itself?

Note: I am using IntelliJ Idea

public class SomeClass {

private String name;
private int id;

@Override
public String toString() {
    return this.name + this.id;
}

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    String objName =  ((SomeClass) o).name;
    return this.name.equals(objName);
}

@Override
public int hashCode() {
    return this.name.hashCode();
}
ggb667
  • 1,881
  • 2
  • 20
  • 44
Dave_dev
  • 23
  • 4
  • Could this answer help you? https://stackoverflow.com/a/1961150/11012656 – Johann Kexel Mar 12 '20 at 18:09
  • The "address"? You're seeing the `toString` representation of whatever it is--how that is constructed varies, but it is generally a representation of the hash code by default. There is no (meaningful) "address" available to the user unless you're specifically debugging a JVM, which you almost certainly are not. – Dave Newton Mar 12 '20 at 18:09
  • @DaveNewton but I am already overriding toString() method and hashCode() method (toString returns value of "name" field and hashCode() returns 0), and still I do not know where does the "@816" comes from ... – Dave_dev Mar 12 '20 at 18:15
  • You commented that _I am already overriding toString() method and hashCode() method_ Apparently not. Please [edit] your question and post the code of your `SomeClass` class. Also, how are you debugging? Is it through some IDE, like Intellij? – Abra Mar 12 '20 at 18:27
  • 2
    @Dave_dev I don't know what your IDE uses for a debugger view of the class in question. Unrelated, but hard-coded hash codes have the potential to introduce issues. – Dave Newton Mar 12 '20 at 18:31
  • I am using IntelliJ Idea, yes... and also, I changed the implementation of the hashCode(). There is edit in the question – Dave_dev Mar 12 '20 at 18:37
  • In the IDEA debugger window, if you right click on the variable name you should be able to change the repesentation method that it's using to get the displayed value – Roddy of the Frozen Peas Mar 12 '20 at 18:39
  • 1
    IIRC, IntelliJ (or maybe Java), attaches a simple increasing id to each instance of a class while debugging. That is what the debugger shows here: the (simple) class name and that id. – Mark Rotteveel Mar 12 '20 at 18:41
  • So, the conclusion is - it is notting important or code-connected :) It is just some ID attached from IntelliJ or JVM – Dave_dev Mar 12 '20 at 18:46
  • 1
    Does this answer your question? [Memory address of variables in Java](https://stackoverflow.com/questions/1961146/memory-address-of-variables-in-java) – Isaiah Mar 12 '20 at 21:46

1 Answers1

1

That 816 belongs to the identityHashCode (method System.identityHashCode()) of every java Object.

Doesn't matter wether your Class overrides or not the hashcode() method, since identityHashCode() will call the natural hashcode() method of your Object.

From the docs:

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode()

aran
  • 10,978
  • 5
  • 39
  • 69