Java Programming: post #1160 — TG.ME

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
❤32👌2
March 12, 2026 8.5K 14