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

confusion with long

 
Saneesh Joseph
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

when i compile a java program with the following assignment ,

long l=2147483650;

i'm getting the error,

Shift.java:8: integer number too large: 20000000000
long l1=20000000000;
^

Can sombody tell me why is this?
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because, all numeric literal are integers by default in Java. If you want JVM to consider that numeric literal as long, you need to be explicit about it.

Say it as (note the small l at the end, which says, my literal is long)

long l = 2342444324324423l;
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Santhosh Kumar]: That is because, all numeric literal are integers by default in Java.

Or rather, all numeric literals which do not contain a decimal point are ints (not integers) by default. If they contain a decimal, they're double by default.

note the small l at the end, which says, my literal is long

This is an excellent example of why I think long literals should always use a capital L, which is much less likely to be confused with a 1 (one).
 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt understand. Is there not any difference between

long long1 = 123456789l; // lowercase l at the end
long long2 = 123456789L; // uppercase L at the end

Is readability is the only difference between those two?

Thanks,
Lalitha.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only difference is that the first one is Evil, and the second is Good.

Yes, readability is the only difference. But it's a pretty important difference, I think. In many fonts, it's extremely difficult to tell the difference between a l and a 1 - so it's vital to make this distinction clear whenever possible. Since Java allows l or L to indicate a long literal, using L makes things very clear, while using l does not.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Santhosh Kumar:
That is because, all numeric literal are integers by default in Java. If you want JVM to consider that numeric literal as long, you need to be explicit about it.

Say it as (note the small l at the end, which says, my literal is long)

long l = 2342444324324423l;


i m strongly aganinst this statement...because
as i didn't heard about this anywhere can you show me where its written in K&B and i wanted to say that

if it is integer...then it must be automatcially cast to long as
int->long implicitly castable and in above original post the left side declaration is already long so there should not be any problem ...


pls correct me ..i m much confused and if possible give me the link where its written or copy past from K&B ebook
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[amit]: i m strongly aganinst this statement...because
as i didn't heard about this anywhere


I think you should have a better reason than this if you want to be "strongly against" anything. Santhosh provided actual code - did you try to compile it and run it? It's a great way to learn new things.

I recommend reading JLS 3.10.1.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok jim

i have tested it...but

seriously not heard about this thing anywhere...
can anybody pls tell me or copy past K&B text...or

tell me why its soo occouring

why dont it is not getting implicitly cast to long ???



it should be atleast implicitly casted.....

i have heard about that foat literals are by default double...

but not about this.....

give reply
 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
long l=2147483650;

For implicitly getting typecasted, that literal should be a valid integer. Here, the literal is too big to be treated as an integer literal. Above, 2147483650 is exceeding the range of the int (32 bits).

Check this,


Correct me if I am wrong.

Thanks,
Lalitha
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The only difference is that the first one is Evil, and the second is Good.

Did I hear right? Does this now imply that fundamentals do indeed exist, despite claims to the contrary ("oh, it's up to logical reasoning, etc. etc...")?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Logical reasoning is Good. Absolutes are Evil.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic