• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question About long Integers  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Obviously, I haven't started to manipulate the datum yet.

According to the book, Java: A Beginner's Guide, the smallest negative number you can have in a long is -9223372036854775808. I try to compile the code above and javac throws an error:


I've tried to search for the answer, but I find the answer -2^63. I'm looking for the actual number in decimal.
-2^63 in google gives me -9.223372e+18. I know I should be able translate that into decimal, but I find my skills lacking.

So, question one, what's u[ with the error in the book? (Is it an error? Am I doing something wrong?)
Question two, what is the actual number?

Thanks.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to make a numeric literal represent a long you need to put an 'L' on the end.
Try
And if you want the lowest possible long value, just use
 
Douglas Knapp
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Joanne. The book hasn't told me that yet.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't put L at the end it will be int literal. Of course -9223372036854775808 doesn't fit into the int thus the error.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Douglas Knapp wrote:Thank you, Joanne. The book hasn't told me that yet.
Which book?
 
Douglas Knapp
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,
I'm starting(again) with Java: A Beginner's Guide. I took the recommendation from the book section here. I thought 9 out of 10 horseshoes made it a good place to start.

Doug
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Douglas Knapp wrote:I'm starting(again) with Java: A Beginner's Guide. I took the recommendation from the book section here. I thought 9 out of 10 horseshoes made it a good place to start.

Seems reasonable. Literals can be tricky, so the easiest is to rememeber to suffix anything that isn't an int with 'd', 'f' or 'L', depending on the type you want ('1.0d' is actually redundant, since '1.0' is a double, but it's not illegal, and I usually add it anyway just to remind myself).

Another Tip: Use 'L' for long literals, because 'l' - also legal - is often difficult to distinguish from '1').

HIH

Winston
 
Douglas Knapp
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm only fifty-eight pages in and haven't covered literals yet. I read the bit about biggest and smallest values and had to write a loop that counted from the lowest to the highest. You know, I haven't sat all the way through it. 9 quadrillion is a big number.

I also rewrote it using MIN_VALUE and MAX_VALUE. Much easier.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you want to iterate all int values. Huh.
You might want to read this ;)
 
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Counting from lowest to highest could be interesting, too - what kind of data type can hold such a number? Something to consider, if you're really counting. Then again, if you're really counting all those numbers, you'll be waiting a while anyway...
 
Douglas Knapp
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:So, you want to iterate all int values. Huh.
You might want to read this ;)


That was kind of a fun read.

I let it run for over an hour and it never even reached the trillions.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought you would reach 10¹² within an hour.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would have thought you would reach 10¹² within an hour.

But if you start from Long.MIN_VALUE...
 
Douglas Knapp
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:But if you start from Long.MIN_VALUE...


Which is exactly what I did.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!