• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

wrappers' initialization logic ?  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please explain why the line 3 below does not compile, but the first two lines compile ?


Thanks,
Divyesh.
 
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try following code
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divyesh,

Maybe I'm wrong, but I'd like to guess.

I think it's because the constructor for a Long wrapper takes either a String or a long. Being that all non-decimal literals are 32-bit "ints", you'd have to append an "L" to the 2:

Long l = 2L ;
Long l = new Long( 2L ) ;

The same applies to type Float, which requires an "f" appended to the value of the literal.

Float f = 2.5f ;
Float f = new Float( 2.5f ) ;

Of course, if you are not using a wrapper, you are allowed to use only "long l = 2" without the appended L, but only because the int is implicitly upcast without any loss of precision. However, that is not the case with floats, as "float f = 2.5" will create a compiler error for possible loss of precision.

Again, maybe I'm wrong, but that's my guess.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does compile at my computer with Long l = 2L
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to remember. You can box then widen. You cannot widen then box.

Integer i = 15;
This is fine: 15 is an int, and can be auto-boxed into an Integer.

Number n = 15;
Object ob = 15;
These are fine. 15 is boxed to an Integer, which can then be widened to Number or Object as Integer is a sub-class of those.

Long l = 15;
15 is boxed to an Integer...which then can't be converted to a Long. To work, the int would have to be widened to a long, and then boxed. But the compiler won't do that for you.
 
Divyeshh Patel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the replies.
Few points from my side
1. Long l=2L would work fine anyway, that was never a problem.
2. I thought the Long.valueOf(long) method is used for auto boxing, so I was wondering why int would not be auto promoted to long when we write Long l=2;
3. If the below logic is true, why do the first two lines compile ? (because Integer cannot even be converted to either Byte or Character)

Matthew Brown wrote:Long l = 15;
15 is boxed to an Integer...which then can't be converted to a Long. To work, the int would have to be widened to a long, and then boxed. But the compiler won't do that for you.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Divyeshh Patel wrote:
3. If the below logic is true, why do the first two lines compile ? (because Integer cannot even be converted to either Byte or Character)


I knew you were going to ask that .

I think it's related to the lack of literals for numeric types shorter than int. The compiler will, as appropriate, directly interpret an int literal as a short/byte/char (as long as it's small enough to fit into that type without loss).

So with Byte b = 2; the compiler realises the 2 must be a byte, and will box that.

 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Divyeshh Patel wrote:
2. I thought the Long.valueOf(long) method is used for auto boxing, so I was wondering why int would not be auto promoted to long when we write Long l=2;



According to the specification, implicit casting and autoboxing can't both be done during an assignment conversion -- it is either one of the two.... actually, there are some combinations which are allowed, but those are listed as an item in the specification.... See section 5.2.

Divyeshh Patel wrote:
3. If the below logic is true, why do the first two lines compile ? (because Integer cannot even be converted to either Byte or Character)



There are actually some exceptions to the conversion... listed further down on the specification, same section. In this case, it is related to compile time constants, and how it can be used to do an implicit narrowing conversion, in addition to the above assignment conversion. However, the specification does not list the long/Long type as part of this -- so this rule doesn't apply to the assignment to the Long type.

Henry
 
Divyeshh Patel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Henry for the explanation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!