There are two different data types in Java. Reference and Primitives.
Primitive Types
There are eight primitive types: Before we defined the size of these primitives, let’s first define what a bit is. A bit stands for binary digit. A bit is the smallest unit of data in a computer. Its value can either be a 1 or 0. (True, False)
Primitive Data Type | Number of Bits |
byte | 8 (Integral Value) |
char | 16 (Unicode Value) |
short | 16 (Integral Value) |
int | 32 (Integral Value) |
long | 64 (Integral Value) |
float | 32 (Floating-Point Value) |
double | 64 (Floating-Point Value) |
boolean | True or False |
Here are some examples of declaring and initializing all of these types.
byte b = 1;
char c = 'c';
short s = 1;
int n = 100;
long l = 200L;
float f = 100.1f;
double d = 100.1;
boolean flag = true;
Reference Types
A reference type refers to an instance of a class. If you remember from my previous post that a class instance is called an object. A reference type points to the location in memory where the objects lives. The reference variable stores the memory address where the objects is located.
Will use the String object as an example:
String name = "Mike";
// name is the reference object and can only point to a String object.