DEV Community

Cover image for The 8 Primitive Types in Java
Jeremy Grifski
Jeremy Grifski

Posted on • Originally published at therenegadecoder.com on

The 8 Primitive Types in Java

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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';
Enter fullscreen mode Exit fullscreen mode

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 ''
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (7)

Collapse
 
orenovadia profile image
orenovadia

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?

Collapse
 
renegadecoder94 profile image
Jeremy Grifski • Edited

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?"

Collapse
 
orenovadia profile image
orenovadia

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

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.

Collapse
 
harshrathod50 profile image
Harsh Rathod

Good. You wrote so much vital information.

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

...in no particular order

Let me order them for you. :-)

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

Collapse
 
ankushabhishek3 profile image
ABHISHEK KUMAR

Awesome explanation...