This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assigning literal values to Wrapper objects.

 
Antony Isaac
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java assumes literals are 'int's, but it needs a long. you can fix this by putting a lower-case L after the 10:

 
Antony Isaac
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that makes it clear.
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fair point. i always have issues with capital letters, however...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ee cummings does java
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic