aspose file tools*
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 Java 8 in Action this week in the Java 8 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about primitive type and wrapper class
 
Similar Threads
conversion of primitives
casting variables
Method Overloading
Can anyone tell the result of this code.
Float literals.