DEV Community

Cover image for The 8 Primitive Types in Java

The 8 Primitive Types in Java

renegadecoder94 profile image Jeremy Grifski Originally published at on ・5 min read

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.

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 keyword:

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

Naturally, int is probably the most common integer type used for simple calculations. If you need more range, see long below.

The Double Primitive Type

Unlike int, Java doubles are 64-bit floating point values, and they are marked by the double keyword:

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.

In general, double is the default choice for floating point values in Java. The alternative is float.

The Char Primitive Type

As we’ve seen already, Java chars represent 16-bit characters, and they are marked by the char keyword:

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 keyword:

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

The 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 keyword:

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 byte and 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 keyword:

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

Perhaps a 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 float. Like 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 keyword:

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 keyword:

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 MAX_VALUE and MIN_VALUE trick won’t work here. Instead, booleans represent true or 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!

The post The 8 Primitive Types in Java appeared first on The Renegade Coder.


Editor guide
orenovadia profile image

Question: if I am running on a 64bit system. Does an integer take less space than a long in the java process?
If not, why shouldn't I just use long instead of int all the time?

renegadecoder94 profile image
Jeremy Grifski Author

Great question! Honestly, this is a bit outside of my systems knowledge, but I would assume that each primitive type would only take up as much space as it needs (i.e. 32 bits for an integer). That said, I'll defer to the systems experts on this one.

Here's a nice discussion that might better address your question.

In addition, I think we can take this question a step further and ask: "Why not always use something like BigInteger?"

orenovadia profile image

Thanks Jeremy.

I have done a little research on the matter.
First of all, even in 64bit systems, pointer addresses are of byte granularity (this is why you can get a specific char from a *char array in C). Because of this, it is possible to directly reference variables of less than 64 bit length. So using int instead of long actually saves memory.

In regards to runtime and performance: Arithmetic operations on 32 and 64 bit integers (on a 64 bit cpu) are done on the hardware (ALU). The time difference should not be significant, still, it is possible that adding two 32bit integers will be faster than longs because of some hardware optimizations.

In regards to BigInteger: I have to disagree on this one. BigInteger is an abstraction implemented on the software level. It must be represented by something like an array of integers, and therefore, arithmetic operations on BigInteger are not O(1) time and space complexity (probably logarithmic - because adding two BigIntegers requires you to compute each integer in an array of logarithmic size of N).

Thread Thread
renegadecoder94 profile image
Jeremy Grifski Author

Thanks for the follow up! I think a lot of what you shared followed my intuition—although I didn't know the details. Thanks again.

In terms of the BigInteger topic, I totally agree. I was trying to pose the question "why not always go bigger?", and you nailed the response. I figured the answer to that question would be similar to your original question.

harshrathod50 profile image
Harsh Rathod

Good. You wrote so much vital information.

thorstenhirsch profile image
Thorsten Hirsch no particular order

Let me order them for you. :-)

byte, char, short, int, long
float, double

ankushabhishek3 profile image

Awesome explanation...