• Post Reply Bookmark Topic Watch Topic
  • New Topic

range of long data type  RSS feed

 
Aziza Korikova
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have simple question about the range of the primitive data type long. On the Java API, it says its ranges up to value 9,223,372,036,854,775,807. However, I am getting errors for much smaller numbers. For example, I am trying to store the number 600851475143 and getting the exception "java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>". ?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a very strange error message, I've never seen that before.

But are you identifying those large numbers as "long" literals by putting an "L" after them? Like this?



If you leave off the "L" then the compiler will treat it as an "int" literal and give you an error message. Although I would have expected a more obvious error message than the one you said you got.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aziza Korikova wrote:i have simple question about the range of the primitive data type long. On the Java API, it says its ranges up to value 9,223,372,036,854,775,807. However, I am getting errors for much smaller numbers. For example, I am trying to store the number 600851475143 and getting the exception "java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>". ?


Runtime compile errors means that you tried to run a program that didn't compile -- don't do that. And quite frankly, I hate this IDE feature. Why should you ever be allowed to run code that doesn't compile?

Henry
 
Aziza Korikova
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whoa! interesting! the L makes it work. That's weird, i've never heard that you need an L after each long? So basically I have to put the L for all numbers greater than the range of int? That seems very strange though, why can't we work with longs the way we work with ints and double, floats, etc? I mean, what is the reasoning behind this L ?
 
Aziza Korikova
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Aziza Korikova wrote:i have simple question about the range of the primitive data type long. On the Java API, it says its ranges up to value 9,223,372,036,854,775,807. However, I am getting errors for much smaller numbers. For example, I am trying to store the number 600851475143 and getting the exception "java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>". ?


Runtime compile errors means that you tried to run a program that didn't compile -- don't do that. And quite frankly, I hate this IDE feature. Why should you ever be allowed to run code that doesn't compile?

Henry


well I did it to see what exception I would get, so this feature is pretty useful in debugging! (although in this case i had no clue what this error meant lol)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aziza Korikova wrote:whoa! interesting! the L makes it work. That's weird, i've never heard that you need an L after each long? So basically I have to put the L for all numbers greater than the range of int? That seems very strange though, why can't we work with longs the way we work with ints and double, floats, etc? I mean, what is the reasoning behind this L ?



Then I guess that you never tried to use "F" for float or "D" for double? ..... although admittedly, the "D" is probably not very useful.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aziza Korikova wrote:

well I did it to see what exception I would get, so this feature is pretty useful in debugging! (although in this case i had no clue what this error meant lol)



Really?!? What would be the worth of debugging a program that doesn't even compile?

Henry
 
Aziza Korikova
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Aziza Korikova wrote:whoa! interesting! the L makes it work. That's weird, i've never heard that you need an L after each long? So basically I have to put the L for all numbers greater than the range of int? That seems very strange though, why can't we work with longs the way we work with ints and double, floats, etc? I mean, what is the reasoning behind this L ?



Then I guess that you never tried to use "F" for float or "D" for double?

Henry


nope...??? i've gone through two java books and no where have i seen this? I am also taking course on java and my professor NEVER mentioned any L's or D's or F's? I know that if for example you want to divide int by double and store result in int, you need to use the cast (int)...but I see that this is different than casting because if I do long num = (long)99999999999;, it doesnt work. But if i want to say double num = 2.014, Ive never gotten error by not putting D at the end. Same thing for floats. So why this difference between long and the other types?
 
Aziza Korikova
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Aziza Korikova wrote:

well I did it to see what exception I would get, so this feature is pretty useful in debugging! (although in this case i had no clue what this error meant lol)



Really?!? What would be the worth of debugging a program that doesn't even compile?

Henry


Well if you debug it, it becomes able to compile
 
Aziza Korikova
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is this thread marked as resolved? I asked why is there difference between long and the other types.? Why I dont get compiler error for saying just double n = 20.1 or float n = 20.3 without putting F or D, but for long there must be L? What is the reasoning behind this?
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aziza Korikova wrote:Why I dont get compiler error for saying just double n = 20.1 or float n = 20.3 without putting F or D, but for long there must be L?


I think you haven't tried what you said.

Well
is correct and you are right but

will give you an compile time error, just check it out.

Simple rule all integers i.e 55 , 22, 789 etc are of type "int" so when you store them in "long" data type variable you have to append an L or l to the number i.e. 55L or 22L etc
But when you store them (i.e. 55 , 22 etc) all integers in "byte" and "short" data type you don't have to append anything and you have to care about only one thing that the number you are storing
is in the range of "byte" or "short" as per your assignment.

Now for floating-point numbers i.e. 22.1, 22.3 , 88.6 etc all these numbers are of type "double" so when you are storing in "double" data type variable then it is optional to append an D or d to that number
but when you store such numbers in "float" data type variable you have to append an F or f to that number
example

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . ..... although admittedly, the "D" is probably not very useful.

Henry
You can use it to convert integer values to a double without writing the decimal point, eg 123D instead of 123.0. You can use it on big values; 123456789012345678901234567890D would not compile without the D.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aziza Korikova wrote:
Henry Wong wrote:
Really?!? What would be the worth of debugging a program that doesn't even compile?

Henry


Well if you debug it, it becomes able to compile



I know you are trying to be funny here -- and you probably think it is silly as well.... as why would anyone want to debug a program just to figure out what the compiler told them without running it?

BTW, I am harping on this, in the chance that there are others that think this is a good idea. Don't do it. There is no worth in running a program that didn't compile -- it's just silly.

Henry
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote:Simple rule all integers i.e 55 , 22, 789 etc are of type "int" so when you store them in "long" data type variable you have to append an L or l to the number i.e. 55L or 22L etc

No you don't. Any int literal value fits into a long. You only need to append the L if a) the literal does not fit in an int, or b) you explicitly need the value to be a long (to influence the result of a calculation). An example of the latter is 1000L * 60 * 60 * 24 * 365 (one year in milliseconds, ignoring DST etc). If none of these literals is marked as a long, then the result will overflow.

Aziza, you should know that it's also allowed to use a lowercase l instead of an L. You should however never use this yourself. In some fonts it's too hard to see the difference between a 1 and a l. A good example that confused me once is the constant values Javadoc page of Java 1.4. Just check this. For a similar class I once mistook the trailing l for a 1, and I didn't get why it wasn't working as expected. Fortunately, since Java 5.0, the Javadoc pages use L now.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: Any int literal value fits into a long. You only need to append the L if a) the literal does not fit in an int, or b) you explicitly need the value to be a long


Thanks ......What you said is much more precise and good.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:[ . . . it's also allowed to use a lowercase l instead of an L. . . . . Fortunately, since Java 5.0, the Javadoc pages use L now.
So does the latest Java Language Specification:
The suffix L is preferred, because the letter l (ell) is often hard to distinguish from the digit 1 (one).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!