wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper Class Object Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper Class Object Doubt" Watch "Wrapper Class Object Doubt" New topic
Author

Wrapper Class Object Doubt

Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
Please help me. I have doubt in the below code.
in //1 how a primitive type is assigned by wrapper object.

//2 how it prints the object value where a object of single member variable is not printing in line //6

//4why hashCode is also same as the value...

// what is the relation between primitive data and wrapper class as we can do Integer I = 10; or int i = I;


Please cleare my boubt....







class B {
int a;
B(int b){a=b;}
}

public class A {
public static void main(String[] args) {
Integer b = new Integer(15);
int i = b;//1

System.out.println(i);//2
System.out.println(b);//3
System.out.println(b.hashCode());//4
B b1 = new B(10);//5
System.out.println(b1);//6
}
}


Output:
15
15
15
B@187c6c7


Thank You So Much...


SCJP 1.5, SCWCD 1.4, SCBCD 5.0, next SCDJWS...
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
Please reply. Its really urgent for me.
Jessica James
Ranch Hand

Joined: Jan 23, 2007
Posts: 69
Hi..

Actually your code won't compile..
int i = b;//1
This line shows the following compilation Error..
"Type mismatch:cannot convert from Integer to int"
Prafulla N. Patil
Ranch Hand

Joined: Mar 20, 2006
Posts: 106
There is error at line no 1
it must be like this
<code>
int i = b.intValue();
</code>


Thanks, Prafulla Patil
SCJP, SCWCD
Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
It compiles or doesn't depending on what java version you use. Java 1.5 knows a little magic called auto-boxing. Look it up
Mamatha Preetham
Ranch Hand

Joined: Jan 23, 2007
Posts: 75
Hi Damodar,

Wrapper classes represents the primitive values as objects. All of the wrapper classes except Character have 2 constructors; one which takes the primitive value and the other which takes the string representation of the value.

So in your code,
Integer b = new Integer(15);

it takes in the primitive int value. Here 15 is the integer number here.
The same can be done by giving String as argument,
Integer b = new Integer("15");

//2 how it prints the object value where a object of single member variable is not printing in line //6

Here, in line 2, it is not printing the object value, it is passing the primitive value(pass by value), whereas in line 6
B b1 = new B(1000); //5
System.out.println(b1); //6
you are creating an instance of the class B, so it gives the class name followed by @ symbol, followed by unsigned hexadecimal representation of the objects code.

FYI, it is not printing the number 10, which you are passing, compile and try with other nums.

Wrapper objects are immutable. This means that once a wrapper object has a value assigned to it, that value cannot be changed
Hope, this answers all your questions.

Cheers,
Mamatha
SCJP(1.4)
[ February 20, 2007: Message edited by: Mamatha Preetham ]
Jessica James
Ranch Hand

Joined: Jan 23, 2007
Posts: 69
//4why hashCode is also same as the value...

Because a hash code value for the Wrapper object, equal to the
primitive(int)value represented by the Wrapper(Integer) object.
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
I have complied the code in java 1.5 it is error free

Thank you for your replies but still I have some confusion...

in line //3 b is also the instance of of Integer class so it should print the reference value like other object!!! But Why it is printing the value of the object? Is there any deference between simole Object and wrapper class object.... Please make me understand...
Thuwaragan Sundaramoorthy
Greenhorn

Joined: Jan 11, 2007
Posts: 26
The Integer class overrides the toString() method of the Object class, which returns the value. Thats why it prints the value in //3.


------------------------------<br />SCJP 1.5
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
Hi I got it...
Thanks!!!


I changed the code and override the toString() method and got the result...

here it is...

class B {
int a;
B(int b){a=b;}
public String toString(){
Integer a1 = new Integer(a);
return a1.toString();
}
}

public class A {
public static void main(String[] args) {
Integer b = new Integer(15);
int i = b;//1

System.out.println(i);//2
System.out.println(b);//3
System.out.println(b.hashCode());//4
B b1 = new B(10);//5
System.out.println(b1);//6
}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Wrapper Class Object Doubt
 
Similar Threads
Doubt in this()
Boxing doubt
== doubt
Doubt with ==
Doubt on Varargs and overriding