wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion with long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusion with long" Watch "confusion with long" New topic
Author

confusion with long

Saneesh Joseph
Greenhorn

Joined: Jun 01, 2005
Posts: 1
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

Joined: Nov 07, 2000
Posts: 242
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

Joined: Jan 30, 2000
Posts: 18671
[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).


"I'm not back." - Bill Harding, Twister
Lalitha Gottumukkula
Ranch Hand

Joined: May 24, 2005
Posts: 45
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Mar 14, 2003
Posts: 810
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


Thanks and Regards, Amit Taneja
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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

Joined: Mar 14, 2003
Posts: 810
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

Joined: May 24, 2005
Posts: 45
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

Joined: Sep 24, 2003
Posts: 1608

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...")?


Tony Morris
Java Q&A (FAQ, Trivia)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Nope. Logical reasoning is Good. Absolutes are Evil.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion with long