Java data types

Primitive data types

Java has what are called primitive data types. A primitive data type specifies the size and type of a variable. We will discuss variables in a different tutorial, but for now here is an example of int and boolean:

int x = 5;
boolean y = true;

In this example the word 'int' is an Integer data type. Primitive data types are predefined by keywords such as int, boolean, etc. Java is a statically typed language and requires you to specify the data type of a variable before you can use it. So in the small example above, you cannot simply say 'x = 5' until you have assigned it a data type. Java has 8 of these data types:

Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808
float 4 bytes Stores fractional numbers from 3.4e−038 to 3.4e+038. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers from 1.7e−308 to 1.7e+038. Sufficient for storing 15 decimal digits
boolean 1 byte Stores true or false values
char 2 bytes Stores a single character/letter

Which whole number type to use?

byte, short, int and long all represent whole number data types. The default / most commonly used is int. If you know the numbers you will be working with and know they fall within the range listed in the table above then you can use whichever one you prefer. Usually developers stick to int unless they know the whole number they will need is larger than int. If you are having issues with using too much memory (rare, but happens) you can opt for using byte or short. A byte is 4x smaller than an int, while a short is 2x smaller.

Which fractional number type to use?

float and double are used to represent fractional numbers. Like byte and short, if you need to conserve memory you should use float. double is generally used as the default, like int for whole numbers. Oracle recommends that these data types should never be used for precise values, such as currency. You should instead use the class BigDecimal (more on classes in a separate tutorial).

What is a boolean?

boolean is a data type that represents true of false and those are the only values this type can have. You will be using this data type for to check true/false conditions within if/else statements, while loops, etc.

Char, a single character/letter

The char data type is used to store a single character (16-bit Unicode character). A char value must be surrounded by single quotes, like 'h' or 'L'. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

What about String?!?

A string is a collection of characters and is actually not a primitive data type. Some people refer to it as the 9th data type because of how much it is used, but it is actually a class. Strings are special in that they are non-primitive, but are not programmer made. As such, you do not need to use the keyword 'new' when instantiating one. It also has methods you can call such as, toUpperCase(), toLowerCase(), length(), concat(). Below are some examples of these uses:

//Simple string
String greeting = "Hello";

int len = txt.length();  // The integer 'len' value will be the length of the variable 'txt'

//The line below will output: The length of the txt string is: 26
System.out.println("The length of the txt string is: " + len);

String hello = "Hello World";
System.out.println(hello.toUpperCase());   // Outputs "HELLO WORLD"
System.out.println(hello.toLowerCase());   // Outputs "hello world"

//To concatenate words together you can use the + sign like below
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);  // Outputs "John Doe"

//You can also concatenate using the method as well
String fName = "House ";
String lName = "Lantaff";
System.out.println(firstName.concat(lastName));  // Outputs "House Lantaff"