Java Data Types and Variables Explained
In Java, data types specify the kind of data a variable can store, and variables are containers that hold values during program execution. Understanding both is fundamental for writing effective Java code.
1. What Is a Variable?
A variable is a name given to a memory location where data is stored.
Example
int age = 25;
String name = "John";
Here:
age is a variable of type int
name is a variable of type String
2. Types of Variables in Java
Java supports three kinds of variables:
1. Local Variables
Declared inside a method or block.
void test() {
int count = 10; // local variable
}
2. Instance Variables
Belong to an object (non-static).
class Person {
String name; // instance variable
}
3. Static Variables
Shared among all objects (declared with static).
static int totalUsers = 0;
3. Java Data Types
Java has two main categories of data types:
A. Primitive Data Types
These are basic built-in types.
Data Type Size Example Description
byte 1 byte byte a = 10; Small integer range
short 2 bytes short s = 200; Larger than byte
int 4 bytes int age = 25; Most common integer type
long 8 bytes long distance = 123456L; Very large integers
float 4 bytes float price = 10.5f; Decimal values (less precision)
double 8 bytes double salary = 45678.90; More precise decimal values
char 2 bytes char grade = 'A'; Single character
boolean 1 bit boolean isLogin = true; True/false values
B. Non-Primitive (Reference) Data Types
Used for objects or collections.
Examples:
String
Arrays
Classes & Objects
Interfaces
Example:
String city = "Hyderabad";
int[] numbers = {1, 2, 3};
4. Declaring and Initializing Variables
Declaration
int age;
Initialization
age = 30;
Declaration + Initialization
int age = 30;
5. Naming Rules for Variables (Important)
✔ Must start with a letter, _, or $
✔ Cannot start with a number
✔ No spaces allowed
✔ Case-sensitive
✔ Cannot use Java keywords (class, int, etc.)
Examples
Valid:
int employeeCount;
String _name;
double $salary;
Invalid:
int 1age;
String employee name;
6. Type Casting in Java
A. Widening (Implicit)
Small → large (safe conversion)
int x = 10;
double y = x; // OK
B. Narrowing (Explicit)
Large → small (risk of data loss)
double a = 10.5;
int b = (int) a; // 10
7. Constants in Java
Use final keyword to make a variable unchangeable.
final int MAX_USERS = 100;
8. Example Program Using Data Types and Variables
public class DataTypeExample {
public static void main(String[] args) {
int id = 101;
String name = "Alice";
double salary = 50000.75;
boolean isActive = true;
System.out.println(id);
System.out.println(name);
System.out.println(salary);
System.out.println(isActive);
}
}
Summary Table
Category Types
Primitive Types byte, short, int, long, float, double, char, boolean
Non-Primitive Types String, Arrays, Classes, Interfaces
Variable types Local, Instance, Static
Learn Full Stack JAVA Course in Hyderabad
Read More
Object-Oriented Programming in Java
Using Bootstrap in Frontend Development
Routing in Angular – A Beginner’s Guide
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments