β‘ Variables & Data Types in Java β
After understanding Java basics, the next important concept is Variables and Data Types. Every Java program stores and manipulates data, and this is done using variables. Letβs understand everything step by step.
β
1οΈβ£ What is a Variable?
A variable is a container that stores data. Think of it like a box that holds values.
Example: int age = 25;
Here:
- int β data type
- age β variable name
- 25 β value stored in variable
Simple Structure:
data_type variable_name = value;
Example:
int number = 10;
double salary = 50000.50;
char grade = 'A';
β
2οΈβ£ Rules for Naming Variables
Java has some rules for variable names.
β Must start with letter, _ or $
β Cannot start with a number
β Cannot use Java keywords
Valid examples:
- int age;
- double salary;
- String studentName;
Invalid examples:
- int 1age;
- double student-name;
β
3οΈβ£ Data Types in Java
Java has two main types of data types.
1οΈβ£ Primitive Data Types
2οΈβ£ Non-Primitive Data Types
πΉ 4οΈβ£ Primitive Data Types
Primitive types store simple values directly in memory. Java has 8 primitive data types.
- byte: 1 byte (e.g., byte a = 10;)
- short: 2 bytes (e.g., short b = 100;)
- int: 4 bytes (e.g., int age = 25;)
- long: 8 bytes (e.g., long population = 8000000000L;)
- float: 4 bytes (e.g., float price = 12.5f;)
- double: 8 bytes (e.g., double salary = 50000.75;)
- char: 2 bytes (e.g., char grade = 'A';)
- boolean: 1 bit (e.g., boolean isTrue = true;)
πΉ 5οΈβ£ Non-Primitive Data Types
Non-primitive types store references to objects.
Examples: String, Arrays, Classes, Objects, Interfaces
Example:
String name = "Java";
int[] numbers = {1, 2, 3, 4};
Difference:
- Primitive: Stores value, fixed size, faster.
- Non-Primitive: Stores reference, dynamic size, slightly slower.
πΉ 6οΈβ£ Type Casting
Type casting means converting one data type to another. There are two types.
β 1. Implicit Casting (Automatic): Smaller type β Larger type.
Example:
int number = 10;
double value = number;
β 2. Explicit Casting (Manual): Larger type β Smaller type.
Example:
double price = 99.99;
int value = (int) price; // Output: 99
πΉ 7οΈβ£ Constants in Java (final keyword)
A constant is a variable whose value cannot change. Java uses the final keyword.
Example:
final double PI = 3.14159;
Constants are usually written in UPPERCASE.
π₯ Example Program (Variables in Java)
class VariablesDemo {
public static void main(String[] args) {
int age = 25;
double salary = 50000.75;
char grade = 'A';
boolean isWorking = true;
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
System.out.println("Working: " + isWorking);
}
}
Output:
Age: 25
Salary: 50000.75
Grade: A
Working: true
β Common Interview Questions
1οΈβ£ What are the 8 primitive data types in Java?
2οΈβ£ What is the difference between primitive and non-primitive data types?
3οΈβ£ What is type casting in Java?
4οΈβ£ What is the difference between implicit and explicit casting?
5οΈβ£ What is the purpose of the final keyword?
π₯ Quick Revision
- Variables β containers for storing data.
- Primitive types: byte, short, int, long, float, double, char, boolean.
- Non-primitive types: String, Arrays, Objects, Classes.
- Type casting: Implicit β automatic; Explicit β manual.
- Constant: Created using the final keyword.
Double Tap β₯οΈ For More