File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes assigning literal values to Wrapper objects. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "assigning literal values to Wrapper objects." Watch "assigning literal values to Wrapper objects." New topic
Author

assigning literal values to Wrapper objects.

Antony Isaac
Greenhorn

Joined: Dec 10, 2009
Posts: 4
Hi,

I was wondering why I get an issue with type Long..

Byte b = 127; //ok
Short s = 1000; //ok

Long l = 10; // not ok

So all i can figure is that all the literal values (127,1000 and 10) are of the primitive type int. In the case of Byte and Short, the compiler automatically checks the value and sees that the value can be assigned to the wrapper type, but why does it not work for Long?

Sorry if i am missing something obvious.

Tony
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Java assumes literals are 'int's, but it needs a long. you can fix this by putting a lower-case L after the 10:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Antony Isaac
Greenhorn

Joined: Dec 10, 2009
Posts: 4
Thanks, that makes it clear.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  19

You shouldn't use a lowercase L. It looks too much like a one, and will confuse people. Use an uppercase L instead: 10L. It does the same but is less confusing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

fair point. i always have issues with capital letters, however...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

ee cummings does java
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
fred rosenberger wrote:fair point. i always have issues with capital letters, however...


From now on, you shall be known (at least to me) as fRED rOSENBERGER.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: assigning literal values to Wrapper objects.