Java Variables & Data Types Explained

🚀 Java Series – Day 2 📌 Variables & Data Types in Java 🔹 What is it? A variable is a container used to store data in memory, and data types define what kind of value a variable can store. Java mainly has two types of data types: • Primitive Data Types – int, double, char, boolean, etc. • Non-Primitive Data Types – String, Arrays, Objects, etc. 🔹 Why do we use it? Variables help programs store and manipulate data during execution. For example: In a banking application, a variable can store a user's account balance, name, or transaction amount. Also, Java stores data in different memory areas: • Stack Memory – Stores primitive variables and method calls. • Heap Memory – Stores objects like Strings, arrays, and custom objects. This separation helps Java manage memory efficiently. 🔹 Example: public class Main { public static void main(String[] args) { int age = 22; // Primitive type (stored in stack) double salary = 50000.50; String name = "Raushan"; // Object stored in heap System.out.println(name + " is " + age + " years old."); } } 💡 Key Takeaway: Understanding variables and memory (Stack vs Heap) helps you write efficient and optimized Java programs. What do you think about this? 👇 #Java #CoreJava #JavaDeveloper #Programming #BackendDevelopment

To view or add a comment, sign in

Explore content categories