Last Updated by
As I begin to edit my Java Basics series from a couple years ago, I thought it would make sense to start pulling some really detailed information out into their own posts. That way, the beginner content is much easier to digest. To start, I’m pulling out all the information I have on the 8 primitive types in Java.
Table of Contents
Java Primitive Types
As the title of this article suggests, Java has 8 primitive types built into the language. In the following subsections, we’ll take a look at all eight primitive types in no particular order. For each primitive type, we’ll describe how to use it and what its limits are.
The Int Primitive Type
To start, Java integers are 32-bit signed (meaning positive or negative) values, and they are denoted by the
int someNumber = 10;
Of course, like all primitive types, integers have their limits. Since they’re 32-bit, we’re looking at a range of values from -2,147,483,648 to 2,147,483,647. That’s a lot of numbers! Of course, we can confirm that range using a handy trick in DrJava’s interactions pane:
Integer.MAX_VALUE // Prints 2,147,483,647 Integer.MIN_VALUE // Prints -2,147,483,648
int is probably the most common integer type used for simple calculations. If you need more range, see
The Double Primitive Type
int, Java doubles are 64-bit floating point values, and they are marked by the
double someNumber = 110.55;
As a reminder, floating point numbers are just real numbers. In other words, we gain access to decimal points when we use doubles.
Since doubles are 64-bit, we have access to significantly more numbers than with integers. If we use the same interactions pane trick, we can find out just how massive the range of potential values is:
Double.MAX_VALUE // Prints 1.7976931348623157E308 Double.MIN_VALUE // Prints 4.9E-324
Keep in mind that negative exponents imply extremely small numbers—as opposed to extremely negative numbers—so we’re not exactly looking at the same kind of range as with integers.
double is the default choice for floating point values in Java. The alternative is
The Char Primitive Type
As we’ve seen already, Java chars represent 16-bit characters, and they are marked by the
char someCharacter = 'f';
All characters in Java are defined using single quotes. Meanwhile, double quotes are used to define strings—something we’ll chat about later.
As usual, we can find out the character range using the following code snippet:
Character.MAX_VALUE // Prints '???' Character.MIN_VALUE // Prints ''
To make sense of this range, we can always cast the results to an integer (more on that later):
(int) Character.MAX_VALUE // Prints 65535 (int) Character.MIN_VALUE // Prints 0
As it turns out, the
char type is the only unsigned type in Java. In other words, a character can range from 0 to 65,535 where each number maps to a specific character. In order to create characters beyond this list, Java may combine pairs of characters. Check out Reverse a String in Java for some examples.
The Byte Primitive Type
When we were chatting about binary, we talked about the concept of a bit. Well, a group of eight bits makes a byte which Java supports as a primitive type. The
byte type is essentially just an 8-bit number which ranges from -128 to 127. As expected, bytes are denoted by the
byte someByte = 20;
As always, we can confirm the range of a the
byte type using the following code snippet:
Byte.MAX_VALUE // Prints 127 Byte.MIN_VALUE // Prints -128
In my experience, the
byte type is useful for reading and processing raw data. In general, however, we will likely have no use for it since the range of potential values is so small.
The Short Primitive Type
short type is another integer type, but it takes up less space than the int type. In fact, it takes up exactly half the space at 16 bits, and it is marked by the
short someNumber = 11;
In terms of range, the
short type clocks in at just half of the bits as an integer, but we can confirm that as usual:
Short.MAX_VALUE // Prints 32767 Short.MIN_VALUE // Prints -32768
For practical purposes, a
short only has 65,546 possible values. Both
short are typically used when memory and disk space is low. Otherwise, it is safe to use
int as it is the default type when declaring integers.
The Long Primitive Type
On the other end of the spectrum is the
long primitive type. This type represents extremely large integers where we might want values even greater than the
int type can provide. The
long type is a 64-bit signed integer which means the values range into the quintillions.
Naturally, we denote longs with the
long someBigNumber = 1013401346173L;
To demonstrate just how wide of a range a 64-bit value can have, let’s take a look at the following code snippet:
Long.MAX_VALUE // Prints 9,223,372,036,854,775,807 Long.MIN_VALUE // Prints -9,223,372,036,854,775,808
long would be useful for computing the distance light travels in a given time frame. After one second, light travels roughly 300 million meters. If we wrote a program to track the distance of light in real time, the int type would cap out after around 7 seconds while a long could calculate out to around 975 years. Don’t believe me? Check out this Gist which runs through the entire calculation.
The Float Primitive Type
While we often use the
double which is a 64-bit floating point number type, Java supports another floating point number type called the
int, however, Java defaults to
double for floating point values. At any rate, we can denote a 32-bit floating point number using the
float someNumber = 11.4f;
To get an idea of the range of a
float, let’s use our trick again:
Float.MAX_VALUE // Prints 3.4028235E38 Float.MIN_VALUE // Prints 1.4E-45
As we can see, 32 bits really reduces our range and precision. If we want to use a decimal value with less precision than
double while using half the space, the
float type is an option.
The Boolean Primitive Type
Finally, we can cover the
boolean type. To declare a
boolean, we can use the
boolean isBool = true;
Booleans are a bit unique because they don’t represent numeric values like all the other primitive types. In fact, our little
MIN_VALUE trick won’t work here. Instead, booleans represent
false which we might recall from the previous lesson on logic.
We won’t spend much time on this now because it will become part of almost everything we do in Java. That said, we often won’t declare them explicitly like this. Instead, they are the result of comparisons which drive logic in code. Check out the boolean operators section below for more information.
Want to Learn More?
As mentioned already, this information could be previously found in the Primitive Types and Variables in Java article. Of course, now that article is much easier to read.
If you liked this article and you want more, become a member of The Renegade Coder. Membership is an excellent way to show your support, and it goes a long way to ensuring content like this stays free to the public.
At any rate, thanks again for your support. See you next time!