Data Types In Java
...a data type just is a storage space in memory for holding certain values.
Vocabulary Check ๐
Something compiles when it is reduced or converted into a language (0's and 1's) the machine can understand.
A bit is the smallest unit of data. It is a binary digit of either 0 or 1.
Data type precision is the amount of digits in a number.
Data type scale is the amount of digits to the right of the decimal point.
Two's complement is the computer's representation of numbers using binary digits.
Imagine you have a box and the box looks something like this:

As you can see by the label on the front, this box contains strings. And each string has a significant color or "value". But they are all a type of String. See where I'm going with this?
In Java a "String" instead is a data type that refers to anything read as text by the compiler. Now, the picture above is to provide you an idea of how a data type just is a storage space in memory for holding certain values.
There are two kinds of data types. Primitive data types and non-primitive data types. Primitive data types are predefined by the language and compile quickly into binary code, while non-primitive data types are defined by the programmer and take longer to compile because they are also objects.
The primitive data types are:
*categorized by bit size
- byte | 8-bit
- short | 16-bit
- int | 32-bit
- long | 64-bit
- float | 32-bit IEEE 754 floating point
- double | 64-bit IEEE 754 floating point
- boolean | not precise
- char | 16-bit Unicode characters
If you are wondering what IEEE 754 stands for, it means the Institute of Electrical and Electronics Engineers Standard for Floating-Point Arithmetic which was established in 1985 by the Institute of Electrical and Electronics Engineers. Okay, so what does that mean? What is a floating point? A floating point is essentially a decimal point with no set position, as opposed to a fixed point number which only allows a certain number of digits on either side of the decimal point. If you are still confused, this discussion on reddit may help clear things up: Why is a floating point called a floating point?
The non-primitive data types are:
Non-primitives refer to objects, hence they are also named reference types.
Primitives
byte
A byte represents an 8-bit signed two's complement integer, meaning it holds a value range of signed numbers from -128 to 127. Because byte is 4 times smaller than an int, it saves a lot of space in memory and is good to use when needing to save memory space inside of an array.
Default value: 0
Syntax: byte a = 10, byte b = -20
short
A short is only 2 times smaller than an int. It represents a 16-bit two's complement integer, and its range lies between -32,768 to 32,767.
Default value: 0
Syntax: short s = 10000, short r = -5000
int
An int is the most commonly used data type when representing whole numbers. t Equal to 2 bytes, it ranges from - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1).
Default value: 0
Syntax: int a = 100000, int b = -200000
long
A long ranges between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1).
Default value: 0L
Syntax: long a = 100000L, long b = -200000L
float
A float is a single-precision 32-bit IEEE 754 floating point. Its value range is unlimited. If you need to save memory in large arrays of floating point numbers, it is recommended to use a float (instead of double). You should never use float for precise values, such as currency.
Default value: 0.0f
Syntax: float f = 234.5f
double
A double is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. Just like float it can be used for decimal values. You should never use double for precise values, such as currency.
Default value: 0.0d
Syntax: double d = 12.3 or 12.3d
Remember: When representing a float data type in Java, you must append the letter f to the end of the data type or it will be stored as double. For double, however, the appended d is not necessary.
Hold Up! โ๐ค
If both float and double have unlimited range, how is double bigger in size?
In Java, the size of a data type refers to the amount of memory required to store a value of that type. While both float
and double
have unlimited range in terms of representing a wide range of numeric values, they differ in precision and the amount of memory they occupy.
In Java, a float
is a 32-bit data type, while a double
is a 64-bit data type. The float
data type uses 4 bytes (32 bits) of memory, and the double
data type uses 8 bytes (64 bits) of memory. This means that a double
requires twice the amount of memory compared to a float
.
The difference in size between float
and double
is directly related to the precision of the values they can represent. A float
can store values with a precision of approximately 7 decimal digits, while a double
can store values with a precision of approximately 15 decimal digits. The larger size of double
allows for a greater range of values with higher precision compared to float
.
Boolean
Boolean is a data type that is defined by only two possible valuesโ true/false. It is used in conditional statements.
Syntax: Boolean isHappy = true
Boolean with a capital 'B' is the wrapper class for the boolean primitive type. If you are wondering which to use, perhaps this thread on StackOverflow will help: Boolean vs boolean.
char
A char represents a single 16-bit Unicode (not ASCII) character. Its value-range lies between '\u0000' (0) to '\uffff' (65,535).
Default value: '\u0000'
Syntax: char letterA = 'A'
Each primitive type has a wrapper class from which they derive:
Byte: wrapper class of byte
Short: wrapper class of short
Integer: wrapper class of int
Long: wrapper class of long
Float: wrapper class of float
Double: wrapper class of double
Boolean: wrapper class of boolean
Character: wrapper class of char
Non-Primitives
class
Class with a capital 'C' is a wrapper class for the class data type which is a reference type for the class object.
Syntax: class Game{}
String
String is the reference type for a message in Java. It is read as text.
Syntax: String simpleString = "Hello"
Array
Array is the reference type for an array of values in Java represented by the bracket ([]) symbols. The type of array being used is defined by the name of the data type preceding it. For instance, you can have an int[], String[], interface[], etc.
Syntax: int[] myIntArray = new int[3]
interface
The interface data type is a reference type for an interface in Java.
Syntax: interface Category{}

Data Types in Java are essentially how we sort our data. "What type does each value belong to?" Some are also objects, these are called non-primitives. And some consist of a definite byte size, these are called primitives.
Non-primtives types are sometimes called reference types because they refer to a type of object. Any object that you instantiate will have a reference type defined by you. But some of the common types include Class, String, Array, and Interface.