Data Types in Java
In Java, every variable is associated with a specific data type. Each type determines the
amount of memory used and the operations permitted. Data types define how variables are
stored and used in memory.
Java data types are divided into two main categories: Primitive and Non-Primitive.
Primitive Data Types
• byte: 8-bit signed integer.
• short: 16-bit signed integer.
• int: 32-bit signed integer.
• long: 64-bit signed integer.
• float: 32-bit floating point.
• double: 64-bit floating point.
• char: 16-bit Unicode character.
• boolean: Represents true or false.
byte Data Type
The byte type is an 8-bit signed integer (-128 to 127). Useful for saving memory, especially
in large numeric arrays.
byte byteVar;
short Data Type
The short type is a 16-bit signed integer (-32,768 to 32,767). Used when memory efficiency
matters more than range.
short shortVar;
int Data Type
The int type is a 32-bit signed integer (~-2 billion to 2 billion). Commonly used for whole
numbers.
int intVar;
long Data Type
The long type is a 64-bit signed integer. Ideal for larger numerical values exceeding int’s
range.
long longVar;
float Data Type
The float type stores 32-bit decimal values with limited precision.
float floatVar;
double Data Type
The double type stores 64-bit decimal values with high precision.
double doubleVar;
char Data Type
The char type stores a single 16-bit Unicode character.
char charVar;
boolean Data Type
The boolean type stores true or false values.
boolean booleanVar;
Non-Primitive Data Types
Non-primitive types (or reference types) are created by the programmer and can store
multiple values or behaviors. Examples include Strings, Arrays, Classes, Interfaces, and
Objects.
String
Represents a sequence of characters. Strings in Java are objects and do not require a null
terminator.
<String_Type> <string_variable> = "<sequence_of_string>";
Arrays
A collection of variables of the same type stored together.
int[] numbers = {1, 2, 3, 4, 5};
Class
A class defines the blueprint of an object, specifying attributes and methods. It uses access
and non-access modifiers to control visibility and usage.
Interfaces
An interface defines a set of abstract methods that classes must implement. It specifies what
a class should do.
Objects
Objects are instances of classes. They have state (fields), behavior (methods), and identity
(unique reference).
Summary
• Primitive types store simple values directly in memory.
• Non-primitive types store references to data objects.
• Use primitive types for simple data and non-primitive types for complex structures.
• Java is strongly typed — each variable must have a declared type.