why identifier of a wrapper class object does not work as a reference variable

autoboxing, java

Solution

the statement `mn.goo(pi)` passes the copy of value `86` while `mn.show(wi)` passes the copy of reference variable which holds the same object.

- why can i do this? wi++ or wi +=2 .i mean why does compiler deal with those reference vriables like normal primitve variables?(doesn't a reference variable store reference of a object?)

Because of the concept of `autoboxing` and `auto-unboxing`, `wi` is converted to `primitive`, incremented, then then converted back to `Wrapper`

2.or if we have==>" Integer wi = new Integer("56") " and "int pi = 56" . why does (wi == pi) returns true. isn't wi supposed to store refernce (address)

This is because for `Integer` wrapper classes, the `==` will return true for the value till `128`. This is by design

For your doubts regarding passign primitives and object references, Please study these programs

class PassPrimitiveToMethod
{
    public static void main(String [] args)
    {
        int a = 5;
        System.out.println("Before Passing value to modify() a = " + a);
        PassPrimitiveToMethod p = new PassPrimitiveToMethod();
        p.modify(a);
        System.out.println("After passing value to modify() a = " + a);
        // the output is still the same because the copy of the value is passed to the method and not the copy of the bits like in refrence variables
        // hence unlike the reference variables the value remains unchanged after coming back to the main method

    }   


    void modify(int b)
    {
        b = b + 1;
        System.out.println("Modified number  b = " + b);
        // here the value passed is the copy of variable a
        // and only the copy is modified here not the variable 
    }       

}

The output is

Before Passing value to modify() a = 5
Modified number  b = 6
After passing value to modify() a = 5

Passing object reference to method

class PassReferenceToMethod
{
    public static void main(String [] args)
    {
        Dimension d = new Dimension(5,10);
        PassReferenceToMethod p = new PassReferenceToMethod();
        System.out.println("Before passing the reference d.height = " + d.height);
        p.modify(d);            // pass the d reference variable
        System.out.println("After passing the reference d.height = " + d.height);
        // the value changes because we are passing the refrence only which points to the single and same object
        // hence the values of the object are modified 
    } 

    void modify(Dimension dim)
    {
        dim.height = dim.height + 1;
    }   


}

The output is

class PassReferenceToMethod
{
    public static void main(String [] args)
    {
        Dimension d = new Dimension(5,10);
        PassReferenceToMethod p = new PassReferenceToMethod();
        System.out.println("Before passing the reference d.height = " + d.height);
        p.modify(d);            // pass the d reference variable
        System.out.println("After passing the reference d.height = " + d.height);
        // the value changes because we are passing the refrence only which points to the single and same object
        // hence the values of the object are modified 
    } 

    void modify(Dimension dim)
    {
        dim.height = dim.height + 1;
    }   


}

The output is

Before passing the reference d.height = 10
After passing the reference d.height = 11

Problem

My question involves wrapper classes. I know that when we store a primitive type literal by using wrapper classes, we are storing it as a object of that wrapper class so object's identifier will be a reference variable (somehow like a pointer in c++). For instance, in `Integer wi = new Integer("56")`, `wi` is a reference variable. But if that is true: Why can I do `wi++` or `wi +=2`? Why does compiler deal with those reference variables like normal primitive variables? Doesn't a reference variable store reference of a object? Given `Integer wi = new Integer("56")` and `int pi = 56`, why does `(wi == pi)` returns true. Isn't wi supposed to store a reference (address)? And another question: When a reference variable is passed to a method as parameter it counts as passing by reference so the modifiction that happens to that reference variable should affect it's value but it doesn't: ``` public class Main { void show(Integer x){ x *=100 ; } void goo(int x){ x *=100 ; } public static void main(String[] args) { Main mn = new Main() ; Integer wi = new Integer("86"); int pi = 86 ; mn.goo(pi); System.out.println(pi); //output = 86 mn.show(wi); System.out.println(wi); //output = 86, shouldn't it be 8600? } } ```

Original source