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
j=new Integer(5)
andInteger i =5
i doubt thati==j
returns true 3) You shouldn't usenew Integer
constructor alwaysvalueOf
orparseXXX
– Myalgiaint i = 56
andInteger.valueOf(56)
, the 'reference' equality is true, I imagine because theInteger
is unboxed and twoint
types are compared. @chrylis There is indeed anInteger
constructor that accepts aString
. – Divide