This is a simple Java code:
public class JTest {
public static void main(String []args) {
Integer a = new Integer(2);
Object b = a;
System.out.print("r = " + b);
}
}
All objects have a parent Object in Java.
When you run this program you will get:
r = 2
Why?
If I do the same thing with this code:
public class JTest {
public static void main(String []args) {
A a = new A();
Object b = a;
System.out.print("r = " + b);
}
}
Where the class A is:
public class A {
int a;
}
The output will be:
r = test.A@9304b1
it will invoke
toString()method of object on which method called on. if its not implemented Object class provides one by default.Try overriding it by this way and check the output