In my Java project, I have a class called myInteger with the following code:
public class myInteger {
private int data;
public myInteger(int data) {
this.data = data;
}
}
Now I have another class called MainClass which contains the main method. I am creating two objects: First object of the java.lang.Integer class and the second object of myInteger class. Then, I print both the object variables by passing the integer and myInteger to the println method.
public class MainClass {
public static void main(String[] args) {
Integer integer = new Integer(1);
myInteger myInteger = new myInteger(1);
System.out.println(integer);
System.out.println(myInteger);
}
}
I get the following output:
1
MainClass.myInteger@67ea6afc
In the case of Integer class, I get the auto-unboxed integer value. But in the second case, I get reference.
Why does this happen? Is there any way to print data field of myInteger object without using any sort of getter methods?