How to print the address of an object if you have redefined toString method

后端 未结 5 2101
梦毁少年i
梦毁少年i 2020-12-05 02:18

I\'m a newbie to Java. Now I\'m studying equals and == and redefinition of equals and toString.

I would like to use both the toString method that I have redefied and

相关标签:
5条回答
  • 2020-12-05 02:25

    Here is an in-depth answer about overriding equals and hashcode

    What issues should be considered when overriding equals and hashCode in Java?

    The key point being The relation between the two methods is:

    Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().

    0 讨论(0)
  • 2020-12-05 02:28

    If you want to achieve sort-of default toString() behavior, you can make use of System.identityHashCode() method. Default toString() will then look like this:

    public String toString(Object o) {
        return o.getClass().getName() + "@" + 
               Integer.toHexString(System.identityHashCode(o));
    }
    
    0 讨论(0)
  • 2020-12-05 02:29

    You can call super() method to execute the corresponding superclass method.

    class Employee{
    ...
        public String toString(){
             String s = super.toString();
            return getClass().getName() + "[name = " + name + 
                ", salary=" + salary + ", hireDay=" + hireDay + "]" + s;
        }
    

    toString() in Object class is as follows

    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    
    0 讨论(0)
  • 2020-12-05 02:32

    Strictly speaking, you can't print the address of an object in pure Java. The number that looks like an object address in the String produced by Object.toString() is the object's "identity hashcode". It may or may not be related to the object's current address:

    • The specs do not say how the identity hashcode number is calculated. It is deliberately left unspecified.

    • Since the number is a hashcode, it cannot change. So even though it is (typically) related to an object address, that will be the object's address at the time when the hashcode was first accessed. This could be different to its current address, and it will be different if the GC has moved the object since the first time the object's identity hashcode was observed.

    • On a 64bit JVM (with a large enough heap size / not using compressed oops) addresses won't fit into an identity hashcode number which is returned as an int.

    Anyhow, the way to get this number is to call System.identityHashCode(obj).


    If you really want an object's current address, you can get it using JNI and a native method (and some abstraction breaking), or by using methods in the Unsafe class. But beware that both of these approaches are non-portable ... and that the object addresses that they give you are liable to "break" when the GC runs.


    For the doubters, this is what the Java 10 javadocs say on the "hashcode != address" point:

    "(The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)"

    Emphasis added. Indeed, with recent JVMs, the default behavior is to NOT base the hashCode on a memory address at all. It has been that way since at least Java 7.

    You can confirm this by including -XX:+PrintFlagsFinal to find out what the hashcode flag defaults to, and then looking at the OpenJDK source code to see what it means. (The code is in the "vm/runtime/synchronizer.cpp" file in some versions, but YMMV.)

    0 讨论(0)
  • 2020-12-05 02:48

    You may create another method inside your Employee class to use super toString method.

    See example:

    public class Employee {
        public String toString() {
            return "MyToStringMethod";
        }
    
        public String superToString() {
            return super.toString();
        }
    
        public static void main(String[] args) {
            Employee b = new Employee();
            System.out.println(b);
            System.out.println(b.superToString());
        }
    }
    

    or combine both in one method:

    public class Employee {
        public String toString() {
            return super.toString() + " MyToStringMethod";
        }
    
        public static void main(String[] args) {
            Employee b = new Employee();
            System.out.println(b);
        }
    }
    
    0 讨论(0)
提交回复
热议问题