Data Types
A data type is a label that defines the operations that a value can perform, as well as the operations that can be applied to that value. For example, if two pieces of data are labeled as text and added together, then the compiler will know to combine the two pieces of text into one piece of text. However, if those two pieces of data are labeled as numbers and added together, then the compiler will know to calculate the total of the numbers.
These values are referenced through user defined variables, and can be in one of two states. A declared variable has reserved system memory for the future value, but does not reference an actual value.
String name; // does not assign a value to name; reserves space in memoryAn instantiated variable holds an actual value.
String name = "Jim";Java recognizes two categories of data types; primitive, and complex.
Primitive Data Types
Primitive data types are predefined by the language, and are used to represent ‘simple’ data types such as numbers, characters, etc. The value possesses only state; a value that is of a primitive type cannot perform any operations, but operations can still be applied to the value.
As mentioned previously the value is stored directly in the variable. This location in computer memory is known as the ‘stack’, which is relatively close to the running application for quick access.
Examples of primitive type variable instantiations include:
int age = 22;
char grade = 'A';
double cost = 20.00
boolean isQualified = true;Complex Data Types
Complex data types are predefined by the language, by libraries and can be defined by developers. They posses behavior in addition to state.
Complex values are stored in a memory location known as the ‘heap’, which lives further from the running application than the stack does. In order to bridge this gap, a reference to the value’s location in the heap is stored within the stack. This reference is known as a ‘pointer’. This can be confirmed by printing the location in memory of objects, which is what Java does by default.
Object firstObject = new Object();
Object copyOfFirstObject = firstObject;
Object secondObject = new Object();
System.out.println(firstObject);
System.out.println(copyOfFirstObject); # prints the same thing as firstObject
System.out.println(secondObject); #does not print the same thing as firstObject, even though both are blankThe String variable is a complex type, used to represent strings of characters (simple text).
String firstName = "Bob";
String lastName = "Doe";
String fullName = firstName.concat(" ").concat(lastName); // firstName is performing an operationIntegral Types
Integral types refer to data types used to store integer values. These data types represent whole numbers, without any fractional or decimal components.
- byte is a sequence of eight bits that can have a value between -128 and 127
- short is a sequence of sixteen bits (2 bytes) with a range between -32,768 and 32,767
- int is a sequence of thirty two bits (4 bytes) with a range between -2,100,000,000 and 2,100,000,000
- long is a sequence of sixty four bits (8 bytes) with a range between -9.2 x ¹⁰¹⁸ and 9.2 x ¹⁰¹⁸
Floating Point Types
Get Dennis Kalaygian’s stories in your inbox
Join Medium for free to get updates from this writer.
Floating-point types are used to represent numbers with a fractional part, including real numbers such as decimal values and scientific notation.
- float is a sequence of thirty two bits (4 bytes) with a range between 1.4 x 10^-45 and 3.4 x 1⁰³⁸
- double is a sequence of sixty four bits (8 bytes) with a range between 4.9 x 10^-324 and 1.8 x 1⁰³⁰⁸
Both the float and the double round the last digit after the decimal point, if the value contains too many digits. A double can store a decimal value that is twice the size (not value) of a float.
The reason that these types have such a large range because of the automatic scientific notation conversion. If the absolute value (not size) is too large, the representation will automatically switch to scientific notation.
The boolean Primitive
The boolean is used to indicate a binary value, and can be only true or false.
boolean isEqual = (1 == 1); // returns trueThe char Primitive
The char primitive is used almost exclusively to represent Unicode characters such as a letter, digit, symbol, etc.
char newline = '\n';Narrowing, Widening and Casting
Narrowing is when a number is converted from a larger data type to a smaller data type.
long theLong = 5L;
int theInt = (int) theLong;If a number is too large then narrowing will result in a completely different number, in a phenomenon called overflow.
Widening is when a number is converted from a smaller data type to a larger data type.
int theInt = 5;
long theLong = (long) theInt;Casting is the process of converting on data type to another.
Underscores in Numeric Literals
Underscores can be used almost anywhere within a number; however, they cannot be used at the beginning or the end, nor can they be used immediately before or after a decimal point.
Scientific Notation
In Java, scientific notation can only be used with float and double. A float can have eight digits, a double can have sixteen; any larger numbers will result in an automatic conversion to scientific notation and a loss in precision.
double number = 5.88e12;
float anotherNumber = 5.0e-8f;





