File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about primitive type and wrapper class

 
zheng li
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).





 
Dejan Miler
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
excellent
 
zheng li
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic