A class is only a blueprint. It becomes powerful when you create an 𝗼𝗯𝗷𝗲𝗰𝘁. When you write: 𝐂𝐚𝐫 𝐦𝐲𝐂𝐚𝐫 = 𝐧𝐞𝐰 𝐂𝐚𝐫(); Something important happens. You’re not just declaring a variable. You’re allocating memory on the heap and creating a real instance of that blueprint. This introduces one of the most important concepts in Java: 𝐑𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝐯𝐬 𝐎𝐛𝐣𝐞𝐜𝐭 • 𝐂𝐚𝐫 𝐦𝐲𝐂𝐚𝐫 → reference (lives on the stack) • 𝐧𝐞𝐰 𝐂𝐚𝐫() → actual object (lives on the heap) The reference doesn’t contain the object. It points to it. That distinction explains: • Why multiple variables can reference the same object • Why 𝐧𝐮𝐥𝐥 causes runtime errors • Why object comparison can be tricky Today was about: • Understanding how objects are created • What the 𝐧𝐞𝐰 keyword really does • The difference between stack and heap memory Once you understand references, Java stops feeling magical — and starts feeling logical. #Java #OOP #MemoryManagement #Programming #SoftwareEngineering #LearningInPublic
Java Objects: References vs Objects
More Relevant Posts
-
I just completed an intensive session focused on the core of Java's efficiency: Immutable Strings and Memory Management. While we often use strings daily, understanding what happens under the hood is what separates a coder from a developer. Key Takeaways from the session: The Power of Command Line Arguments: We explored how the String[] args in the main method actually functions, learning how to pass dynamic data into applications via the CLI—a crucial skill for building professional-grade tools . Strings as Objects: In Java, strings aren't just data; they are objects . I learned the three distinct ways to initialize them: using the new keyword, using string literals, and converting character arrays . Memory Architecture (SCP vs. Heap): This was a game-changer. I now understand that Java optimizes memory by using the String Constant Pool (SCP) for literals to prevent duplicates, while the Heap Area allows for duplicate objects when the new keyword is used . The Comparison Trap: I finally mastered the difference between reference comparison and value comparison. Using == compares the memory address (reference), while the .equals() method compares the actual content . Immutability: We began exploring why certain data, like birthdays or names, are best handled as immutable strings—meaning they cannot be changed once created in memory . I'm looking forward to the next phase of this journey: Object-Oriented Programming (OOP)! . #Java #SoftwareDevelopment #Programming #MemoryManagement #TechLearning #JavaDeveloper #CodingJourney #Tapacadmey
To view or add a comment, sign in
-
-
I recently revisited Chapter 3: "Methods Common to All Objects" in Effective Java by Joshua Bloch. This chapter focuses on the core methods defined in Object that every Java class inherits, and how to override them correctly. Here are the key takeaways I found most valuable: 1. equals() • Override equals() when a class has a notion of logical equality, not just object identity. • Follow the contract: reflexive, symmetric, transitive, consistent, and x.equals(null) must return false. • Compare only significant fields and ensure type compatibility. 2. hashCode() • Always override hashCode() when overriding equals(). • Equal objects must have the same hash code. • A good hash function combines the hash codes of significant fields. This is critical when using hash-based collections like HashMap or HashSet. 3. toString() • Provide a meaningful toString() implementation. • Include useful information about the object's state. • A well-designed toString() greatly simplifies debugging and logging. 4. clone() and Comparable • clone() is tricky and often better avoided in favor of copy constructors or factory methods. • Implement Comparable when a class has a natural ordering, and ensure consistency with equals(). 💡 My takeaway: Correctly implementing equals(), hashCode(), and toString() is fundamental for writing reliable Java classes. These methods influence how objects behave in collections, comparisons, debugging, and logging. Getting them right improves correctness and maintainability across the entire codebase. #Java #EffectiveJava #JoshuaBloch #CleanCode #SoftwareEngineering #JavaTips #Coding #JavaDevelopment
To view or add a comment, sign in
-
“Where does data actually live in Java… Stack or Heap?” Not how to write the code. But what really happens in memory when the code runs. When a Java program runs, memory is mainly divided into two places. Stack and Heap. Here’s the simple way to think about it. The Stack stores method calls and local variables. Every time a method runs, a new stack frame is created. When the method finishes, that frame disappears. It’s fast, structured, and managed automatically. The Heap, on the other hand, is where objects actually live. Whenever you create something with new, the object goes into the heap. The stack only keeps the reference pointing to that object. So something like this: Person p = new Person(); What really happens is: ↳ p (reference) lives in the stack ↳ Person object lives in the heap This small distinction explains a lot of things developers struggle with: • why objects persist beyond a method call • how memory leaks happen • how garbage collection works • why references behave the way they do Sometimes the hardest part of software engineering isn’t writing code. It’s understanding what the runtime is doing behind the scenes. How do you usually explain Stack vs Heap to someone learning Java? #Java #SoftwareEngineering #Programming #JavaDeveloper #Coding
To view or add a comment, sign in
-
-
🚀 Java Learning Journey – Day 4 Deep Dive into Stack, Heap & References Today was a big clarity day. Instead of just writing Java code, I focused on understanding what actually happens in memory when a program runs. Here’s what I learned: 🧠 1️⃣ Stack vs Heap Stack stores method calls and local variables. Heap stores objects created using new. Every method call creates a new stack frame. When a method finishes, its stack frame is removed. Understanding this made method execution much clearer. 🔎 2️⃣ Primitive vs Reference Primitive values (int, double, etc.) are stored directly in stack. Reference variables are stored in stack. The actual object they refer to is stored in heap. Example: Person p1 = new Person(); p1 → Stack Person object → Heap ♻️ 3️⃣ Object Reachability & Garbage Collection An object becomes eligible for Garbage Collection when: No references point to it anymore. Reassigning or nullifying references can make objects unreachable. This clarified a lot of confusion around memory leaks and object lifecycle. 💡 Biggest Realization Java memory is not magic. Once you understand: Stack frames Reference copying Object reachability Everything becomes predictable and logical. #Java #LearningJourney #JavaMemoryModel #BackendDevelopment #SoftwareEngineering #Day4
To view or add a comment, sign in
-
people often say that 𝐦𝐞𝐭𝐡𝐨𝐝 𝐨𝐯𝐞𝐫𝐫𝐢𝐝𝐢𝐧𝐠 𝐢𝐬 𝐫𝐮𝐧𝐭𝐢𝐦𝐞 𝐩𝐨𝐥𝐲𝐦𝐨𝐫𝐩𝐡𝐢𝐬𝐦 but that statement isn’t entirely accurate with c++ 𝗺𝗲𝘁𝗵𝗼𝗱 𝗼𝘃𝗲𝗿𝗿𝗶𝗱𝗶𝗻𝗴 simply means a child class provides its own implementation of a function that already exists in the parent class with the same signature, however, runtime polymorphism only happens when the method call is resolved based on the actual object type at runtime in c++(my fav), this only happens if the base class function is marked 𝐯𝐢𝐫𝐭𝐮𝐚𝐥 and the call is made through a 𝐛𝐚𝐬𝐞 𝐜𝐥𝐚𝐬𝐬 𝐩𝐨𝐢𝐧𝐭𝐞𝐫 𝐨𝐫 𝐫𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝘦.𝘨. 𝘉𝘢𝘴𝘦* 𝘱𝘵𝘳 = 𝘯𝘦𝘸 𝘋𝘦𝘳𝘪𝘷𝘦𝘥(); 𝘱𝘵𝘳->𝘥𝘪𝘴𝘱𝘭𝘢𝘺(); without virtual, the call is resolved at compile time, even if the object is actually 𝘋𝘦𝘳𝘪𝘷𝘦𝘥 but wow (𝘢𝘯𝘰𝘵𝘩𝘦𝘳 𝘳𝘦𝘢𝘴𝘰𝘯 𝘯𝘰𝘵 𝘵𝘰 𝘩𝘢𝘵𝘦 𝘫𝘢𝘷𝘢), 𝐦𝐞𝐭𝐡𝐨𝐝𝐬 𝐚𝐫𝐞 𝐯𝐢𝐫𝐭𝐮𝐚𝐥 𝐛𝐲 𝐝𝐞𝐟𝐚𝐮𝐥𝐭 in java (except static, final, and private), when a method is overridden, runtime polymorphism happens automatically :) java performs 𝐝𝐲𝐧𝐚𝐦𝐢𝐜 𝐝𝐢𝐬𝐩𝐚𝐭𝐜𝐡 𝐚𝐮𝐭𝐨𝐦𝐚𝐭𝐢𝐜𝐚𝐥𝐥𝐲, without needing a virtual keyword #CPP #Java #OOP #SystemDesign #Programming
To view or add a comment, sign in
-
Deep Dive into Java Fundamentals: Collections & Wrapper Classes ☕️ Today was all about strengthening the bedrock of my Java knowledge. I spent the day exploring the theoretical foundations of the Collection Interface, the Collections Utility Class, and Wrapper Classes. Understanding the "why" behind these concepts is just as important as the "how." Here’s a quick breakdown of my key takeaways: Collection vs. Collections: Clarified the distinction between the root interface for data structures and the utility class used for polymorphic algorithms (sorting, searching, etc.). Wrapper Classes: Diving into how Java wraps primitive data types into objects, enabling them to be used within the Collections Framework through Autoboxing and Unboxing. Data Structure Architecture: Looking at how different implementations (List, Set, Queue) handle data differently under the hood. Building a solid theoretical base is essential for writing efficient, scalable code. Back to the IDE to put these theories into practice! #Java #SoftwareDevelopment #JavaFullStack #CodingJourney #BackendDevelopment #ContinuousLearning
To view or add a comment, sign in
-
-
Day 3 of my Java Journey: Moving from Setup to Syntax! ➡️After yesterday’s deep dive into JDK 25 and how the JVM handles Bytecode, today was all about the building blocks of the language : Variables and Data Types. Coming from other languages, I really appreciate Java’s strongly-typed nature. Today I tackled: ●Primitive vs. Non-Primitive types: Understanding why int and double are handled differently than String. ●Type Casting: Navigating the nuances of widening and narrowing conversions (and avoiding those pesky loss-of-precision errors!). ●Memory Management: A peek into how primitives live on the Stack while objects head to the Heap. Java feels less like "just coding" and more like building a high-performance machine where every part has a specific place. Next stop: Operators and Control Flow! 🚀 #Java #LearningJourney #VVIT #BackendDevelopment #CodingLife #Day3
To view or add a comment, sign in
-
𝐉𝐚𝐯𝐚 𝐢𝐬 𝐚𝐥𝐰𝐚𝐲𝐬 𝐏𝐚𝐬𝐬-𝐛𝐲-𝐕𝐚𝐥𝐮𝐞. 𝐒𝐨 𝐰𝐡𝐲 𝐝𝐢𝐝 𝐦𝐲 𝐚𝐫𝐫𝐚𝐲 𝐜𝐡𝐚𝐧𝐠𝐞? 🤔 I wrote a simple program to test how Java handles method arguments. [1] Declared an array 𝐚𝐫𝐫 in 𝐦𝐚𝐢𝐧. [2] Passed it to a 𝐜𝐡𝐚𝐧𝐠𝐞(𝐢𝐧𝐭[] 𝐧𝐮𝐦𝐬) method. [3] Modified 𝐧𝐮𝐦𝐬[𝟎] AND 𝐧𝐮𝐦𝐬[𝟏] inside the method. [4] The original 𝐚𝐫𝐫 was updated. 𝐓𝐡𝐞 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠 𝐄𝐱𝐩𝐥𝐚𝐧𝐚𝐭𝐢𝐨𝐧: Java is strictly Pass-by-Value. When I passed 𝐚𝐫𝐫, I didn't pass the object itself. I passed a copy of the reference variable. [A] Both the original 𝐚𝐫𝐫 and the method parameter 𝐧𝐮𝐦𝐬 hold the same memory address (pointing to the same array object in the Heap). [B] So, when 𝐧𝐮𝐦𝐬 modifies the data, it modifies the shared object. Understanding Stack (references) vs. Heap (objects) is crucial for avoiding bugs. #Java #MemoryManagement #SoftwareEngineering #CodingInterviews #LearningInPublic #BackendDevelopment #DataStructures #Coding #WeMakeDevs #100DaysOfCode #IntelliJ
To view or add a comment, sign in
-
-
🚀 Day 12 – Understanding Arrays & Implementing Searching Techniques in Java Today’s focus was on mastering Arrays and implementing searching problems using methods in Core Java.this session helped me understand how data is stored, accessed, and processed efficiently using array structures — one of the most fundamental concepts in programming. Instead of writing everything inside main(), I practiced solving problems using properly defined methods to keep the logic clean and reusable. 🧩 What I Worked On: • Creating and initializing arrays • Taking array input from users • Traversing arrays using loops 🛠 Concepts Applied: ✔ Arrays (Declaration, Initialization, Traversal) ✔ Method Creation & Reusability ✔ Parameter Passing (Array as Argument) ✔ Return Statements ✔ Looping Constructs Key Learning Outcomes: • Understood how arrays manage multiple data values efficiently • Learned how to pass arrays to methods • Strengthened searching logic using structured programming • Improved code readability with modular design • Practiced writing clean, maintainable programs Arrays are a foundational step toward mastering Data Structures and Algorithms, and today strengthened that base significantly. Step by step building strong Core Java fundamentals before moving into advanced data structures and backend development #100DaysOfCode #Java #CoreJava #Arrays #DataStructures #ProblemSolving #JavaDeveloper #BackendDevelopment #CodingJourney
To view or add a comment, sign in
-
-
Day 3 of Java From Code to Memory 🧠💻 Today things got REAL. No more JVM theory. No more architecture talk. Today I learned how Java actually stores data inside memory. 👉 Variables. Sounds simple… but it’s powerful. When we write: int age = 21; Java doesn’t just “remember” 21. It: • Reserves space in memory • Decides how many bits to allocate • Stores the value in binary (0s & 1s) • Links it with the name age That moment when you realize… Programming = Managing Memory 🔥 Also understood: Java is statically typed. You must declare the data type first. No mixing random data. Strict but safe. Explored the 8 primitive data types: byte, short, int, long float, double char boolean And yes double wins over float for precision 👀 Biggest takeaway? Behind every simple line of code… there’s memory allocation, bits, and logic working silently. Day 3 and the foundation is getting stronger. We’re not just writing code anymore we’re understanding how machines think. Consistency > Motivation 🚀🔥 Special thanks to Aditya Tandon Sir & Rohit Negi Sir 🙌 #Java #CoreJava #LearningInPublic #Programming #Developers #BuildInPublic
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