🚀 Day 13 – Java Full Stack Journey | Understanding Variables & Memory in Java Today’s session was about something every developer must truly understand — How Java manages memory internally. Not just writing code, but understanding what happens inside RAM when a program runs. 🧠 🔹 Variables in Java A variable is a container used to store data. Java mainly has two important types of variables: ✔ Instance Variables ✔ Local Variables 🔹 Instance Variables Declared directly inside a class Memory allocated inside the Heap segment Stored inside objects Automatically assigned default values by Java Example: class Demo { int a; float b; boolean c; } Default values: int → 0 float → 0.0 boolean → false char → empty character 🔹 Local Variables Declared inside a method Memory allocated inside the Stack segment ❌ No default values Must be initialized before usage Example: int a; System.out.println(a); // Compilation error Java will not assign default values to local variables. 🔹 Memory Structure During Execution When a Java program runs, memory is divided into: • Code Segment • Heap Segment • Stack Segment • Static Segment Objects → Heap Instance variables → Inside objects Local variables → Stack References → Stack 💡 Key Learning of the Day Understanding memory flow helps in: Debugging effectively Avoiding logical errors Writing optimized code Performing better in technical interviews Today was less about syntax and more about JVM thinking. Day 13 Complete ✔ #Day13 #Java #CoreJava #JVM #MemoryManagement #HeapAndStack #JavaDeveloper #FullStackJourney #LearningInPublic TAP Academy
Java Memory Management: Variables & Heap vs Stack
More Relevant Posts
-
🚀 Day 16 – Java Full Stack Journey | Methods (All 4 Types) & Memory Execution Deep Dive Today was about mastering one of the most powerful concepts in Java: 👉 Methods 👉 Parameters vs Arguments 👉 Stack & Heap execution flow Not just writing methods — but understanding how they execute inside memory. 🔹 The 4 Types of Methods in Java 1️⃣ No Input – No Output public void greet() { System.out.println("Hello World"); } 2️⃣ No Input – With Output public int add() { return 50 + 40; } 3️⃣ With Input – No Output public void add(int a, int b) { int c = a + b; System.out.println(c); } 4️⃣ With Input – With Output public int add(int a, int b) { return a + b; } This is the most commonly used type in real-world applications. 🔹 Important Terminologies ✔ Parameters → Variables declared in method signature ✔ Arguments → Values passed during method call Example: add(50, 40); int a, int b → Parameters 50, 40 → Arguments 🔹 What Happens in Memory? When a method is called: 1️⃣ A Stack Frame is created 2️⃣ Local variables are stored inside the stack 3️⃣ Objects are created inside the Heap 4️⃣ After execution → Stack frame is removed 5️⃣ Objects without references → Become Garbage 6️⃣ Garbage Collector cleans them automatically This follows LIFO (Last In, First Out) principle. Understanding this makes debugging and interviews much easier. 🔹 Real Power of Methods Instead of rewriting logic multiple times: printSquare(n); printSquare(n2); printSquare(n3); You write the logic once, reuse it multiple times. ✔ Code Reusability ✔ Clean Structure ✔ Reduced Code Duplication ✔ Better Maintainability 💡 Biggest Learning Today Java is not about memorizing syntax. It’s about: Understanding execution flow Knowing how memory behaves Writing reusable, structured logic These fundamentals build the foundation for: OOPS → Exception Handling → Collections → Multithreading → Real Projects Strong basics = Strong developer. Day 16 Complete ✔ #Day16 #Java #CoreJava #Methods #StackAndHeap #JVM #GarbageCollection #OOPS #FullStackJourney #LearningInPublic TAP Academy
To view or add a comment, sign in
-
-
☕ #ThinkingInJava — Post No. 2 Building deeper Java understanding, one concept at a time. 👉 What made me revisit this? While exploring Java file structure, I had a follow-up curiosity: if multiple classes can exist in one file, what happens to the main() method? Where should it live, and which one runs? 👇 💡 Java Concept — Multiple classes & main() behavior Java allows flexibility in structuring classes inside a file, but execution behavior is very explicit and runtime-driven. ✅ Core Rules / Facts • A Java file can contain multiple classes, but at most one can be public • The main() method does not have to be inside the public class • You can define main() in any class within the file • If multiple classes contain main(), none runs automatically • JVM executes only the class explicitly specified at runtime (or selected in IDE) 🎯 Interview One-liner 👉 In Java, the main() method can exist in any class, and when multiple entry points exist, the JVM runs only the class explicitly invoked. 🧠 Why this matters in real projects Understanding entry-point behavior helps while debugging multi-class utilities, running POCs, and organizing automation helpers that may contain independent executable code. 🔖 Takeaway Execution in Java is explicit → Structure is flexible → Clarity comes from understanding entry points hashtag #Java #AutomationSpecialist #TestAutomation
To view or add a comment, sign in
-
🚀 Java 8 Series – Day 6 𝗦𝘁𝗿𝗲𝗮𝗺 𝗔𝗣𝗜 – 𝗜𝗻𝘁𝗲𝗿mediate vs Terminal Operations Yesterday we introduced Streams. Today let’s understand how Streams actually execute. A Stream pipeline has 3 parts: 𝗦𝗼𝘂𝗿𝗰𝗲 → 𝗜𝗻𝘁𝗲𝗿𝗺𝗲𝗱𝗶𝗮𝘁𝗲 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀 → 𝗧𝗲𝗿𝗺𝗶𝗻𝗮𝗹 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻 But what’s the difference between Intermediate and Terminal? 🔹 𝗜𝗻𝘁𝗲𝗿𝗺𝗲𝗱𝗶𝗮𝘁𝗲 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀 👉Return another Stream 👉 Lazy in nature 👉 Do NOT execute immediately 𝗘𝘅𝗮𝗺𝗽𝗹𝗲𝘀: ⭐ filter() ⭐ map() ⭐ sorted() ⭐ distinct() ⭐ limit() ⭐ skip() ⭐ flatMap() 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: names.stream() .filter(name -> name.length() > 3) .map(String::toUpperCase); This will NOT execute yet. Why? Because there is no terminal operation. 🔹 𝗧𝗲𝗿𝗺𝗶𝗻𝗮𝗹 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀 👉 Trigger the execution 👉 Produce a final result 👉 Close the stream 𝗘𝘅𝗮𝗺𝗽𝗹𝗲𝘀: ⭐ collect() ⭐ forEach() ⭐ reduce() ⭐ count() ⭐ min() ⭐ max() 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: List result = names.stream() .filter(name -> name.length() > 3) .map(String::toUpperCase) .collect(Collectors.toList()); Now it executes. 🔥 𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝗖𝗼𝗻𝗰𝗲𝗽𝘁: 𝗟𝗮𝘇𝘆 𝗘𝘃𝗮𝗹𝘂𝗮𝘁𝗶𝗼𝗻 Streams execute only when a terminal operation is present. This improves performance because operations are chained and optimized internally. Visual Flow Source → filter → map → sorted → collect Single pass processing. Not multiple loops. Tomorrow: Deep dive into map() vs flatMap() 🔥 (Most confusing interview topic) Follow the series if you're building strong Java fundamentals 🚀 #Java #Java8 #StreamAPI #BackendDeveloper #Coding #InterviewPreparation #SpringBoot
To view or add a comment, sign in
-
Day 11/30 🚀 Java Deep Dive: Class Loading, Static Flow & Execution Order In our recent Java sessions, I explored what really happens when a Java program runs — and it completely changed my understanding beyond the common “execution starts from main()” belief. 🔍 Key Learnings: ✅ A Java file can contain multiple classes → compilation generates multiple .class files ✅ JVM executes the class that contains the main method ✅ Before main() runs, JVM performs class loading and initializes: ➡️ Static variables ➡️ Static blocks ➡️ Then the main method stack frame is created 🧠 7 Elements of a Java Class: Static Variables Static Block Static Methods Instance Variables Instance Block Instance Methods Constructors ⚖️ Golden Rule: 👉 Static belongs to the class 👉 Instance belongs to the object This directly impacts memory allocation and execution flow: Static members → loaded once in the method area / static segment Instance members → created per object in the heap Instance block executes before constructor during object creation 🛠️ Behind the Scenes Flow: Source Code → Compilation → Bytecode → JVM → Class Loader → Static Initialization → main() → Object Creation → Constructor → Methods → Garbage Collection Understanding this internal flow helped me connect OOP concepts, memory model, and JVM architecture instead of treating them as separate topics. 📌 This is a crucial foundation for: ✔ Writing optimized Java code ✔ Understanding static vs instance behavior ✔ Cracking interviews on JVM & class loading #Java #JVM #OOP #ClassLoader #StaticKeyword #JavaInternals #ProgrammingJourney #SDEPreparation
To view or add a comment, sign in
-
-
Many people write Java code every day, but very few stop and think about how memory actually works behind the scenes. Understanding this makes debugging and writing efficient code much easier. Here’s a simple way to think about Java memory inside the JVM: 1. Heap Memory This is where all the objects live. Whenever we create an object using "new", the memory for that object is allocated in the heap. Example: "Student s = new Student();" The reference "s" is stored somewhere else, but the actual "Student" object is created inside the Heap. Heap memory is shared and managed by Garbage Collection, which automatically removes unused objects. 2. Stack Memory Stack memory is used for method execution. Every time a method runs, a new stack frame is created. This frame stores local variables and references to objects. When the method finishes, that stack frame is removed automatically. That’s why stack memory is fast and temporary. 3. Method Area (Class Area) This part stores class-level information such as: • Class metadata • Static variables • Method definitions • Runtime constant pool It is created once when the class is loaded by the ClassLoader. 4. Thread Area / Program Counter Each thread has its own program counter which keeps track of the current instruction being executed. It helps the JVM know exactly where the thread is in the program. In simple terms: Stack → Method execution Heap → Object storage Method Area → Class definitions Thread Area → Execution tracking Understanding this flow gives a much clearer picture of what really happens when a Java program runs. #Java #JVM #BackendDevelopment #SoftwareEngineering #Programming
To view or add a comment, sign in
-
-
🚀 Day 13— Restarting My Java Journey with Consistency Today's Learnings: 🔹 Instance Variables & Instance Methods These belong to objects and are accessed using the object reference. They help define the state and behavior of objects. 🔹 Local Variables Local variables do not get default values in Java. They must be initialized before use, otherwise the compiler throws an error. 🔹 Constructors in Java Constructors are special methods used to initialize objects at the time of creation. No return type Same name as class Example: Student s1 = new Student(); When an object is created, the constructor is automatically invoked to initialize the object. Types of constructors : • Default Constructor – No parameters • Parameterized Constructor – Accepts values to initialize object fields Important rule: If a programmer defines any constructor, Java does not create a default constructor automatically. 🔹 this Keyword this refers to the current object. It is commonly used to distinguish instance variables from local variables. 🔹 Constructor Overloading A class can have multiple constructors with different parameter lists to initialize objects in different ways. 🔹 Constructor Chaining Using this() we can call one constructor from another constructor within the same class. Important rule: The this() call must be the first statement inside a constructor. Interview Specific: 🔹 Can We Call a Constructor Manually? No. Constructors cannot be called like normal methods. They are automatically invoked when an object is created using the new keyword. 🔹 Interesting Runtime Insight When we create an object using new, Java allocates memory in the heap at runtime. But what if heap does not have enough free space, then Java will throw a runtime exception. Learning daily with Coder Army and Aditya Tandon Bhaiya and Rohit Negi Bhaiya #Day13 #Java #Consistency #BackendDevelopment #LearningJourney #SoftwareEngineering #CoderArmy
To view or add a comment, sign in
-
-
Still using loops in Java? You might be missing something powerful… 🚀 Day 6 of Prepare with Pankaj 💻 🔹 What is Stream? A Stream is a sequence of elements used to process collections (List, Set) in a functional and efficient way. 🔹 Why use Streams? ✔ Less code (no complex loops) ✔ Better readability ✔ Easy parallel processing 🔹 Common Operations: 👉 filter() Used to filter data Example: Get only even numbers 👉 map() Used to transform data Example: Multiply each number by 2 👉 collect() Used to collect the result into a List or Set 🔹 Simple Example: import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); List<Integer> result = list.stream() .filter(n -> n % 2 == 0) .map(n -> n * 2) .collect(Collectors.toList()); System.out.println(result); } } 💡 Conclusion: Streams help you write clean, concise, and efficient code. Must-know for every Java developer! #Java #Java8 #Streams #BackendDeveloper #Coding #PrepareWithPankaj 🚀
To view or add a comment, sign in
-
-
Day 8 – Understanding the Java ClassLoader ⏳ 1 Minute Java Clarity – How Java loads classes When we run a Java program, the JVM needs to load classes into memory before executing them. But how does that happen? That’s the job of the ClassLoader. Here’s the simple idea 👇 📦 What is a ClassLoader? A ClassLoader is a component of the JVM that loads .class files into memory so the program can run. In simple terms: 👉 ClassLoader loads Java classes for the JVM. ⚙️ Types of ClassLoaders Java mainly uses three types: 1️⃣ Bootstrap ClassLoader Loads core Java classes like java.lang, java.util. 2️⃣ Extension ClassLoader Loads classes from the Java extension libraries. 3️⃣ Application ClassLoader Loads classes from the application’s classpath. 💡 Why ClassLoader is important Dynamically loads classes when needed, Improves memory efficiency, Helps the JVM manage large applications 📌 Quick summary ClassLoader → Loads .class files → JVM executes them. 🔹 Next in my #1MinuteJavaClarity series → What is the Java String Pool? ❓ Did you know Java uses different class loaders behind the scenes? #Java #BackendDeveloper #JavaFullStack #LearningInPublic #Programming #JavaProgramming #SoftwareEngineering #TechCommunity
To view or add a comment, sign in
-
-
#Java #JVM #JVMInternals #SystemDesign #Bytecode #Programming #Architecture #ClassLoader Deconstructing Hello World: A Deep Dive into JVM Internals Every Java developer has written : System.out.println("Hello World"); But here’s a serious question: - Can you explain what actually happens after you press Run ? * Most developers can build APIs. * Some can optimize performance. * Very few truly understand what the JVM is doing internally. So I decided to break down the simplest Java program - from a runtime and architecture perspective. In this GitHub project, I deconstructed below mentioned: • How the JVM bootstraps and initializes • How the ClassLoader loads java.lang.System • What happens during bytecode verification • How JIT compiles hot methods • How System.out is wired to PrintStream • What println() actually invokes internally • How Java eventually talks to the OS This isn’t about printing text. * It’s about understanding abstraction layers. * It’s about respecting the runtime. * It’s about engineering maturity. Because senior developers don’t just write code. - They understand the system executing it. If you care about JVM internals, performance engineering, or writing Java with intent - this might interest you. https://lnkd.in/gkYGkrJj
To view or add a comment, sign in
-
Mastering Java: From Encapsulation to POJO Classes 🚀 Just finished an intensive session on deep-diving into Java Encapsulation and the practical implementation of POJO (Plain Old Java Object) classes. Understanding how to structure data and provide controlled access is the cornerstone of professional software development. Here are the key takeaways: 🔹 Encapsulation & Security: It’s not just about making variables private. It’s about providing controlled access through public getters and setters, ensuring data integrity across your application. 🔹 The POJO Standard: A true POJO class isn't just a container. To be fully functional and industry-standard, it needs: Private variables A zero-parameter constructor A parameterized constructor Both getters and setters for all fields 🔹 Handling Input Like a Pro: We explored solving the common Scanner buffer problem (that annoying "slash n" issue when switching from nextInt() to nextLine()) and how to efficiently process CSV-style input using String.split() and Integer.parseInt(). 🔹 Object Management: Instead of creating and destroying objects in a loop, we learned to store them in Object Arrays, allowing us to manage and retrieve data for 50+ objects as easily as one. 💡 Pro-Tip: Use IDE shortcuts (like Alt+Shift+S in Eclipse) to automatically generate your boilerplate code! Focus your energy on solving the logic, not typing getters. #Java #Programming #SoftwareDevelopment #CleanCode #ObjectOrientedProgramming #TechLearning #POJO #Encapsulation
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