Data Types in Java

Hello everyone! Today I wanted to explain the concept of the various data types in Java.

To start with, you need to understand that there are two kinds of data types; primitive and non-primitive. The main difference between the two is that non-primitive types are predefined by Java for us; it’s the main reason we are focusing on the primitive types, as we’ll be defining and creating them! The other important thing to remember about the primitive data types are they’re the more ‘traditional’ idea behind the term “data type”. We use these to store and manipulate data and they are pretty basic in nature.

There are eight primitive data types offered in Java that I like to think of as belonging to three categories. Remember as we’re going through the examples that you must declare a data type before you use it with it’s type and name!

First we have the numeric data types which are differentiated from by the size of the data being stored. A byte is best to store a very small number, anywhere between -128 to 127, or 8-bits.

byte myByte = 50;

A short is best to store a larger number, anywhere between -32,768 to 32,767, or 16-bits.

short myShort = 22445;

An int is the most common numeric data type you’ll see and use. It is equivelent to 32-bits, so it can store a number between negative two-billion and two billion.

int myInt = 500000;

A long is the last and largest numeric data type. Coincidentally, it goes to a 64-bit limit and it’s very likely you’ll never need anything more for your number-storing needs. This is because it can store anywhere between negative nine-quintillion and nine-quintillion. The long data type does not mess around!

//NOTE: When defining a long, you must enter an L at the end
long myLong = 873499849546656L;

The next category concerns fractional numbers. Once again, the major difference between these two data types is the number of decimal digits you need to store.

First, we have a float which can only handle between six or seven digits. Float also has to be a special flower like long in which you need to add a letter at the end in order for it to act correctly.

//NOTE: add the f at the end!
float myFloat = 86.75309f;

Next up is the more common fractional number data type, you might as well call it int’s brother from another mother; the double! Doubles can handle up to fifteen decimal digits, making it much more suitable for precise calculations.

double myDecimal = 65.545454545454;

I’ll be honest with you; the last category is more of a hodgepodge, they don’t have a whole lot in common with each other.

First we have boolean, which can store two different values; either true or false. It’s useful for checking other data types against each other and performing if/else statements, etc.

boolean checkSize = false;
int first = 5;
int second = 9;
        if (second < first) {
        } else {
            System.out.println(checkSize);
        }

Next we have the char data type, useful for storing a single character.

char myCharacter = 'y';

So….We’re done now right? Eight data types, I gave you eight examples, you should go home now right? WRONG! Surprise, we’ve got one left to talk about! The String data type. It’s what’s normally referred to as the ‘ninth’ data type, but it’s special in the fact that it’s non-primitive. Java defines string for us, we just need to declare it. A String can hold a sequence of characters such as movie titles or commands.

String myString = "Back to the Future";

Okay, now you can start gathering your stuff and getting out of here. I gave you a quick crash course in Java data types and hopefully it was interesting for you! If you wish to see more of the code snippets that you saw here please go to my git post at: https://github.com/salkiduckyJUMP/RLHull-Repo/blob/master/JavaDataTypes.java

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.