🔹 Pass by Value vs Pass by Reference in Java • In Java, everything is Pass by Value. • For primitive types (int, double, char, etc.), Java sends a copy of the actual value. • Any changes inside the method will NOT affect the original variable. • For objects, Java sends a copy of the reference (memory address). • Both original reference and method reference point to the same object in heap memory. • If you modify the object’s data inside the method, the change is reflected outside the method. • If you assign a new object inside the method, the original reference will NOT change. 🧠 Easy Recall Trick Primitive → Copy of Value → No Change Object → Copy of Address → Object Data Can Change #TapAcademy Java #CoreJava #ProgrammingConcepts #PassByValue #InterviewPreparation #JavaDeveloper #WomenInTech #LearningJourney
Java Pass by Value vs Pass by Reference Explained
More Relevant Posts
-
🚀 Java Method Arguments: Pass by Value vs Pass by Reference Ever wondered why Java behaves differently when passing primitives vs objects to methods? 🤔 This infographic breaks it down clearly: ✅ Pass by Value – When you pass a primitive, Java sends a copy of the value. The original variable stays unchanged. ✅ Objects in Java (Copy of Reference) – When you pass an object, Java sends a copy of the reference. You can modify the object’s data, but the reference itself cannot point to a new object. 💡 Why it matters: Prevent bugs when modifying data inside methods Understand how Java handles variables and objects under the hood 🔥 Fun Fact: Even objects are passed by value of reference! Java is always pass by value – whether it’s a primitive or an object. #Java #Programming #CodingTips #JavaDeveloper #SoftwareEngineering #LinkedInLearning #CodeBetter
To view or add a comment, sign in
-
-
📌 Comparable<T> vs Comparator<T> in Java — Know the Real Difference In Java, both Comparable and Comparator are functional interfaces used for object sorting — but they serve different purposes. 🔹 Comparable<T> Belongs to java.lang package Defines natural (default) sorting order Contains compareTo(T obj) method Sorting logic is written inside the same class Supports only one sorting sequence Used with: Arrays.sort(T obj[]) Collections.sort(List<E> list) 🔹 Comparator<T> Belongs to java.util package Defines custom sorting order Contains compare(T o1, T o2) method No need to modify the original class Supports multiple sorting sequences Used with: Arrays.sort(T obj[], Comparator<T> cmp) Collections.sort(List<E> list, Comparator<T> cmp) ==> Key Takeaway: Use Comparable when you want a single, natural ordering of objects. Use Comparator when you need flexible, multiple, or user-defined sorting logic. Understanding this difference is crucial for writing clean, scalable, and maintainable Java code. #Java #CoreJava #CollectionsFramework #Comparable #Comparator #JavaDeveloper #BackendDevelopment #Programming
To view or add a comment, sign in
-
-
Text Block simple example In this post under Java Core, I will introduce you to newly added "Text Block" feature. This feature was added as part of Java 15. Pre Java 15, if we have to declare a multiline string we used to declare it as shown below String result = "Since Java 15, text blocks are \n" + "available as a standard feature.\n" + "With Java 13 and 14, \n" + "we needed to enable it as a preview feature.";...
To view or add a comment, sign in
-
One of Java’s Most Powerful Concepts: Immutability - Many developers use String every day in Java… but few realize why it’s immutable. Example: String name = "Java"; name.concat(" Developer"); System.out.println(name); Output: Java Even though we tried to modify it, the value did not change. Why? Because String objects in Java are immutable. Whenever you modify a String, Java actually creates a new object instead of changing the existing one. Example: String name = "Java"; name = name.concat(" Developer"); System.out.println(name); Output: Java Developer Why Java designed it this way? Immutability helps with: 🔒 Security (important for class loading & networking) ⚡ Performance (String Pool optimization) 🧵 Thread Safety (no synchronization required) This small design decision is one of the reasons Java remains powerful for enterprise systems. ☕ Lesson: Great developers don't just write code… they understand why the language works the way it does. 💬 Question for developers: Which Java concept took you the longest time to understand? #Java #JavaDeveloper #Programming #BackendDevelopment #CleanCode #SoftwareEngineering
To view or add a comment, sign in
-
Simple code. Subtle behavior. Easy to misjudge. Two similar Java methods. Two completely different outputs. Case 1-> public static int tricky1() { int x = 10; try { return x; } finally { x = 50; } } Output: 10 Explanation: When return x executes, Java evaluates and stores the value (10) first. After that, the finally block runs. Even though x becomes 50 inside finally, it does not change the already prepared return value. Case 2-> public static int tricky2() { try { return 10; } finally { return 30; } } Output: 30 Explanation: If finally contains a return statement, it overrides any previous return from try or catch. This is why returning from finally is considered bad practice — it can override results and even hide exceptions. #Java #CoreJava
To view or add a comment, sign in
-
What is Garbage Collection in Java 🤔 Many developers use Java daily, but memory management often stays a mystery Here’s the simple truth Garbage Collection (GC) → JVM automatically removes objects that are no longer referenced Why it matters → Prevents memory leaks, keeps apps stable, avoids OutOfMemoryError String name = new String("Java"); name = null; // old object now eligible for GC Key Points ======= Object with no references → eligible for GC Eligible ≠ immediately deleted → JVM decides timing Most objects in Java apps are cleaned automatically → you focus on building features Rule of Thumb Stateless objects → no GC worries Heavy object creation → can trigger frequent GC, impacts performance Understanding GC = writing efficient, scalable Java code #Java #InterviewSeries #LearnJava #BackendDevelopment #JavaDeveloper #CodingTips #Programming #JavaInterviewPrep #TechLearning #DeveloperTips
To view or add a comment, sign in
-
What is Garbage Collection in Java 🤔 Many developers use Java daily, but memory management often stays a mystery Here’s the simple truth Garbage Collection (GC) → JVM automatically removes objects that are no longer referenced Why it matters → Prevents memory leaks, keeps apps stable, avoids OutOfMemoryError String name = new String("Java"); name = null; // old object now eligible for GC Key Points ======= Object with no references → eligible for GC Eligible ≠ immediately deleted → JVM decides timing Most objects in Java apps are cleaned automatically → you focus on building features Rule of Thumb Stateless objects → no GC worries Heavy object creation → can trigger frequent GC, impacts performance Understanding GC = writing efficient, scalable Java code #Java #InterviewSeries #LearnJava #BackendDevelopment #JavaDeveloper #CodingTips #Programming #JavaInterviewPrep #TechLearning #DeveloperTips
To view or add a comment, sign in
-
Day 11 - What I Learned In a Day(JAVA) Today I learned that Java variables are classified into two areas and understood their scope. 1️⃣ Global Area (Instance Variable) Declared inside the class but outside methods. Accessible by all methods inside the class. Scope: Entire class. Memory is created when the object is created. 2️⃣ Local Area (Local Variable) Declared inside a method, constructor, or block. Accessible only inside that method or block. Scope: Limited to that block only. Memory is created when the method runs. Types of Variables in Java (Based on Scope): 1️⃣ Local Variable Declared inside a method. Used only inside that method. Cannot be used outside the method. 2️⃣ Non-Static Variable (Instance Variable) Declared inside the class but outside methods. Belongs to the object. Each object has its own copy. 3️⃣Static Variable Declared using static keyword. Belongs to the class. One copy is shared by all objects. Three Important Statements in Java (Variables): 1️⃣ Declaration Creating a variable. You are telling Java the type and name of the variable. No value is given. Example: int a; 2️⃣ Initialization Giving a value to a variable. The variable must already be declared. Example: a = 10; 3️⃣ Declaration + Initialization Creating the variable and giving value at the same time. Example: int a = 10; Practiced 👇 #Java #Variables #Programming #CodingJourney
To view or add a comment, sign in
-
📌 wait(), notify(), notifyAll() in Java — Thread Communication In multithreading, sometimes threads need to coordinate with each other instead of just locking resources. Java provides three important methods for communication: • wait() • notify() • notifyAll() 1️⃣ wait() • Causes the current thread to release the lock • Moves the thread into waiting state • Must be called inside synchronized block 2️⃣ notify() • Wakes up one waiting thread • Does NOT release the lock immediately • The awakened thread waits until lock is available 3️⃣ notifyAll() • Wakes up all waiting threads • Only one will acquire the lock next 4️⃣ Important Rules • These methods belong to Object class • Must be called inside synchronized context • Used for inter-thread coordination 5️⃣ Why They Are Needed Used in scenarios like: • Producer–Consumer problem • Task scheduling • Resource pooling 🧠 Key Takeaway synchronized controls access. wait/notify control communication. Together, they enable proper coordination between threads in Java. #Java #Multithreading #Concurrency #ThreadCommunication #CoreJava
To view or add a comment, sign in
-
📌 wait(), notify(), notifyAll() in Java – Thread Communication In multithreading, sometimes threads need to coordinate with each other instead of just locking resources. Java provides three important methods for communication: • wait() • notify() • notifyAll() 1️⃣ wait() • Causes the current thread to release the lock • Moves the thread into waiting state • Must be called inside synchronized block 2️⃣ notify() • Wakes up one waiting thread • Does NOT release the lock immediately • The awakened thread waits until lock is available 3️⃣ notifyAll() • Wakes up all waiting threads • Only one will acquire the lock next 4️⃣ Important Rules • These methods belong to Object class • Must be called inside synchronized context • Used for inter-thread coordination 5️⃣ Why They Are Needed Used in scenarios like: • Producer–Consumer problem • Task scheduling • Resource pooling 🧠 Key Takeaway synchronized controls access. wait/notify control communication. Together, they enable proper coordination between threads in Java. #Java #Multithreading #Concurrency #ThreadCommunication #CoreJava
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