Java Data Types
This article aims to make understanding Java data types easier by evaluating them and providing examples.
Java Data Types
In Java, a variable must be a certain data type, as was discussed in the previous chapter:
Example: 
Example: 
There are two categories of data types when discussing Java data types:
- Primitive data types – includes byte, short, int, long, float, double, boolean and char
- Non-primitive data types – such as String, Arrays and Classes (Upcoming chapters will provide more information on these non-primitive data types.)
Primitive Data Types
Primitive data type only describes the size and kind of variable values. It lacks any extra functions.
Java supports the following eight primitive data types:
Data Type | Size | Overview |
---|---|---|
byte | 1 byte | Stores whole numbers having a range between -128 and 127. |
short | 2 bytes | Supports the entire range of whole numbers between -32,768 and 32,767. |
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647. |
long | 8 bytes | Contains the whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 |
float | 4 bytes | Fractional numbers are stored. Enough to hold six to seven decimal digits |
double | 8 bytes | Fractional numbers are stored. Enough to store 15 decimal digits |
boolean | 1 bit | Holds values for true or false. |
char | 2 bytes | A single character, letter, or ASCII values are stored. |
Numbers
As far as Java data types are concerned, primitive number types can be divided into two main categories:
Unlike decimal types, integer types store whole numbers (such as 786 or -987). There are four valid types: byte, short, integer, and long.
Depending on the numeric value, you should use the appropriate type.
A floating point value represents numbers containing fractional and decimal parts. The type of data is either float or double.
Java’s most commonly used numerical types are int (for whole numbers) and double (for floating point numbers). As you read on, we will describe them all.
Integer Types
Type Byte:
Bytes can store whole numbers between -128 and 127. If you are certain that the value will be between -128 and 127, a byte can be used instead of an int.
Example: 
Example: 
Type Int:
It is possible to store whole numbers between -2147483648 and 2147483647 using the int data type. When creating variables with numeric values, the int data type is the preferred data type.
Example: 
Example: 
Short Type:
Short data types can store whole numbers between -32768 and 32767:
Example: 
Example: 
Long Type:
Long data types can store whole numbers from -9223372036854775808 to 9223372036854775807. The value is stored here when int is not large enough to contain it. In Java data types, you should end the value with an “L”:
Example: 
Example: 
Floating Point Types
In Java data types, a floating point type best suits numbers with decimals, such as 9.99 or 3.14515.
Float:
In the float data type, fractional numbers can range from 3.4e−038 to 3.4e+038. The value should be terminated with an “f“:
Example: 
Example: 
Double:
Double data types can store fractional numbers from 1.7e−308 to 1.7e+308. In Java, you should end the value with a “d”:
Example: 
Example: 
Is it better to use floats or doubles?
Floating point precision refers to how many digits the value can have after the decimal point.
A float variable has a precision of only six or seven decimal digits, while double variables have a precision of about 15 digits.
Therefore, most calculations are safer when done with doubles.
Scientific Numbers:
Floating point numbers can also be scientific numbers indicating the power of 10 with an “e“:
Example: 
Example: 
Booleans
Booleans can only take true or false values and are declared with the boolean keyword:
Example: 
Example: 
The main use of boolean values is conditional testing, which you will discover in the following chapter.
Characters
A char data type stores a single character. When it comes to Java data types, the character must be surrounded by single quotes, such as ‘A’ or ‘c’:
Example: 
Example: 
You can also display certain characters using ASCII values:
Example: 
Example: 
Strings
The String data type is used to store a sequence of characters (text) in Java. In order to use string values, double quotes must be used:
Example: 
Example: 
Some refer to String as “the special ninth type” in Java because it is so widely used.
Strings are actually non-primitive Java data types, since they refer to objects.
There are methods on the String object that can be used to perform certain operations on strings.
You don’t have to worry if you don’t understand what “object” means.
In a later chapter, we will learn more about strings and objects.
Non-Primitive Data Types
Because they refer to objects, non-primitive data types are called reference types.
Primitive and non-primitive data types differ in the following ways:
- In Java, primitive types are predefined (already defined).
- The non-primitive types are created by the programmer and are not defined by Java (except for Strings).
- In contrast to primitive types, non-primitive types can call methods to perform certain operations.
- Primitive types always have a value, while non-primitive types can be null.
- Primitive types begin with a lowercase letter, while non-primitive types begin with an uppercase letter.
- A primitive type’s size depends on its data type, while a non-primitive type’s size is the same.
You now know in detail what are Java data types. Strings, Arrays, Classes, Interfaces, etc., are examples of non-primitive types. A later chapter will discuss these topics in more detail.