Normally Java int has range -2147483648...2147483647, so the max value is 2147483647, I don't want to use negative number. an int can have more than 4 billions of different values, can I use int range 2-4 billions? in eclipse, I use int a = 31474836471; I get compile error. how do I do?
-
1Use a long type variable. – Gilbert Le Blanc Mar 14 '13 at 16:52
-
1[Java doesn't support unsigned ints](http://stackoverflow.com/q/430346/851811). Use `long` instead. – Xavi López Mar 14 '13 at 16:52
-
You could always store the wanted value **-2147483648** and add it again when interpreting the value. Disadvantage: arithmetic operations will no longer be valid. – MrSmith42 Mar 14 '13 at 16:54
3 Answers
You've already asked this Question as part of another one.
The Answer is no, you cannot make the Java int
type so that you have 4 billion values >= zero.
And asking the same Question again won't change the Answer.
You need to change your program to replace the "pk" type from int
to long
. You have no other alternatives, given you have stated that keys must be >= zero.

- 698,415
- 94
- 811
- 1,216
No, in Java, all numbers are signed; there are no unsigned numbers. If you need a higher range of integers, then use long
.

- 176,041
- 30
- 275
- 357
-
-
1Apparently, you can't use `switch` on a `long`, but that wasn't part of your original question. Why would you need to switch on a long? Do you really have billions of cases? – rgettman Mar 14 '13 at 16:56
-
-
-
-
-
@Dave Newton I asked because I was too lazy to create a new project just to test it out. But that's what I ended up doing anyways and as I expected, you can't switch a long (which is weird since with the new java you can switch a string...) – souldzin Mar 14 '13 at 17:21
-
@SoulDZIN you wouldn't need a new project, just a long, and a file with a main method. Should take all of a couple of minutes. – Dave Newton Mar 14 '13 at 17:31
-
Just realized that this question wasn't asking about int and 'switch' statements, I misread the title. Sorry about that! – souldzin Mar 14 '13 at 17:44
You can't. The negative range is available for int
s because the value is signed. Only so much information can be represented in the bits.
See this other question for some good differentiation between signed and unsigned numbers: Signed versus Unsigned Integers