Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"type int out of range" error on a long variable!

 
Nick Singh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind, I made a stupid blunder since I don't usually use long and never put an L at the end of the i variable.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
600851475143 is still an int literal. To use it as a long literal, you must append L to it: 600851475143L.
You can officially also use a lowercase l but you should never do that. In some fonts, the difference between l and 1 is not easy to see, so 600851475143l could be seen as both 600851475143L and as 6008514751431. An example:
Without the colouring inside that snippet it would be hard to read the difference between l1 and l2.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would further posit that l1, l2, etc. are not fantastic variable names, for similar reasons.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have me there. There isn't a problem in the declaration or assignment to those, but using them as arguments, in comparisons or on the right hand side of assignments, can cause confusion. For instance:
With some fonts you may read this as being always true - 11 == 11 after all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic