Pass by Value and Pass by Reference Understanding how data is passed to functions is important for writing correct and predictable code. Pass by Value: In pass by value, a copy of the variable is passed to the function. Any changes made inside the function do not affect the original variable. Example: public class Main { static void changeValue(int x) { x = 20; } public static void main(String[] args) { int a = 10; changeValue(a); System.out.println(a); } } Output will be 10 because only a copy of the value is modified. Pass by Reference: In pass by reference, the reference (address) of the variable is passed, so changes inside the function affect the original object. In Java, objects are passed by value of reference. Example: class Data { int value; } public class Main { static void changeObject(Data d) { d.value = 20; } public static void main(String[] args) { Data obj = new Data(); obj.value = 10; changeObject(obj); System.out.println(obj.value); } } Key takeaway: Java is strictly pass by value. For objects, the value being passed is the reference, which allows object data to be modified. Understanding this concept is crucial for avoiding unexpected bugs in Java programs. #Java #PassByValue #PassByReference #ProgrammingBasics #DSA #SoftwareEngineering
Java Pass by Value and Reference Explained
More Relevant Posts
-
☕ 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗮 𝗿𝗲𝗰𝗼𝗿𝗱 𝗶𝗻 𝗝𝗮𝘃𝗮? 👇 A record is a special kind of class introduced in Java 16 (preview in 14/15) designed to model immutable data. Think of it as: 👉 “𝘈 𝘤𝘰𝘯𝘤𝘪𝘴𝘦, 𝘪𝘮𝘮𝘶𝘵𝘢𝘣𝘭𝘦 𝘿𝙏𝙊 𝘸𝘪𝘵𝘩 𝘦𝘷𝘦𝘳𝘺𝘵𝘩𝘪𝘯𝘨 𝘨𝘦𝘯𝘦𝘳𝘢𝘵𝘦𝘥 𝘧𝘰𝘳 𝘺𝘰𝘶.” 🧩 𝗪𝗵𝗮𝘁 𝗝𝗮𝘃𝗮 𝗴𝗲𝗻𝗲𝗿𝗮𝘁𝗲𝘀 𝗳𝗼𝗿 𝗮 𝗿𝗲𝗰𝗼𝗿𝗱 For this: 𝘱𝘶𝘣𝘭𝘪𝘤 𝘳𝘦𝘤𝘰𝘳𝘥 𝘜𝘴𝘦𝘳𝘋𝘛𝘖(𝘚𝘵𝘳𝘪𝘯𝘨 𝘯𝘢𝘮𝘦, 𝘪𝘯𝘵 𝘢𝘨𝘦) {} Java automatically creates: ✔ 𝘱𝘳𝘪𝘷𝘢𝘵𝘦 𝘧𝘪𝘯𝘢𝘭 𝘧𝘪𝘦𝘭𝘥𝘴 ✔ 𝘗𝘶𝘣𝘭𝘪𝘤 𝘤𝘢𝘯𝘰𝘯𝘪𝘤𝘢𝘭 𝘤𝘰𝘯𝘴𝘵𝘳𝘶𝘤𝘵𝘰𝘳 ✔ 𝘎𝘦𝘵𝘵𝘦𝘳-𝘭𝘪𝘬𝘦 𝘮𝘦𝘵𝘩𝘰𝘥𝘴: user.name() user.age() ✔ 𝘦𝘲𝘶𝘢𝘭𝘴() ✔ 𝘩𝘢𝘴𝘩𝘊𝘰𝘥𝘦() ✔ 𝘵𝘰𝘚𝘵𝘳𝘪𝘯𝘨() And it’s immutable by default. 🏆 𝗪𝗵𝗲𝗻 𝘀𝗵𝗼𝘂𝗹𝗱 𝘆𝗼𝘂 𝘂𝘀𝗲 𝗿𝗲𝗰𝗼𝗿𝗱𝘀? ✅ DTOs ✅ API responses ✅ Request objects ✅ Event messages ✅ Read-only models ✅ Config snapshots 🚫 𝗪𝗵𝗲𝗻 𝗡𝗢𝗧 𝘁𝗼 𝘂𝘀𝗲 𝗿𝗲𝗰𝗼𝗿𝗱𝘀? ❌ JPA Entities ❌ Mutable domain models ❌ Objects with lifecycle/state changes ❌ Objects requiring inheritance 🧠 𝗠𝗲𝗻𝘁𝗮𝗹 𝗺𝗼𝗱𝗲𝗹 (𝗲𝗮𝘀𝘆 𝘁𝗼 𝗿𝗲𝗺𝗲𝗺𝗯𝗲𝗿) 𝗖𝗹𝗮𝘀𝘀 → behavior + state 𝗥𝗲𝗰𝗼𝗿𝗱 → state only (data carrier) #java #developer #softwaredevelopment
To view or add a comment, sign in
-
-
Day 5 1️⃣ Typecasting in Java Typecasting is converting one data type into another. Sounds simple — but it can silently break logic if misunderstood. Two types: ✔ Implicit (Widening Casting) Smaller → Larger data type Handled automatically by Java. Examples: byte → short short → int int → long long → float char → int Important insight: long → float happens implicitly, but precision can be lost. Just because it compiles doesn’t mean it’s safe. That’s a blind spot many beginners miss. ✔ Explicit (Narrowing Casting) Larger → Smaller data type Requires manual instruction from the programmer. Examples: double → float long → int int → byte Here, Java forces you to take responsibility. If data is lost — that’s on you. That distinction matters in real-world applications where financial values, counters, and IDs are involved. 2️⃣ Increment & Decrement Operators Understanding the difference between: Pre-increment (++a) → increments first, then uses the value Post-increment (a++) → uses the value first, then increments Small syntax difference. Big logical impact inside loops and expressions. 3️⃣ Wrapping (Overflow Behavior) For example in byte: Maximum value: 127 If you add 1 → it wraps to -128 #FullStackDeveloper #JavaJourney #AppAcademy #BackendDevelopment #SoftwareEngineering #CodingLife
To view or add a comment, sign in
-
-
🚀 𝗪𝗵𝘆 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻 𝗙𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸 𝗜𝗻𝘀𝘁𝗲𝗮𝗱 𝗼𝗳 𝗔𝗿𝗿𝗮𝘆𝘀? Today I revised an important core Java concept. I reflected on an important question: 👉 Why do we prefer the Collection Framework over Arrays in real-world applications? 🔎 𝗔𝗿𝗿𝗮𝘆𝘀 — 𝗦𝗶𝗺𝗽𝗹𝗲 𝗯𝘂𝘁 𝗟𝗶𝗺𝗶𝘁𝗲𝗱 • Fixed size (defined at creation time) • No built-in utility methods • Manual resizing required • Limited flexibility for dynamic data • Homogeneous data Example: int[] arr = new int[3]; Once the size is fixed, it cannot grow. 🚀 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻 𝗙𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸 – 𝗙𝗹𝗲𝘅𝗶𝗯𝗹𝗲 & 𝗦𝗰𝗮𝗹𝗮𝗯𝗹𝗲 Java provides powerful data structures through the Collection Framework. Example: ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.add(40); // Automatically resizes ✅ 𝗪𝗵𝘆 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻𝘀 𝗔𝗿𝗲 𝗣𝗿𝗲𝗳𝗲𝗿𝗿𝗲𝗱 • Dynamic resizing • Built-in methods (add(), remove(), contains()) • Multiple data structures: • List → Ordered data • Set → Unique elements • Map → Key-value pairs • Queue → FIFO processing • Better scalability for real-world systems 💡 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 *Arrays are good for fixed-size data. *Collections are designed for real-world, evolving applications. Understanding why we use something makes us stronger developers — not just coders. #Java #CollectionFramework #Programming #DSA #LearningJourney
To view or add a comment, sign in
-
-
Solving LeetCode 876: Middle of the Linked List Finding the middle node of a linked list might sound simple, but it’s a great exercise in pointer manipulation and efficiency. Problem Statement Given the head of a singly linked list, return the middle node. If there are two middle nodes, return the second one. Example: Input: 1 → 2 → 3 → 4 → 5 → Output: 3 → 4 → 5 Input: 1 → 2 → 3 → 4 → 5 → 6 → Output: 4 → 5 → 6 My Java Solution class Solution { public ListNode middleNode(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; // move one step fast = fast.next.next; // move two steps } return slow; // slow ends at middle } } Key Insights Using two pointers (slow & fast) ensures we find the middle in a single pass. Time complexity: O(n) Space complexity: O(1) This technique is widely applicable in problems like cycle detection and linked list partitioning.
To view or add a comment, sign in
-
-
Aggregating nested data efficiently is a must-have Java skill. Use Streams with flatMap and Collectors.summingDouble to calculate total training cost cleanly. Link to video: https://lnkd.in/giFt8G_2
To view or add a comment, sign in
-
🚀 Day 8 – Core Java | Data Types from Memory Perspective Today’s session completely changed the way I look at data types. Instead of memorizing int, float, long, we understood why data types exist. 🔑 Key Learnings: ✔ RAM is a collection of bytes ✔ Bytes → Bits → Transistors ✔ Transistors understand only 0 & 1 ✔ Real-world data must be converted into binary before storage 💡 Big insight: Data types are not “types of data” Data types are converters that transform real-world data into 0s and 1s ✔ Understood primitive data types: Integer: byte, short, int, long Real numbers: float, double char, boolean ✔ Learned why multiple integer data types exist → Memory efficiency matters → Small choices scale massively in real applications ✔ Practical understanding of: Memory allocation Data ranges Why correct data type selection matters in industry 🎯 Interview takeaway: Don’t give textbook answers. Explain concepts from memory & system perspective. This session laid the foundation for thinking like a real developer, not just writing code 🚀 #CoreJava #DataTypes #MemoryManagement #JavaFundamentals #DeveloperMindset #LearningJourney
To view or add a comment, sign in
-
Hitting important stuff on Day 3 – Arrays & Basic Problem Solving in Java✅ 90 Days of Getting Better at Java : Today I focused on Arrays, one of the most used data structures in Java and a foundation for: - Handling collections of data - Backend request processing - Real-world business logic What I covered today: - Declaring & initializing arrays - Iterating using loops - Finding sum, max, and average - Writing clean, readable logic Here’s a simple program I practiced 👇 Java public class ArrayBasics { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; int sum = 0; int max = numbers[0]; for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; if (numbers[i] > max) { max = numbers[i]; } } double average = (double) sum / numbers.length; System.out.println("Sum = " + sum); System.out.println("Max = " + max); System.out.println("Average = " + average); } } 💡 Key takeaway: Simple data structures + clean logic = strong backend foundations. If you’re also revisiting Java fundamentals or preparing for backend roles, let’s grow together 🚀 #Java #DSA #BackendDevelopment #SpringBoot #100DaysOfCode #LearningInPublic
To view or add a comment, sign in
-
⚡ 𝗝𝗮𝘃𝗮 𝗥𝗲𝗰𝗼𝗿𝗱 – 𝗪𝗿𝗶𝘁𝗲 𝗟𝗲𝘀𝘀 𝗖𝗼𝗱𝗲, 𝗦𝘁𝗮𝘆 𝗖𝗹𝗲𝗮𝗻 Java 𝗥𝗲𝗰𝗼𝗿𝗱 is used to create data-only classes with very less code. Before records, we had to write: • constructor • getters • toString() • equals() • hashCode() Now Java does it automatically 💥 𝘱𝘶𝘣𝘭𝘪𝘤 𝘳𝘦𝘤𝘰𝘳𝘥 𝘜𝘴𝘦𝘳(𝘚𝘵𝘳𝘪𝘯𝘨 𝘯𝘢𝘮𝘦, 𝘪𝘯𝘵 𝘢𝘨𝘦) { } That’s it. Java creates everything for you. What Record Gives You? ✅ Immutable fields ✅ Constructor ✅ Getters ✅ equals & hashCode ✅ toString 𝗪𝗵𝗲𝗻 𝘁𝗼 𝗨𝘀𝗲 𝗥𝗲𝗰𝗼𝗿𝗱? ✔ DTO classes ✔ API response objects ✔ Immutable data ✔ Clean architecture 𝗪𝗵𝗲𝗻 𝗡𝗢𝗧 𝘁𝗼 𝗨𝘀𝗲? ❌ If object needs setters ❌ If business logic is heavy ❌ If inheritance is required 𝗜𝗻𝘁𝗿𝗼𝗱𝘂𝗰𝗲𝗱 𝗶𝗻 𝗝𝗮𝘃𝗮 𝟭𝟲 (𝘀𝘁𝗮𝗯𝗹𝗲) 🚀 💡 Record = Plain data + zero boilerplate 🔑 Keywords for Better Reach #Java #JavaRecords #JavaDeveloper #CleanCode #BackendDevelopment #Programming #SoftwareEngineering #JavaTips #ModernJava
To view or add a comment, sign in
-
Arrays are fixed. Real applications aren’t. That’s why Java introduced the 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻𝘀 𝗙𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸. Instead of managing size manually, you use dynamic data structures like: • 𝐀𝐫𝐫𝐚𝐲𝐋𝐢𝐬𝐭 • 𝐋𝐢𝐧𝐤𝐞𝐝𝐋𝐢𝐬𝐭 • 𝐇𝐚𝐬𝐡𝐒𝐞𝐭 • 𝐇𝐚𝐬𝐡𝐌𝐚𝐩 Example: List<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); Unlike arrays: • Collections grow dynamically • Provide built-in methods • Reduce manual memory handling But collections are not interchangeable. Choosing the wrong one affects: • Performance • Memory usage • Readability For example: • ArrayList → fast random access • LinkedList → efficient insert/delete • HashSet → unique elements • HashMap → key-value storage Today was about: Understanding why collections exist When to use List vs Set vs Map Writing scalable data logic Good developers don’t just store data. They choose the right structure for it. #Java #Collections #DataStructures #SoftwareEngineering #Programming #LearningInPublic
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