Primitive types

Updated: 3 years ago

Check out other lessons in Learning the Java syntax

We've already learned how to use the int type for declaring variables to hold numbers. We're now going to cover all of the primitive types that you can use in your Java applications.

You're probably wondering what the heck are primitive types. They are the most basic type of values. They run super fast and are optimized to take as little space in memory as possible. They are the basic building blocks and you'll use them pretty much everywhere.

Java has 8 primitive types and an extra one that's not actually a primitive, but because it's so widely used we give it an honorary status of a primitive.

// byte, used for whole numbers between -128 and 127
byte numberOfFingers = 10;

// short, used for whole numbers between -32768 and 32767
short heightOfMountEverestInFeet = 29028;

// int, used for whole numbers between -2^31 and 2^32
int numberOfBooksInClosestLibrary = 165745215;

// long, used for whole numbers between -2^63 and 2^63
// note the L keyword
long currentTimeInMilliSeconds = 1444762010361L;

// float, used for decimal numbers
// note the F keyword
float rangeInMiles = 223.423F;

// double, used for bigger decimal numbers
float moneyBalance = 0.00000001;

// boolean, used for either true or false
boolean hasLearnedPrimitiveTypes = true;

// char, characters and such
// note the single quotes
char firstLetter = 'a';

// honorary primitive'
// String, texts and such
// note the double quotes and an uppercase String
String bestPlaceToLearnJava = "";

You may be wondering what's with the L keyword at long and the F keyword at floats. Well, that's because you have to tell the compiler what type of number you actually want. For whole numbers, it will default to int and you have to force it to use long. While for decimal numbers, it defaults to double and you have to force it to use floats.


Strings are not actually a primitive as they are a class (that's why String is capitalized). We use strings all the time so the creators of Java put a lot of effort in strings, so they are optimized and run real fast. Strings are created with the double quoutes ("") and you can even combine two strings together with the + operator.

Default values

You can declare a variable and not assign a value to it. The compiler will then use a default value for that type. For numbers that's 0, for boolean is false, and for Strings and other classes it's null. null is a special keyword that tells us, that the value is actually missing (it has no value).

What type to use?

Choosing the right type comes with practice. Choosing boolean is easy, because they are either true or false. On the other hand, choosing the right number type is a bit harder. For whole numbers, we either use int or long, because byte and short are way too small. You should start with int and when necessary use long.

When it comes to decimal numbers, you start with float, and when that's not precise enough, you bump it up to double.

« Previous lesson:
» Next lesson: