💡 Question: What will be the output of this Java code? public class Test { public static void main(String[] args) { Integer a = 100; Integer b = 100; Integer c = 200; Integer d = 200; System.out.println(a == b); System.out.println(c == d); } } 🤔 Think before answering. Don’t rush. Most people say the same output for both lines… and that’s where the trap is. 👇 Actual Output true false 🔥 Why this happens? (The twist) Java caches Integer objects in the range -128 to 127 a and b point to the same cached object c and d are different objects in heap memory == compares references, not values 👉 If this surprised you, imagine this bug hiding in a production system 😅 📌 Lesson for Interviews & Real Code Always use .equals() for wrapper classes Understand autoboxing, caching, and JVM internals Core Java ≠ basic Java 💬 Comment your answer before reading the explanation ❤️ Like if Java still manages to trick you after years 🔁 Repost to challenge your Java network #Java #CoreJava #JavaInterview #JVM #Programming #SoftwareEngineering #JavaDeveloper #LinkedInLearning
Java Integer caching: true vs false
More Relevant Posts
-
🚀 𝐇𝐨𝐰 𝐚 𝐉𝐚𝐯𝐚 𝐏𝐫𝐨𝐠𝐫𝐚𝐦 𝐑𝐮𝐧𝐬 – 𝐒𝐭𝐞𝐩 𝐛𝐲 𝐒𝐭𝐞𝐩 Ever wondered what actually happens when you run a Java program? Here’s the complete flow from .java file to output 👇 🔹 1️⃣ 𝑾𝒓𝒊𝒕𝒊𝒏𝒈 𝒕𝒉𝒆 𝑷𝒓𝒐𝒈𝒓𝒂𝒎 We write Java code in a file with .java extension. This code is human-readable but not understood by the machine. 🔹 2️⃣ 𝑪𝒐𝒎𝒑𝒊𝒍𝒂𝒕𝒊𝒐𝒏 (𝒋𝒂𝒗𝒂𝒄) The Java Compiler checks the syntax and converts the .java file into a .class file. ✔️ .class file contains bytecode ✔️ Bytecode is platform-independent 🔹 3️⃣ 𝑪𝒍𝒂𝒔𝒔 𝑳𝒐𝒂𝒅𝒆𝒓 The Class Loader loads the .class file into memory. It ensures classes are loaded only once and in the correct order. 🔹 4️⃣ 𝑩𝒚𝒕𝒆𝒄𝒐𝒅𝒆 𝑽𝒆𝒓𝒊𝒇𝒊𝒄𝒂𝒕𝒊𝒐𝒏 Before execution, JVM verifies bytecode for: ✔️ Security ✔️ Memory access ✔️ Illegal code This makes Java safe and reliable. 🔹 5️⃣ 𝑬𝒙𝒆𝒄𝒖𝒕𝒊𝒐𝒏 𝒃𝒚 𝑱𝑽𝑴 The Execution Engine runs the bytecode: Interpreter executes line by line 🔹 6️⃣ 𝑶𝒖𝒕𝒑𝒖𝒕 The JVM interacts with the OS and hardware, and finally you see the output. 📌 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆: ➤ Java is compiled once and run anywhere ➤ .class file + JVM = Platform Independence #Java #CoreJava #JVM #JavaBasics #Compilation #LearningJourney #JavaDeveloper
To view or add a comment, sign in
-
-
🌱 My Spring Journey : Understanding Spring Contexts & the .class Property in Java Difference between FileSystemXmlApplicationContext and ClassPathXmlApplicationContext : 🔹 FileSystemXmlApplicationContext → Creates the IOC container by locating the Spring bean configuration file from the specified path of the file system. → We can provide either a relative path or an absolute path. 🔹 ClassPathXmlApplicationContext : → Creates the IOC container by locating the given Spring bean configuration file from directories or JAR files added to the classpath. class property / .class property : 🔹 It is useful to get the object of java.lang.Class having the metadata of a given class or interface. Example : Class c1 = System.class; 🔹 Here, class is the static property of type java.lang.Class in the System class. 🔹 c1 is not an object of the System class, but an object of java.lang.Class that holds detailed metadata about the System class. Java Compiler adds multiple things to every class during compilation. Some of them are : 🔹 Adds java.lang.Object as the super class if the class doesn’t explicitly extend any class. 🔹 Adds a non-parameterized constructor if no constructor is defined. 🔹 Adds a static class property of type java.lang.Class, and more. #Java #SpringFramework #CoreJava #IOC #DependencyInjection #JavaDeveloper #BackendDevelopment #LearningJourney
To view or add a comment, sign in
-
final vs finally vs finalize in Java These three look similar, but they are very different. This is a classic interview question. 🔹 final Used to restrict modification. final variable → value cannot change final method → cannot be overridden final class → cannot be inherited 👉 Used at compile time 🔹 finally Used in exception handling. Always executes Runs whether exception occurs or not Used for cleanup (closing files, DB connections) 👉 Part of try-catch-finally 🔹 finalize() Used by Garbage Collector. Called before object is destroyed Not reliable Rarely used in modern Java 👉 Managed by JVM, not developers 🧠 Quick Tip Control code → final Handle cleanup → finally JVM memory cleanup → finalize() Understanding small differences like this shows strong Core Java fundamentals. 🚀 #Java #CoreJava #JavaInterview #ProgrammingConcepts #BackendDevelopment #JavaDeveloper #LearningInPublic #DevelopersCommunity
To view or add a comment, sign in
-
-
Today I learned how different types of Strings are actually used in real-world Java applications. While working on a simple Order Service–style example, I clearly understood: 1. String (Immutable) Used for fixed and sensitive data like order status and customer names. 2. StringBuilder (Mutable, Fast) Used to dynamically build responses such as order summaries without creating multiple objects. 3. StringBuffer (Mutable + Thread-safe) Used for audit/logging scenarios where multiple threads may be involved. This helped me understand that Strings are not just about syntax — choosing the right type impacts performance, memory, and scalability in backend systems. Learning Java step by step and applying concepts with an industry mindset. More learning, more practice . #Java #JavaLearning #BackendDevelopment #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
𝐌𝐨𝐬𝐭 𝐉𝐚𝐯𝐚 𝐝𝐞𝐯𝐞𝐥𝐨𝐩𝐞𝐫𝐬 𝐮𝐧𝐝𝐞𝐫𝐬𝐭𝐚𝐧𝐝 𝐒𝐭𝐫𝐢𝐧𝐠 𝐚𝐥𝐥 𝐰𝐫𝐨𝐧𝐠… Strings in Java are immutable, and that’s why the String Constant Pool works efficiently. Understanding this is essential for memory management, thread safety, and security. 𝐒𝐓𝐑𝐈𝐍𝐆 𝐂𝐎𝐍𝐒𝐓𝐀𝐍𝐓 𝐏𝐎𝐎𝐋 – 𝐖𝐇𝐀𝐓 Special memory area storing unique String literals Avoids duplicate objects → saves memory Example: String s1 = "Java"; String s2 = "Java"; → Only one object is created and shared 𝐒𝐓𝐑𝐈𝐍𝐆 𝐂𝐎𝐍𝐒𝐓𝐀𝐍𝐓 𝐏𝐎𝐎𝐋 – 𝐖𝐇𝐄𝐑𝐄 -->>Located inside Heap Memory (not Stack) -->>Interview detail: Before Java 7 → PermGen From Java 7 onwards → Heap Memory 𝐖𝐇𝐘 𝐒𝐓𝐑𝐈𝐍𝐆 𝐈𝐒 𝐈𝐌𝐌𝐔𝐓𝐀𝐁𝐋𝐄 Allows safe sharing from the String Constant Pool Thread-safe by design Improves security (used in URLs, class loaders, file paths) Reliable HashMap keys Immutability ensures changes in one reference don’t affect others. 𝐒𝐓𝐑𝐈𝐍𝐆 𝐋𝐈𝐓𝐄𝐑𝐀𝐋 𝐕𝐒 𝐍𝐄𝐖 𝐒𝐓𝐑𝐈𝐍𝐆 "Java" → Stored in String Constant Pool new String("Java") → Creates a new object in Heap equals() → true == → false 𝐎𝐍𝐄-𝐋𝐈𝐍𝐄 𝐈𝐍𝐓𝐄𝐑𝐕𝐈𝐄𝐖 𝐀𝐍𝐒𝐖𝐄𝐑 String Constant Pool is a special area inside Heap memory that stores unique String literals, enabled by String immutability. Save this if Core Java fundamentals matter #Java #CoreJava #String #StringConstantPool #flm #JavaInterview #Programming #Learning #Growth #Software
To view or add a comment, sign in
-
🔹 Ways to Create Strings in Java & Duplicate Behavior 🔹 1️⃣ Using String Literal: String str = "Java"; Stored in the String Constant Pool (SCP) inside the Heap Duplicate literals are NOT allowed – JVM reuses the same object for identical strings Memory efficient Preferred for most use cases 🔹 2️⃣ Using new Keyword: String str = new String("Java"); Creates a new object in Heap memory The literal "Java" also exists in SCP Duplicates ARE allowed – each new creates a separate object Not memory-efficient if overused 🔹 3️⃣ Using Character Array: char chars[] = {'J','a','v','a'}; String str = new String(chars); Converts a char array into a String object Stored in Heap memory Duplicates ARE allowed – each conversion creates a new object Useful for dynamically building Strings from characters 🔹 Key Insight SCP (String literals) → duplicates NOT allowed, reused for efficiency Heap (new / char array) → duplicates allowed, each object is unique #Java #CoreJava #StringsInJava #StringMemory #Heap #StringConstantPool #Programming #LinkedInLearning
To view or add a comment, sign in
-
-
String, StringBuilder, StringBuffer in Java - when to use what String:- • Immutable • Every modification creates a new object • Safe to use when data does not change StringBuilder:- • Mutable • Not thread-safe • Best choice for string manipulation in single-threaded or local scope. StringBuffer:- • Mutable • Thread-safe (synchronized) • Slower due to synchronization overhead. What most developers do wrong: • Use String inside loops for concatenation. • Use StringBuffer assuming it is always better. • Ignore performance impact of immutability. Key takeaway: Use String for constants, StringBuilder for performance, and StringBuffer only when thread safety is truly required. #Java #CoreJava
To view or add a comment, sign in
-
🔹 What is a String? In Java, a String is an object that represents a sequence of characters enclosed in double quotes (" "). Strings are immutable, meaning their value cannot be changed once created, and they use UTF-16 encoding internally. 🔹 Types of Strings Java provides different types of classes to work with strings based on mutability and thread-safety: String – Immutable and thread-safe. StringBuffer – Mutable and thread-safe; suitable for multi-threaded environments. StringBuilder – Mutable and not thread-safe; ideal for single-threaded programs. 🔹 Why We Use Strings Strings are essential for handling text data in applications: User input and output Authentication and authorization (username/password) API requests and responses Logging and messaging File and data processing 🔹 Advantages of Strings Immutable → ensures data integrity and security Thread-safe → safe in concurrent programs Memory-efficient → uses String Constant Pool for literals Rich API → provides powerful built-in methods for manipulation #Java #CoreJava #StringsInJava #JavaBasics #Programming #LinkedInLearning #JavaInterview
To view or add a comment, sign in
-
-
📌 📍 𝐃𝐚𝐲 𝟑 | 𝐀𝐈-𝐏𝐨𝐰𝐞𝐫𝐞𝐝 𝐉𝐚𝐯𝐚 𝐅𝐮𝐥𝐥 𝐒𝐭𝐚𝐜𝐤 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠 Today I focused on understanding the core execution flow of Java — JDK, JRE, and JVM ☕💻 🔹 𝐓𝐨𝐩𝐢𝐜𝐬 𝐂𝐨𝐯𝐞𝐫𝐞𝐝 ▪ JDK, JRE, JVM ▪ Java First Application ▪ JVM internals & memory basics 🧪 𝐉𝐚𝐯𝐚 𝐄𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 𝐅𝐥𝐨𝐰 Test.java → javac Test.java → Test.class ➡ Executed using java Test ➡ Output: Hello, Java! 🎉 🛠 𝐉𝐃𝐊 (𝐉𝐚𝐯𝐚 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐦𝐞𝐧𝐭 𝐊𝐢𝐭) • Used to develop & compile Java programs • Contains JRE + developer tools (javac, debugger) 📌 JDK = JRE + Dev Tools 🏠 𝐉𝐑𝐄 (𝐉𝐚𝐯𝐚 𝐑𝐮𝐧𝐭𝐢𝐦𝐞 𝐄𝐧𝐯𝐢𝐫𝐨𝐧𝐦𝐞𝐧𝐭) • Used to run Java programs • Contains JVM + core libraries • ❌ No compiler 📌 JRE = JVM + Libraries 🔥 𝐉𝐕𝐌 (𝐉𝐚𝐯𝐚 𝐕𝐢𝐫𝐭𝐮𝐚𝐥 𝐌𝐚𝐜𝐡𝐢𝐧𝐞) 🧠 Brain of Java • Loads & verifies .class files • Manages memory (Heap, Stack, Method Area) • Executes bytecode ⚙️ 𝐖𝐡𝐲 𝐉𝐚𝐯𝐚 𝐢𝐬 𝐇𝐲𝐛𝐫𝐢𝐝 ✔ Compiled → Bytecode ✔ Interpreted / JIT-compiled at runtime 👉 JVM does both interpreting & compiling 🍳 𝐊𝐢𝐭𝐜𝐡𝐞𝐧 𝐀𝐧𝐚𝐥𝐨𝐠𝐲 • JDK = Kitchen + tools + ingredients • JRE = Stove + utensils • JVM = Stove that actually heats the food 🔥 🎯 𝐊𝐞𝐲 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 JVM makes Java platform-independent, secure, and high-performance — that’s why Java is still a top industry language 🚀 #Java #JavaFullStack #JVM #JDK #JRE #LearningJourney #SoftwareEngineering #TechLearning
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