This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about primitive type and wrapper class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about primitive type and wrapper class" Watch "question about primitive type and wrapper class" New topic
Author

question about primitive type and wrapper class

zheng li
Ranch Hand

Joined: Jun 16, 2009
Posts: 56

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
http://www.coderanch.com/t/417622/Programmer-Certification-SCJP/certification/Golden-Rules-widening-boxing-varargs#2110869

Just remember 3rd rule from the above link, widening + boxing is not allowed.

Generally when you write



It is converted by compiler to


But when you write


Then compiler should use this function of Long class for autoboxing:


here Long.valueOf(long) is taking long argument and in Long l=5; we are giving int argument.
Means the flow for this autoboxing is going through widening first.
Means int 5 needs to be converted to long 5, then long 5 should be autoboxed to Long wrapper class.

And java compiler does not support widening + boxing. Same allows for Double.valueOf(double) and Float.valueOf(float).






SCJP 6
Dejan Miler
Ranch Hand

Joined: Nov 14, 2009
Posts: 56
Hello
Number 5 or any decimal number is of integer type (integer literal).
First of all this is legal because byte, short and char are of type int



And second this are not legal because thy are not of int type (compiler cannot do auto boxing )
We can solve the problem in the two ways
First by modifying the given value like this



Or we can use casting like this



Dejan.

SCJP 1.6 in progress ....
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
excellent
zheng li
Ranch Hand

Joined: Jun 16, 2009
Posts: 56
Dejan Miler wrote:Hello
Number 5 or any decimal number is of integer type (integer literal).
First of all this is legal because byte, short and char are of type int


I thought wrapper class Byte, Short, Char, Long work the same way, but Byte, Short, Char accept integer type without any kind of casting, which confuses me.
If default decimal number type is long, I think
Integer i = 5
will be legal.

Thank all of you
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: question about primitive type and wrapper class
 
Similar Threads
casting variables
Float literals.
Can anyone tell the result of this code.
conversion of primitives
Method Overloading