Java categorizes data types into two distinct groups: primitive types and reference types. Primitives store the actual data values, where as reference types store memory addresses pointing to objects.
Primitive Data Types
There are eight built-in primitive types in Java, designed to be efficient and hold simple values.
Integral Types
These types represent whole numbers without fractional components.
- byte: An 8-bit signed integer. Its range is limited to -128 through 127, making it ideal for saving memory in large arrays.
- short: A 16-bit signed integer. It accommodates values ranging from -32,768 to 32,767.
- int: The default 32-bit signed integer type for whole numbers. It supports values from -2^31 to 2^31-1 (-2,147,483,648 to 2,147,483,647).
- long: A 64-bit signed integer used when the
intrange is insufficient. It spans from -2^63 to 2^63-1. Literals must be suffixed withLorl(e.g.,100000L).
Floating-Point Types
These types represent numbers with decimal precision.
- float: A single-precision 32-bit floating-point value. It requires the
Forfsuffix. - double: A double-precision 64-bit floating-point value. This is the default type for decimal numbers and offers greater precision than
float.
Text and Logical Types
- char: A single 16-bit Unicode character. It is capable of storing any international character.
- boolean: Represants a logical entity with only two possible states:
trueorfalse.
Default Values and Wrappers
When fields are declared but not initialized, they adopt default values: 0 for numerics, 0.0 for floats, \u0000 for char, and false for boolean. Java also provides wrapper classes—such as Integer, Double, and Character—to convert these primitives into objects, enabling usage in collections and generics.
// Example of primitive variable declarations
byte smallVal = 100;
short mediumVal = 5000;
int defaultVal = 25000;
long hugeVal = 9999999999L;
float singlePrec = 4.5f;
double doublePrec = 12.99;
char initial = 'J';
boolean isActive = false;
Reference Data Types
Reference types do not hold the data itself but rather a reference (memory address) to the data on the heap. This category encompasses more complex structures.
- Classes: Defined using the
classkeyword, classes act as blueprints for objects. They encapsulate state (fields) and behavior (methods). - Interfaces: Defined with the
interfacekeyword, interfaces specify a contract of methods that implementing classes must fulfill. - Arrays: Container objects that hold a fixed number of values of a single type. Arrays can be single-dimensional or multi-dimensional.
- Enumerations: Specialized classes defined via
enum, primarily used to define collections of constants. - Annotations: Metadata markers defined by
@interface. They provide data about the program but do not directly affect code execution. - Strings: Though conceptually a sequence of characters, the
Stringclass is a reference type. Strings are immutable, meaning their values cannot be altered after creation. - Wrapper Classes: As mentioned, object versions of primitives like
LongorBoolean. - Collections: Part of the Java Collections Framework, these include
List,Set, andMapinterfaces for dynamically storing and manipulating groups of objects.
Reference types are managed by the Java Garbage Collector, wich automatically reclaims memory used by objects that are no longer reachable. Furthermore, they support inheritance and interface implementation, forming the backbone of Java's object-oriented paradigm.
// Defining a Class
class DataObject {
int id;
public void process() { }
}
// Defining an Interface
interface Processor {
void execute();
}
// Array initialization
int[] numberList = new int[20];
// Enum definition
enum Status {
PENDING, ACTIVE, CLOSED
}
// Annotation definition
@interface MetaInfo {
String version();
}
// String usage
String textData = "Sample Text";
// Wrapper class usage
Integer count = 55;
// Collection framework usage
java.util.List<String> names = new java.util.ArrayList<>();
java.util.Map<String, Integer> scores = new java.util.HashMap<>();