Variables & Data Types
Learn how Java stores information using variables and different data types.
What is a Variable?
A variable is a named memory location used to store a value that can be used by a Java program.
Every variable has a data type, a name, and a value.
int age = 21;
Variable Declaration
dataType variableName = value;
For example:
int age = 21;
double salary = 45000.50;
String name = "Samadhan";
boolean active = true;
Primitive Data Types
Java provides eight primitive data types. They represent simple values directly.
| Type | Typical Size | Example | Purpose |
|---|---|---|---|
byte
|
8-bit |
byte age = 25;
|
Small integer values. |
short
|
16-bit |
short count = 1000;
|
Small to medium integer values. |
int
|
32-bit |
int salary = 50000;
|
Common integer type. |
long
|
64-bit |
long population = 1400000000L;
|
Large integer values. |
float
|
32-bit |
float price = 10.5f;
|
Single-precision decimal values. |
double
|
64-bit |
double pi = 3.14159;
|
Double-precision decimal values. |
char
|
16-bit |
char grade = 'A';
|
A single UTF-16 code unit. |
boolean
|
JVM-dependent |
boolean valid = true;
|
Represents true or false. |
Integer Data Types
Java provides four integer types:
byte, short, int,
and long.
byte smallNumber = 100;
short number = 30000;
int population = 1000000;
long distance = 9876543210L;
int is the
usual default choice. Use long when values
can exceed the range of int.
Floating-Point Data Types
Java provides float and double
for floating-point numbers.
float temperature = 36.5f;
double salary = 55000.75;
A floating-point literal is treated as double
by default. Use the f suffix for a
float literal.
Character Type
The char type represents a single UTF-16 code
unit and uses single quotes.
char grade = 'A';
char symbol = '#';
char digit = '5';
Boolean Type
The boolean type represents one of two logical
values: true or false.
boolean isLoggedIn = true;
boolean hasPermission = false;
Reference Data Types
Reference types store references to objects rather than primitive values directly.
Common reference types include classes, arrays, interfaces, enums, and records.
String name = "CIIT Institute";
int[] numbers = {10, 20, 30};
Person person = new Person();
String
String is a class used to represent sequences
of characters.
String name = "CIIT Institute";
System.out.println(name);
Strings are objects and are immutable in Java.
Constants with final
The final keyword can be used when a variable
should not be reassigned after initialization.
final double PI = 3.141592653589793;
Local Variable Type Inference
Modern Java supports the var keyword for local
variables. The compiler infers the variable's type from its
initializer.
var name = "Samadhan";
var age = 21;
var salary = 45000.50;
var does not make Java dynamically typed.
The type is still determined at compile time.
Declaration and Initialization
A variable can be declared first and initialized later when using an explicit type.
int age;
age = 21;
However, var requires an initializer because
the compiler needs that value to infer the type.
var age = 21;
Default Values
Instance and static fields receive default values when they are not explicitly initialized. Local variables do not receive automatic default values and must be initialized before use.
| Type | Default Field Value |
|---|---|
byte, short, int, long |
0 |
float, double |
0.0 |
char |
'\u0000' |
boolean |
false |
| Reference types | null |
Complete Example
public class Program {
public static void main(String[] args) {
String name = "CIIT Institute";
int age = 21;
double salary = 50000.50;
char grade = 'A';
boolean employed = true;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
System.out.println("Employed: " + employed);
}
}
Output
Name: CIIT Institute
Age: 21
Salary: 50000.5
Grade: A
Employed: true
Variable Naming Rules
- Variable names are case-sensitive.
- A variable name cannot start with a digit.
- Spaces are not allowed in variable names.
- Java keywords cannot be used as variable names.
- Use meaningful names that describe the stored value.
// Good
int studentAge = 21;
// Avoid
int x = 21;
Summary
Variables allow Java programs to store and work with information. Java divides types into primitive types and reference types. Understanding these types is essential before learning expressions, methods, collections, and object-oriented programming.