How can I get a long number bigger than Long.MAX_VALUE?
I want this method to return true :
4 Answers 4
That method can’t return true . That’s the point of Long.MAX_VALUE . It would be really confusing if its name were. false. Then it should be just called Long.SOME_FAIRLY_LARGE_VALUE and have literally zero reasonable uses. Just use Android’s isUserAGoat , or you may roll your own function that always returns false .
Note that a long in memory takes a fixed number of bytes. From Oracle:
long: The long data type is a 64-bit signed two’s complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.
We usually use integers when we want to calculate whole numbers. But Integers in Java can only hold 32 bit values which could be insufficient for some tasks. When we need bigger range of values, we could use long values. In Java, Long values is represented in 64 bits. However, even if this can hold bigger range of values, there is still a limit to it’s maximum value. The Java Long Max Value is 9,223,372,036,854,775,807. Below are some more details to explain this.
Explanation for Long Max Value
The primitive long and it’s wrapper class Long is represented by 64 bits in Java, and so the upper bound is limited by this. Since Long values can hold both negative and positive values, around 63 bits represents the negative number spectrum, while another 63 bits holds the value range of 0 and positive values. So since 0 is included in the other side of the spectrum, the maximum value Long can hold is 2 raised to the power of 63 minus 1. We need to do the minus one to account for the number 0. While the lowest negative value is minus 2 raised to the power of 63.
So 2 raised to the power of 63 is 9,223,372,036,854,775,808 which is an even number because 2 is an even number. And deducting one will bring the value 9,223,372,036,854,775,807 – which is the maximum value for long values.
Software construction, the web, and other techs
Why is this not Javadoc’d clearly?
10 thoughts on “ What are Java’s min and max values? ”
They are documented quite clearly via the JavaDoc for Integer.MAX_VALUE etc. Look where it says “See also: Constant Field Values”
The values are clearly displayed there, e.g.
- Gary Gregory Post author July 11, 2015 at 20:48
Yes, that’s true, the information is there, buried.
The implementation of the documentation for constant field values feels like an added-on after thought.
I have a hard time imagining someone (or people in a meeting) saying: Ok, let’s make this information available, it is indeed useful. But, let’s make them jump through a hyperlink to get there because… what?
What is worse in this specific case is that the Javadoc for Long.MAX_VALUE does give a value (2^63 – 1) Why one and not the other?
It’s Math and CS-like to write in powers of 2, yes, but a pain for writing documentation for normal humans.
Interesting arguments, and there are some fun-to-know facts about the Integer.MAX_VALUE, like How many characters can a Java String have?
How can you declare a really big matrix in Java? Like 3B x 3B entries? Java does not let you define arrays with long, just with int, and int is not enough… any suggestions?
The trouble is that there is so much information that it is all, in effect, “buried.” That’s an unfortunate result of such a rich and powerful language.
Suggestions for a 2D array with 3B * 3B entries? Well, other than coupling every computer in the world together to make a single virtual machine to run your JVM, I would recommend using a database to store that (MY SQL or SQL Lite).
As presented here, these values are incorrect. For example, “9223372036854775807” is an integer literal, not a long literal. As an integer, it’s much too large. You mean “9223372036854775807L”, which is a long literal and will correctly compile.
Have a query i came to know that for int unsigned min val is 0 & max is how much.How do we come to know what are signed & unsigned how can we get maximum & minimum values for all data types in java.Could any one tell me
Sun, on their “infinite wisdom” have decided that there are no unsigned types in java (although the JVM implementation would be the same).
As a result, it’s a total pain to implement anything that reads/processes/writes binary records.