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);
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);
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);
Output signal
Before passing the reference d.height = 10 After passing the reference d.height = 11
source share