Home > Blockchain >  Why Integer object prints the value instead of it's address?
Why Integer object prints the value instead of it's address?

Time:03-13

Generally, non-primitive data-types stores reference of the data. While we try printing the array we get its address. But why we are getting the data printed instead of its address in the case of ArrayList, LinkedList, etc? Same with the wrapper classes,

Integer i=5;
System.out.println(i);

we get its value as output! I'm confused! Kindly explain to me what's going on? Am I missing any fundamental thing in understanding? If possible, share the source.

CodePudding user response:

That happens because the Integer class (that's a class that wraps a value of the primitive type int in an object) has an implementation of the method toString. So when you try to print it, it's the overriden toString method that's being actually called.

Here's the implementation you can find on the Integer class:

public static String toString(int i) {
        int size = stringSize(i);
        if (COMPACT_STRINGS) {
            byte[] buf = new byte[size];
            getChars(i, size, buf);
            return new String(buf, LATIN1);
        } else {
            byte[] buf = new byte[size * 2];
            StringUTF16.getChars(i, size, buf);
            return new String(buf, UTF16);
        }
    }

CodePudding user response:

When you use:

System.out.println(anyObject);

Actually you are implicitly calling the toString() method of that object. You can see the toString() definition for Integer from API Java:

When a class does not have the toString() method overriden, it will use the Object class implementation:

CodePudding user response:

For example we have 2 model classes. Model1 and Model2. Notice the code for each of them.

Model 1 Code
public class Mode1 {
    private String name;
    private int age;

    public Model1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Model setName(String name) {
        this.name = name;
        return this;
    }

    public int getAge() {
        return age;
    }

    public Model setAge(int age) {
        this.age = age;
        return this;
    }

    @NonNull
    @Override
    public String toString() {
        return "Model{"  
                "name='"   name   '\''  
                ", age="   age  
                '}';
    }
}

Model 2 code
public class Model2 {
    private String name;
    private int age;

    public Model2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Model setName(String name) {
        this.name = name;
        return this;
    }

    public int getAge() {
        return age;
    }

    public Model setAge(int age) {
        this.age = age;
        return this;
    }
}

Now in java class I create an object of each of them.

static class TestClass{
        public static void main(String[] args){
            Model1 model1 = new Model1("Sambhav",18);
            Model2 model2 = new Model2("Sambhav",25);
            System.out.println(model1.toString());
            System.out.println(model2.toString());
        }
    }

Now, when I run, I get the following output.

Model{
    name=Sambhav
    ,age=18
}
Model@5fh9hf8

We see that in the first model, when printed, gives the text that we provided in the toString method. But, in the second model, we don't have one. And, then we get the result as Class Name @ It's hashcode. This is why we see this value.

Hope It helps

  • Related