📘 Java Learning Journey — Day 13: Variables & Memory Management Today, I learned about variables and how Java manages memory at runtime. A variable is a memory location used to store values and manipulate data in a program. Every variable is associated with a specific data type, which defines the type of data it can hold. 🔹 Types of Variables in Java 1️⃣ Local Variables Declared inside a method or block Accessible only within that method or block No default values are assigned Memory is allocated in the Stack Segment Memory is deallocated automatically when the method or block execution ends 2️⃣ Instance Variables Declared inside a class, outside any method Accessible throughout the class using object reference Default values are provided by JVM Memory is allocated in the Heap Segment Exists as long as the object exists 🔹 Java Runtime Memory Structure (JRE) When a Java program runs, RAM allocates memory called the Java Runtime Environment (JRE). It consists of four major segments: Code Segment – Stores compiled machine-level (bytecode) instructions Static Segment – Stores static members Stack Segment – Stores local variables and object references Heap Segment – Stores objects and instance variables 🔹 Object Creation & Memory Allocation When execution starts from the main method: Objects are created using the new keyword The object is stored in the Heap Segment The reference variable is stored in the Stack Segment Example: Demo d = new Demo(); Here, new instructs the JVM to create an object in heap memory, while d holds the reference in stack memory. 🚀 This session helped me clearly understand variable scope, lifetime, and JVM memory allocation, which are crucial for writing efficient and optimized Java programs. #Java #CoreJava #Variables #JVM #MemoryManagement #LearningJourney #SoftwareDevelopment
Java Variables & Memory Management with JVM
More Relevant Posts
-
Day8 🚀: Variables & Memory Structure in Java Today’s learning was about what is variable and Types of variables and how Java uses RAM when a program executes. and how program execution works in java This helped me understand what happens behind the scenes when we run a Java program. A variable is a container that stores data values. In Java, every variable must be declared with a specific data type. 🔹 Syntax: data Type variable Name = value; 🔹 Example: int age = 21; double salary = 25000.50; char grade = 'A'; String name = "Theja"; 🔹 Types of Variables Covered 1️⃣ Local Variables *Declared inside methods *Stored in Stack memory *Created when method starts and destroyed when method ends 2️⃣ Instance Variables *Declared inside a class but outside methods *Stored in Heap memory *Each object has its own separate copy 🔹 How Java Uses RAM During Execution When we run a Java program, memory is divided into different segments: 🧠 1. Code Segment °Stores the compiled bytecode °Contains the program instructions 🧠 2. Static Segment °Stores static variables °Memory is allocated only once 🧠 3. Stack Segment °Stores local variables °Stores method calls °Works in LIFO (Last In First Out) order 🧠 4. Heap Segment °Stores objects and instance variables °Managed by Garbage Collector 🔹 How Program Execution Works in Java 1️⃣ Code is written and compiled into bytecode 2️⃣ JVM loads the class into memory 3️⃣ main() method is pushed into Stack 4️⃣ Objects are created in Heap 5️⃣ Variables are allocated memory 6️⃣ After execution, unused objects are removed by Garbage Collector 🔹 What I Understood Today Understanding variables is not enough. Knowing where they are stored in memory and how Java manages RAM gives deeper clarity about performance and program behavior. Learning how Java works internally is making my foundation stronger 💻🔥 #Java #MemoryManagement #JVM #Programming #LearningJourney #Day8
To view or add a comment, sign in
-
-
🚀 Learning Update: Java Encapsulation, POJO Classes & Real-World Object Handling Today’s live session helped me understand how Encapsulation works practically in Java by building a complete program step-by-step. 🔹 Encapsulation in Java Encapsulation protects data by making variables private and providing controlled access through public methods like getters and setters. 🔹 Building a POJO Class We created an Employee class with: • Private variables (empId, empName, empSalary) • Zero-parameterized constructor • Parameterized constructor • Getter and Setter methods This type of class is called a POJO (Plain Old Java Object) and is widely used in real-world Java applications. 🔹 Understanding the this Keyword The this keyword refers to the currently executing object and helps resolve the shadowing problem when local variables and instance variables have the same name. 🔹 Handling Multiple Objects Efficiently Instead of repeatedly creating objects, we used: ✔ Loops to handle multiple inputs ✔ Arrays of objects to store multiple Employee objects ✔ Scanner input handling to read user input dynamically 🔹 Important Debugging Insight While working with Scanner, I learned about the input buffer problem when mixing nextInt() and nextLine() and how to fix it by flushing the buffer. 🔹 Working with CSV Input & Wrapper Classes We also handled input like: 1,Alex,50000 Using: • split() method to separate values • Integer.parseInt() to convert String to integer • Wrapper classes for type conversion 💡 Key Takeaway Writing programs step-by-step and understanding how objects, constructors, arrays, and input handling work together makes Java concepts much clearer. Excited to keep improving my Core Java and problem-solving skills through continuous practice. #Java #Encapsulation #OOP #POJO #Programming #SoftwareDevelopment #LearningJourney #JavaDeveloper TAP Academy
To view or add a comment, sign in
-
-
Day 2 – Learning Java Full Stack Java may look simple on the surface, but today I learned what actually happens behind the scenes when a Java program runs. Here’s a clear breakdown of my learning 👇 🔹 Execution of a Java Program A Java program follows a two-step process: 1️⃣ Compilation 2️⃣ Interpretation (Execution) 🔹 Step 1: Compilation Java source code must be saved with a .java extension The javac compiler checks for syntax errors If a syntax error exists → compile-time error (bytecode is NOT generated) If no errors → bytecode (.class file) is generated Important: Without successful compilation, execution never happens. 🔹 Step 2: JVM Verification & Execution Once bytecode is generated, JVM performs multiple checks: ✔️ Confirms bytecode is generated by javac ✔️ Verifies bytecode is not modified ✔️ Checks for logical errors during execution If all checks pass → ➡️ JVM converts bytecode into machine code and executes it. If a logical error occurs → ➡️ Exception is raised, and execution stops immediately. 🔹 Understanding Exceptions with Examples Division by zero → ArithmeticException When an exception occurs: Statements before the error execute Statements after the error do NOT execute 🔹 Key Commands I Practiced javac Demo.java → compilation java Demo → execution And revisited the basic structure: Class declaration main() method → entry point of execution Key takeaway: Java doesn’t blindly execute code. It verifies, validates, and protects execution through compiler checks, JVM verification, and exception handling. Sharing my learning as I go — hoping this helps someone understand Java execution more clearly 🙌 More insights coming soon. #Java #JavaFullStack #JVM #ExceptionHandling #LearningInPublic #BackendDevelopment #ProgrammingBasics
To view or add a comment, sign in
-
-
🚀 Learning Core Java – Understanding static Keyword & Method Area Many people believe that a Java program starts its execution from the main() method. But technically, the process starts even before that. When a Java program runs, the class is first loaded into memory (RAM) by the JVM. During this phase, the JVM scans and loads static members of the class. The execution flow generally follows this order: 1️⃣ The class is loaded into memory (method area). 2️⃣ The JVM initializes static variables. 3️⃣ Static blocks are executed. 4️⃣ Finally, the JVM looks for the main() method, which is the entry point for program execution. ⸻ 🔹 Static vs Instance Members Static Members • Belong to the class itself, not to objects. • Stored in the method area of memory. • Can be accessed directly using the class name. • Do not require an object for access. Example conceptually: ClassName.staticVariable ⸻ Instance Members • Belong to individual objects. • Created when an object is instantiated. • Accessed using the object reference. Example conceptually: object.variable ⸻ 🔎 Important Rule ✔ Static members can access only static members directly. ✔ Instance members can access both static and instance members. ⸻ 💡 Key Insight • Static members → belong to the class • Instance members → belong to objects Understanding the static keyword and memory structure helps in writing efficient and well-organized Java programs. Excited to keep strengthening my Java fundamentals! 🚀 #CoreJava #JavaProgramming #StaticKeyword #JavaMemory #ProgrammingFundamentals #JavaDeveloper #LearningJourney #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 8 | Core Java Learning Journey 📌 Topic: String in Java Today, I learned about Strings in Core Java, one of the most frequently used and important concepts for handling textual data in Java applications. 🔹 What is a String ? A String is a sequence of characters. In Java, Strings are objects used to store and manipulate text efficiently. 🔹 Declaration of String There are two ways to create a String in Java: 1️⃣ Using String Literal String name = "Ketan"; 2️⃣ Using new Keyword String name = new String("Ketan"); 🔹 Important String Classes Provided by Java Java provides several built-in classes for handling strings: 1️⃣ java.lang.String 2️⃣ java.lang.StringBuffer 3️⃣ java.lang.StringBuilder 4️⃣ java.util.StringTokenizer 🔹 What is SCP (String Constant Pool)? The String Constant Pool is a special memory area inside the heap that stores String literals to optimize memory usage and improve performance. 🔹 Properties of SCP ✅ Stores only String literals ✅ Prevents duplicate String objects ✅ Improves memory utilization ✅ Enhances application performance 🔹 String Memory Allocation in Java Strings created using the new keyword create a new object in the Heap area, while their literals exist in the String Constant Pool (SCP). Strings created without the new keyword are stored only in the SCP, and if the value already exists, Java reuses the same reference instead of creating a new object, which improves memory efficiency. 🔹 Interview Insight (Java Strings) ✔️ Every predefined class in Java inherits from the Object class. ✔️ The String class is final to prevent inheritance and ensure security. ✔️ String objects are immutable, which helps in performance optimization, caching, and thread safety. 📌 Key Learning: A strong understanding of Strings, SCP, and immutability helps in writing secure, efficient, and optimized Java applications. A special thanks to Vaibhav Barde Sir for his consistent guidance and clear explanations. 🙏 Looking forward to learning more Core Java concepts ahead! 💻✨ #CoreJava #JavaStrings #JavaDeveloper #BackendEngineering #SoftwareDeveloper #ComputerScienceGraduate #ProgrammingLife #TechLearning #JavaConcepts #LearningJourney
To view or add a comment, sign in
-
-
🚀 Learning Java OOP — Understanding Object Class in Java Today I explored one of the most important concepts in Java: **Object Class**, the root of the entire class hierarchy. 🔹 Every class in Java directly or indirectly inherits from `Object` class 🔹 It provides common methods available to all objects 🔹 This is why every object in Java gets default behaviors automatically ✅ Important methods in Object Class: • `toString()` → Converts object data into readable text • `equals()` → Compares two objects • `hashCode()` → Generates unique hash value • `getClass()` → Returns runtime class information • `clone()` → Creates duplicate object • `wait()`, `notify()`, `notifyAll()` → Used in multithreading • `finalize()` → Deprecated method 💡 Key Insight: When we print an object reference, Java internally calls `toString()`. That is why overriding `toString()` helps display object data in a meaningful way. 📌 Object class contains **12 methods + 1 constructor**, and it is called the **parent of all Java classes**. #Java #OOP #ObjectClass #Programming #LearningJourney #JavaDeveloper #SoftwareDevelopment
To view or add a comment, sign in
-
-
Day -12 🚀 Understanding Java Strings: Memory Management & Comparison While learning Java, one important concept every developer should understand is how Strings are stored and compared in memory. 🔹 String Constant Pool (SCP) When a string is created using a literal: Java Copy code String s = "Java"; It is stored in the String Constant Pool, which avoids duplicate values and saves memory. Multiple references can point to the same string object. 🔹 Heap Memory When a string is created using the new keyword: Java Copy code String s = new String("Java"); A new object is always created in the heap, even if the same value already exists. 📌 String Comparison Methods ✅ Reference Comparison (==) Checks whether two references point to the same memory location. Java Copy code s1 == s2 ✅ Value Comparison (.equals()) Checks whether the actual characters in the strings are the same. Java Copy code s1.equals(s2) ✅ Case-Insensitive Comparison (.equalsIgnoreCase()) Compares strings ignoring uppercase and lowercase differences. Java Copy code s1.equalsIgnoreCase(s2) 💡 Key Takeaway: Use string literals for memory efficiency and .equals() when comparing string values. Understanding these small concepts helps build strong programming fundamentals and improves coding practices in Java development. #Java #JavaProgramming #Programming #Coding #SoftwareDevelopment #LearnToCode #ComputerScience #CodingJourney #Developers #TechLearning
To view or add a comment, sign in
-
-
Day 9 | Full Stack Development with Java Today’s learning helped me understand how Java actually manages memory and variables behind the scenes. JRE (Java Runtime Environment) JRE provides the runtime environment required to execute Java programs. When a Java program runs, memory is divided into segments: Code Segment Static Segment Heap Segment Stack Segment Understanding this made it easier to connect variables with memory allocation. What is a Variable? A variable is a named memory location used to store data. Each variable: Has a data type Stores a specific type of value Must be declared before use data_type variable_name; Example: Java Copy code int a; Types of Variables in Java Instance Variables Declared inside a class but outside methods Stored in Heap memory Created when an object is created Assigned default values by JVM Default primitive values: int, byte, short, long → 0 float, double → 0.0 boolean → false char → empty character Objects → null Local Variables Declared inside a method Stored in Stack memory Exist only during method execution Must be initialized before use No default values provided Pass by Value (Java Concept) In Java, arguments are passed by value. This means: A copy of the value is passed. Changes inside the method do not affect the original variable. Reference Behavior When objects are assigned: Java Car a = new Car(); Car b = a; Both a and b refer to the same object in heap memory. Modifying object data using b will reflect when accessed using a, because both point to the same memory address. Key Takeaway Understanding variables is not just about syntax — it’s about understanding: Memory allocation (Stack vs Heap) Object references Data flow inside programs This is helping me build strong backend fundamentals step by step. #Day9 #Java #Variables #JRE #FullStackDevelopment #LearningInPublic #ProgrammingJourney
To view or add a comment, sign in
-
-
📘 Java Learning Journey — Day 10, 11 & 12 Over the past few sessions, I deepened my understanding of some fundamental Java concepts that play a crucial role in writing efficient and reliable programs. 🔹 Day 10: Typecasting in Java Typecasting is the process of converting one data type into another. Java supports two types of typecasting: 1️⃣ Implicit Typecasting (Widening) This occurs when a smaller data type is automatically converted into a larger data type by the JVM. Example: byte → int → float → double No data loss occurs because the destination type has a wider range. Real-world analogy: Think of pouring water from a small bucket into a big bucket — it fits perfectly without spilling. 2️⃣ Explicit Typecasting (Narrowing) This is the manual conversion of a larger data type into a smaller data type using casting syntax. Here, loss of precision may occur, which is known as truncation. Real-world analogy: Trying to pour water from a big bucket into a small bucket — overflow is unavoidable unless you limit the quantity. 🔹 Day 11: Increment & Decrement Operators I explored pre-increment (++a) and post-increment (a++), and how their behavior differs during execution. An interesting observation was with byte overflow: A byte ranges from -128 to 127 Incrementing beyond 127 causes the value to wrap around to -128 This happens at runtime, not during compilation This highlights how Java handles arithmetic operations internally during execution. 🔹 Day 12: Execution Behavior & Data Overflow Understanding how Java manages data at runtime helped clarify: Why arithmetic operations on smaller data types are promoted to int How overflow occurs silently during execution The importance of choosing the right data type for accuracy and safety 🚀 These concepts strengthened my foundation in Java and improved my understanding of how the JVM handles data behind the scenes. #Java #CoreJava #TypeCasting #IncrementDecrement #ProgrammingBasics #LearningJourney #SoftwareDevelopment #JVM
To view or add a comment, sign in
-
-
🚀 Day 30 – Java Static Keyword Today’s learning from my Java Notes was all about the powerful static keyword. 📘 The static keyword in Java is used to define members that belong to the class, not to the object. 🔹 Key Understanding: ✅ Static Members belong to the Class Static Variables Static Methods Static Blocks ✅ Non-Static (Instance) Members belong to Objects Instance Variables Instance Methods Instance Blocks Constructors 💡 Rules of Static: 1️⃣ Static variables can be accessed by both static and instance members. 2️⃣ Instance variables can be accessed only by instance members. 3️⃣ Static members cannot directly access instance variables. 📌 Static Variables: Belong to the class Initialized only once when the class is loaded Single copy shared by all objects Accessed using: ClassName.variableName 📌 Static Methods: Belong to the class Can access only static data Cannot call non-static methods directly Accessed using: ClassName.methodName() 📌 Static Block: Executes only once when the class loads Used to initialize static variables Runs before the main() method 🔄 Flow of Execution: 1️⃣ Static variables & static block 2️⃣ Main method 3️⃣ Instance block 4️⃣ Constructor 5️⃣ Instance method #Day30 #Java #StaticKeyword #JavaLearning #BackendDevelopment
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development