Why the wrapper class object id does not work as a reference variable

My question includes wrapper classes. I know that when we store a literal of a primitive type using wrapper classes, we save it as an object of this wrapper class, so the identifier of the object will be a reference variable (sort of like a pointer to C ++). For example, in Integer wi = new Integer("56") , wi is a reference variable. But if so:

  • Why can I do wi++ or wi +=2 ? Why does the compiler handle these reference variables, such as regular primitive variables? Is the reference variable referencing the reference to the object of the object?

  • Given Integer wi = new Integer("56") and int pi = 56 , why (wi == pi) returns true. Shouldn't wi store the link (address)?

And one more question: when a reference variable is passed to the method as a parameter, it is considered to be passed by reference, therefore, the modification that this reference variable occurs should affect its value, but this is not so:

 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? } } 
+6
source share
4 answers

the mn.goo(pi) operator passes a copy of the value 86 , and mn.show(wi) passes a copy of the reference variable that contains the same object.

  • Why can I do this? wi ++ or wi + = 2.i means why does the compiler deal with these referenced vriables, such as regular primitive variables? (Does the reference to the object reference variable refer to the object?)

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

2.or if we have ==> "Integer wi = new integer (" 56 ")" and "int pi = 56". why (wi == pi) returns true. wi wont have to store refernce (address)

This is due to the fact that for wrapper classes Integer == will return true to 128 . It is by design

For your doubts about 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 } } 

Output signal

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

Passing an object reference to a 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; } } 

Output signal

 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; } } 

Output signal

 Before passing the reference d.height = 10 After passing the reference d.height = 11 
+4
source

The java compiler automatically inserts intValue and Integer.valueOf calls to convert between int and Integer . For example, here is a code snippet from a question:

 void show(Integer x){ x *=100 ; } 

And here is what actually happens:

 void show(Integer x) { int unboxed = x.intValue(); unboxed *= 100; } 

As you can see, line x *= 100 does not really change the Integer object that you pass, it only changes the int value extracted from this Integer object.

Similarly, the wi == pi code from the question actually means wi.intValue() == pi , which explains your observation.

+1
source

Java uses the "call by value" concept, as described in detail here. So, in your case x * = 100; in the show method only updates the local variable

0
source

The compiler decompresses wi into a primitive data type. So, this is a primitive data type, since everything in Java goes by value, changes to formal arguments do not affect the actual arguments.

 mn.show(wi); 
0
source

Source: https://habr.com/ru/post/952358/


All Articles