0

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243

6 Answers6

0

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

rorschach
  • 2,871
  • 1
  • 17
  • 20
0

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.

YMomb
  • 2,366
  • 1
  • 27
  • 36
0

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

xxlali
  • 996
  • 2
  • 15
  • 43
0

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.

Sriram S
  • 533
  • 3
  • 26
0

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.
  }
}
Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

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.

Sathyendran a
  • 1,709
  • 4
  • 21
  • 27