TGViewer
Programming Bay Programming Bay @programmingbay · 2.26K subscribers
Post #426 259
⚡ 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?


🔥 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.


Claim your Free $5 Bonus Here:
https://bit.ly/3wUxw09

LinkedIn profile 👇

https://www.linkedin.com/in/subarno-roy-3b2251374

Join our WhatsApp Channel 👇

https://whatsapp.com/channel/0029VbAi27y0lwghBe9mE42i

WhatsApp Community Link 👇

https://chat.whatsapp.com/HPJDqRr6G1sKQIqfdJF3pL

1️⃣ GROUP FOR PROGRAMMERS🖥
📎 Channel Link:
[ https://t.me/realgroupforprogrammer ]

2️⃣ Coding Community
📎 Channel Link:
[ https://t.me/Coding_CommunityOfficial ]

3️⃣ Programming Bay
📎 Channel Link:
[ https://t.me/programmingbay ]

4️⃣ Data Structures and Algorithms
📎 Channel Link:
[ https://t.me/datastructuresandalgoofficial ]
  • ❤ 1
More from @programmingbay
  1. Oct 3, 2026𝗧𝗵𝗼𝘀𝗲 𝘄𝗵𝗼 𝘄𝗮𝗻𝘁 𝗿𝗲𝗳𝗲𝗿𝗿𝗮𝗹𝘀 𝗮𝗻𝗱 𝗝𝗼𝗯𝘀 𝗮𝗻𝗱 𝗜𝗻𝘁𝗲𝗿𝗻𝘀𝗵𝗶𝗽�…
  2. Sep 30, 2026🚀 Understand Any Tech System with One Prompt as a programmer (Note: System understanding,…
  3. Sep 30, 2026❤️ Here is the list of highly recommended Telegram channels for your free learning ❤️ Get…
  4. Sep 27, 2026🚨 Earn Passive Money Easily 🚨 Since your device is already connected to the internet, yo…
  5. Sep 26, 2026Hackathon National Research & Innovation Challenge Season 3 📌APPLY LINK : https://bit.ly/…
  6. Sep 13, 2026✅ Coding Interview Acronyms You MUST Know 💻🔥 DSA → Data Structures & Algorithms CPU → Ce…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →