简体   繁体   中英

Difference between wrapper class reference and user defines class reference in java

Suppose I have a custom class, say Test.

Test test = new Test(); // test is the reference.

Now when I print the value of test, it returns the hashcode .

Now consider,

Integer i = new Integer(10);

When I print the value of i , it returns 10.

Can someone help me to understand what exactly is the difference here? I believe both are object references, but for wrapper class reference, it returns the value of the object it is pointing to.

When you create a new class, it inherits the method toString() from Object . Integer class overrides that method to return the inner value.

When printing, there is an implicit call to toString() method.

By default (in for your Test class) it uses the one inside Object class. For Integer, it convert the Integer to a String in 10-base.

Your Test class is using Object class's toString() method which prints hashCode. But for Integer class, toString method is overrided. You can see Integer.java here

user defined reference is an object,if you print that object means you may get some hash code because every class extends Object class,so your also have the property (method) tostring().

Wrapper class wraps its respective primitive data type Integer i = new Integer(10); and i=10; both same in value.

When you call System.out.println(Object) (or, more generally, PrintStream.println(Object) ):

This method calls at first String.valueOf(x)

String.valueOf(Object) returns:

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

Neither of your objects are null , so the toString() method of the instances is called.

In the case of Integer :

The value is converted to signed decimal representation and returned as a string

In the case of Test , unless you've explicitly overridden it (or a superclass has overridden it), you will call Object.toString() :

[T]his method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode()) 

If this isn't the desired behaviour, override toString() in Test :

class Test {
  @Override public String toString() {
    // ... Your implementation.
  }
}

Whenever you print the object Java will invoke the toString() method. The default implementation of toString() available in Object Class. Object is the base class for the all the Object in java.

 public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

It will print the class Name with full package path @ and HashCode of the object.

The test class doesn't override the toString() method. But all the wrapper class in java override the toString() .so when You invoke the Integer method it invoke the toString() implemented in Integer class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM