aspose 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
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 9943
    
    6

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



Never ascribe to malice that which can be adequately explained by stupidity.
Antony Isaac
Greenhorn

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

Joined: Oct 27, 2005
Posts: 19216

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
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9943
    
    6

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."
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: assigning literal values to Wrapper objects.
 
Similar Threads
questions regarding auto box
question about primitive type and wrapper class
Widening and narrowing conversion questions
primitives, literals
Question on conversion and autoboxing