đ đšđ»đ±đČđżđđđźđ»đ±đ¶đ»đŽ đđ”đČ đđźđđź đđ¶đđ đđ»đđČđżđłđźđ°đČ â đ§đ”đČ đđČđźđżđ đŒđł đąđżđ±đČđżđČđ± đđŒđčđčđČđ°đđ¶đŒđ»đ In Javaâs đđŒđčđčđČđ°đđ¶đŒđ» đđżđźđșđČđđŒđżđž, the List interface represents an ordered, index-based collection that allows duplicate elements. Itâs like your digital to-do list â you can add, remove, or access any task by its position. đđČđ đ¶đșđœđčđČđșđČđ»đđźđđ¶đŒđ»đ: đđżđżđźđđđ¶đđ â đ„đșđŻđąđźđȘđ€ đąđłđłđąđșđŽ, đšđłđŠđąđ” đ§đ°đł đ§đąđŽđ” đłđąđŻđ„đ°đź đąđ€đ€đŠđŽđŽ đđ¶đ»đžđČđ±đđ¶đđ â đ±đŠđłđ§đŠđ€đ” đ§đ°đł đ§đłđŠđČđ¶đŠđŻđ” đȘđŻđŽđŠđłđ”đȘđ°đŻđŽ đąđŻđ„ đ„đŠđđŠđ”đȘđ°đŻđŽ đ©đČđ°đđŒđż â đđŠđšđąđ€đș đ”đ©đłđŠđąđ„-đŽđąđ§đŠ đ·đŠđłđŽđȘđ°đŻ (đŻđ°đ” đ€đ°đźđźđ°đŻđđș đ¶đŽđŠđ„ đ”đ°đ„đąđș) đŠđđźđ°đž â đ€đđąđŽđŽđȘđ€ đđđđ đ„đąđ”đą đŽđ”đłđ¶đ€đ”đ¶đłđŠ đŁđ¶đȘđđ” đ°đŻ đđŠđ€đ”đ°đł Each serves a specific purpose depending on whether you value speed, memory efficiency, or thread safety. đđ đźđșđœđčđČ đđŒđ±đČ import java.util.*; public class ListExamples {   public static void main(String[] args) {     // đđżđżđźđđđ¶đđ đđ đźđșđœđčđČ Â Â Â Â List<String> arrayList = new ArrayList<>();     arrayList.add("Java");     arrayList.add("Python");     arrayList.add("C++");     arrayList.add("Python"); // duplicates allowed     System.out.println("ArrayList: " + arrayList);     // đđ¶đ»đžđČđ±đđ¶đđ đđ đźđșđœđčđČ Â Â Â Â List<String> linkedList = new LinkedList<>();     linkedList.add("Spring");     linkedList.add("Hibernate");     linkedList.addFirst("Java EE");     System.out.println("LinkedList: " + linkedList);     // đ©đČđ°đđŒđż đđ đźđșđœđčđČ Â Â Â Â Vector<Integer> vector = new Vector<>();     vector.add(10);     vector.add(20);     vector.add(30);     System.out.println("Vector: " + vector);     // đŠđđźđ°đž đđ đźđșđœđčđČ Â Â Â Â Stack<String> stack = new Stack<>();     stack.push("Apple");     stack.push("Banana");     stack.push("Cherry");     System.out.println("Stack before pop: " + stack);     stack.pop();     System.out.println("Stack after pop: " + stack);   } } đąđđđœđđ: đđłđłđąđșđđȘđŽđ”: [đđąđ·đą, đđșđ”đ©đ°đŻ, đ++, đđșđ”đ©đ°đŻ] đđȘđŻđŹđŠđ„đđȘđŽđ”: [đđąđ·đą đđ, đđ±đłđȘđŻđš, đđȘđŁđŠđłđŻđąđ”đŠ] đđŠđ€đ”đ°đł: [10, 20, 30] đđ”đąđ€đŹ đŁđŠđ§đ°đłđŠ đ±đ°đ±: [đđ±đ±đđŠ, đđąđŻđąđŻđą, đđ©đŠđłđłđș] đđ”đąđ€đŹ đąđ§đ”đŠđł đ±đ°đ±: [đđ±đ±đđŠ, đđąđŻđąđŻđą] đĄ In real-world applications, Lists manage user records, logs, orders, and much more. Choosing the right implementation can make a big difference in performance. #Java #OOP #CollectionFramework #ArrayList #LinkedList #Stack #Vector #JavaDeveloper
Understanding Java List Interface: ArrayList, LinkedList, Vector, Stack
More Relevant Posts
-
đ đ đźđđđČđżđ¶đ»đŽ đđźđđź đ đźđœ đđ»đđČđżđłđźđ°đČ: đđźđđ”đ đźđœ đđ đđ¶đ»đžđČđ±đđźđđ”đ đźđœ đđ đ§đżđČđČđ đźđœ đđ đđźđđ”đđźđŻđčđČ If youâve ever worked with key-value data in Java, the Map Interface is your go-to toolkit. It maps unique keys to specific values â just like storing names and phone numbers in your contacts list đ±. đđČđżđČâđ đź đŸđđ¶đ°đž đŻđżđČđźđžđ±đŒđđ»: đč đđźđđ”đ đźđœ: đđąđŽđ”đŠđŽđ”, đ¶đŻđ°đłđ„đŠđłđŠđ„, đąđđđ°đžđŽ đ°đŻđŠ đŻđ¶đđ đŹđŠđș â đ±đŠđłđ§đŠđ€đ” đ§đ°đł đŻđ°đŻ-đ”đ©đłđŠđąđ„đŠđ„ đąđ±đ±đđȘđ€đąđ”đȘđ°đŻđŽ. đč đđ¶đ»đžđČđ±đđźđđ”đ đźđœ: đđąđȘđŻđ”đąđȘđŻđŽ đȘđŻđŽđŠđłđ”đȘđ°đŻ đ°đłđ„đŠđł â đšđłđŠđąđ” đžđ©đŠđŻ đ°đłđ„đŠđł đźđąđ”đ”đŠđłđŽ. đč đ§đżđČđČđ đźđœ: đđ”đ°đłđŠđŽ đŹđŠđșđŽ đȘđŻ đŽđ°đłđ”đŠđ„ (đąđŽđ€đŠđŻđ„đȘđŻđš) đ°đłđ„đŠđł â đȘđ„đŠđąđ đ§đ°đł đ°đłđ„đŠđłđŠđ„ đ„đąđ”đą đ°đł đłđąđŻđšđŠ đČđ¶đŠđłđȘđŠđŽ. đč đđźđđ”đđźđŻđčđČ: đđ©đłđŠđąđ„-đŽđąđ§đŠ đŁđ¶đ” đ°đđ„-đŽđ€đ©đ°đ°đ â đłđŠđ±đđąđ€đŠđ„ đźđ°đŽđ”đđș đŁđș đđ°đŻđ€đ¶đłđłđŠđŻđ”đđąđŽđ©đđąđ±. đđ đźđșđœđčđČ: import java.util.*; public class MapExample {   public static void main(String[] args) {     // đïžâŁ đđźđđ”đ đźđœ - đšđ»đŒđżđ±đČđżđČđ±, đźđčđčđŒđđ đŒđ»đČ đ»đđčđč đžđČđ     Map<Integer, String> hashMap = new HashMap<>();     hashMap.put(3, "Sachin");     hashMap.put(1, "Amit");     hashMap.put(2, "Ravi");     hashMap.put(null, "Unknown"); // Allowed     System.out.println("HashMap: " + hashMap);     // đźïžâŁ đđ¶đ»đžđČđ±đđźđđ”đ đźđœ - đ đźđ¶đ»đđźđ¶đ»đ đ¶đ»đđČđżđđ¶đŒđ» đŒđżđ±đČđż     Map<Integer, String> linkedHashMap = new LinkedHashMap<>();     linkedHashMap.put(3, "Sachin");     linkedHashMap.put(1, "Amit");     linkedHashMap.put(2, "Ravi");     System.out.println("LinkedHashMap: " + linkedHashMap);     // đŻïžâŁ đ§đżđČđČđ đźđœ - đŠđŒđżđđČđ± đŻđ đžđČđ, đ»đŒ đ»đđčđč đźđčđčđŒđđČđ±     Map<Integer, String> treeMap = new TreeMap<>();     treeMap.put(3, "Sachin");     treeMap.put(1, "Amit");     treeMap.put(2, "Ravi");     System.out.println("TreeMap: " + treeMap);     // đ°ïžâŁ đđźđđ”đđźđŻđčđČ - đ§đ”đżđČđźđ±-đđźđłđČ đŻđđ đ»đŒ đ»đđčđč đźđčđčđŒđđČđ±     Map<Integer, String> hashtable = new Hashtable<>();     hashtable.put(3, "Sachin");     hashtable.put(1, "Amit");     hashtable.put(2, "Ravi");     System.out.println("Hashtable: " + hashtable);     // đ đđđČđżđźđđ¶đ»đŽ đŒđđČđż đź đ đźđœ     for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {       System.out.println(entry.getKey() + " -> " + entry.getValue());     }   } } đĄ đ§đ¶đœ: When you hear âđłđźđđ đčđŒđŒđžđđœâ, think HashMap. When you need đŒđżđ±đČđż, think LinkedHashMap or TreeMap. When you need đđ”đżđČđźđ± đđźđłđČđđ, go for ConcurrentHashMap. Understanding these differences helps you write cleaner, faster, and more scalable code đȘ #Java #HashMap #CollectionsFramework #LinkedHashMap #TreeMap #JavaInterviewPrep
To view or add a comment, sign in
-
⥠10+ Approaches to Find Max or Min Value in Java 8 Streams â Master It Like a Pro đȘ Working with Java 8 Streams? Here are 10 powerful and elegant ways to find the maximum or minimum value from a list â all clean, modern, and interview-ready đ đ§© Sample Data List<Integer> myList = Arrays.asList(10, 15, 8, 49, 25, 98, 98, 32, 15); đ„ Find Maximum Value â 10 Approaches List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15); 1ïžâŁ Using instance comparator (compareTo on elements) myList.stream().max(Integer::compareTo).get(); 2ïžâŁ Using static comparator (Integer.compare) myList.stream().max(Integer::compare).get(); 3ïžâŁ Using IntStream for primitive comparison myList.stream().mapToInt(Integer::intValue).max().getAsInt(); 4ïžâŁ Using reduce() with Integer::max myList.stream().reduce(Integer::max).get(); 5ïžâŁ Using Collectors.summarizingInt() for all stats myList.stream().collect(Collectors.summarizingInt(i -> i)).getMax(); 6ïžâŁ Using Comparator.comparingInt() (good for objects too) myList.stream().max(Comparator.comparingInt(Integer::intValue)).get(); 7ïžâŁ Using Comparator.naturalOrder() for clean syntax myList.stream().max(Comparator.naturalOrder()).get(); 8ïžâŁ Using Comparable::compareTo (generic Comparable) myList.stream().max(Comparable::compareTo).get(); 9ïžâŁ Using custom lambda comparator (manual compare logic) myList.stream().max((a,b) -> a>b ? 1 : (a<b ? -1 : 0)).get(); đ Using summaryStatistics() for one-line numeric max myList.stream().mapToInt(Integer::intValue).summaryStatistics().getMax(); đ Using sorted() + skip() to pick last element myList.stream().sorted().skip(myList.size()-1).findFirst().get(); đ Same Approaches for Find Minimum Value List<Integer> myList = Arrays.asList(10, 15, 8, 49, 25, 98, 98, 32, 15); 1ïžâŁ myList.stream().min(Integer::compareTo).get(); 2ïžâŁ myList.stream().min(Integer::compare).get(); 3ïžâŁ myList.stream().mapToInt(Integer::intValue).min().getAsInt(); 4ïžâŁ myList.stream().reduce(Integer::min).get(); 5ïžâŁ myList.stream().collect(Collectors.summarizingInt(i -> i)).getMin(); 6ïžâŁ myList.stream().min(Comparator.comparingInt(Integer::intValue)).get(); 7ïžâŁ myList.stream().min(Comparator.naturalOrder()).get(); 8ïžâŁ myList.stream().min(Comparable::compareTo).get(); 9ïžâŁ myList.stream().min((a, b) -> a > b ? 1 : (a < b ? -1 : 0)).get(); đ myList.stream().mapToInt(Integer::intValue).summaryStatistics().getMin(); đ myList.stream().sorted().findFirst().get(); đĄ Quick Summary for Efficiency đ§ For numeric lists â mapToInt().max() or summaryStatistics() đ For objects â Comparator.comparing() đ For analytics â Collectors.summarizingInt() đȘ For readability â reduce() or naturalOrder() âïž For demos â sorted().skip() (less efficient) đ Follow me for more Java 8, Streams, and interview-ready code tips! đ #Java #Java8 #Streams #CodingTips #FunctionalProgramming #InterviewPreparation #CleanCode #Developers #LinkedInLearning #CodeNewbie
To view or add a comment, sign in
-
Hello Everyone, Todays topic is struct, interface Comparing with java, we have the extends for extending the classes and implements for interfaces so similar to that, for denoting the function belongs to this struct, we mention the class name first and then the function name As always i have prepared a cheat sheet for your reference, just a bit of scrolling is required to go through it once. // You can edit this code! // Click here and start typing. package main import "fmt" func main() { e := Employee{ Person: Person{ Name: "Suraj", Age: 25, }, EmployeeID: "E1010", Department: "Finance", } fmt.Println("Name : ", e.Person.Name) //Create a Shape interface with Area() method r := Rectangle{width: 4, height: 5} fmt.Println("Area of the rectangle is : ", r.Area()) //Print full Name with first name and last name struct name := Name{firstName: "Raju", middleName: "", lastName: "Bhai"} fmt.Println("FullName : ", name.fullName()) // Add a method AgeInYears() to Name fmt.Printf("Age of %v is : %v", name.fullName(), name.AgeInYears()) } type Person struct { Name string Age int } type Employee struct { Person EmployeeID string Department string } type Shape interface { Area() float64 } type Rectangle struct { width float64 height float64 } func (r Rectangle) Area() float64 { return r.width * r.height } var personDetails = map[string]int{"Ram": 23, "Raju": 44, "Sharma": 34} type Name struct { firstName string middleName string lastName  string } func (n Name) AgeInYears() int { if age, ok := personDetails[n.firstName]; ok { return age } return -1 } func (n Name) fullName() string { //denoting the fullName function belongs to the Name struct return n.firstName + " " + n.middleName + " " + n.lastName } #Golang #GolangForCoding #StructAndInterfacesInGolang #Examples #Struct #Interfaces Golang Golang Development As you are shifting towards golang from java or python, u may thing that we can use the Same class name and all that here in Golang also we can import the struct from other package and mention that package_name.struct_name If any doubts or any topics to be taken further, you can comment it out or mail me out : surajece2020@gmail.com Feel free to reach out, Happy learning! đ
To view or add a comment, sign in
-
Strings methods in Java 1ïžâŁ length() Definition: Returns the number of characters in a string (including spaces). đĄ Why it matters: Useful for checking input length (like passwords, usernames) or controlling loops. Example: String name = "Rakshitha"; System.out.println(name.length()); // Output: 9 2ïžâŁ charAt() Definition: Returns the character at a given index (index starts from 0). đĄ Why it matters: Helps to access or check specific characters, like the first letter of a name. Example: String word = "Java"; System.out.println(word.charAt(2)); // Output: v 3ïžâŁ toUpperCase() / toLowerCase() Definition: Converts all letters in a string to uppercase or lowercase. đĄ Why it matters: Useful for ignoring case in comparisons or displaying consistent text. Example: String text = "Java"; System.out.println(text.toUpperCase()); // JAVA System.out.println(text.toLowerCase()); // java 4ïžâŁ equals() / equalsIgnoreCase() Definition: equals() compares two strings exactly, while equalsIgnoreCase() ignores case differences. đĄ Why it matters: Used to compare user inputs like login IDs or names, case-sensitive or not. Example: String a = "Java"; String b = "java"; System.out.println(a.equals(b)); // false System.out.println(a.equalsIgnoreCase(b)); // true 5ïžâŁ contains() Definition: Checks if a string contains a certain sequence of characters. đĄ Why it matters: Used to search or validate text (like checking if an email contains â@â). Example: String email = "rakshitha@gmail.com"; System.out.println(email.contains("@gmail.com")); // true Awesome đ Here are the next 5 common String methods in Java â explained simply with definition, why it matters, and example đ 6ïžâŁ substring() Definition: Extracts a part of the string between given start and end indexes. đĄ Why it matters: Used to get a specific portion of text, like extracting username from an email. Example: String word = "JavaDeveloper"; System.out.println(word.substring(0, 4)); // Output: Java 7ïžâŁ replace() Definition: Replaces all occurrences of a character or substring with another value. đĄ Why it matters: Useful for correcting text, filtering data, or formatting user input. Example: String text = "I love Java"; System.out.println(text.replace("Java", "Python")); // Output: I love Python 8ïžâŁ split() Definition: Splits a string into multiple parts based on a given delimiter and returns an array. đĄ Why it matters: Used to break text into pieces, such as splitting CSV data or words in a sentence. Example: String data = "apple,banana,grape"; String[] fruits = data.split(","); System.out.println(fruits[1]); // Output: banana 9ïžâŁ trim() Definition: Removes all leading and trailing spaces from a string. đĄ Why it matters: Essential for cleaning user input before saving or comparing values. Example: String name = " Rakshitha "; System.out.println(name.trim()); // Output: Rakshitha #Java #CoreJava #JavaProgramming #LearnJava #JavaDeveloper
To view or add a comment, sign in
-
âđ» JAVA THREADS CHEAT SHEET đč đ§ What is a Thread? đ The smallest unit of execution in a process. Each thread runs independently but shares memory & resources with others. đ§© Enables multitasking and parallel execution. đč âïž What is a Process? đ An executing instance of a program. Each process has its own memory space and can run multiple threads. đ§” Types of Threads đ€ 1ïžâŁ User Threads â Created by users or apps. â JVM waits for them before exit. đĄ Example: Thread t = new Thread(() -> System.out.println("User Thread")); t.start(); đ» 2ïžâŁ Daemon Threads âïž Background threads (e.g., Garbage Collector). â JVM doesnât wait for them. đĄ Example: Thread d = new Thread(() -> {}); d.setDaemon(true); d.start(); đ Creating Threads đž Extending Thread: class MyThread extends Thread { public void run(){ System.out.println("Running..."); } } new MyThread().start(); đž Implementing Runnable: new Thread(() -> System.out.println("Runnable running...")).start(); đ Thread Lifecycle đ§© NEW â RUNNABLE â RUNNING â WAITING/BLOCKED â TERMINATED đčïž Common Thread Methods đ§© Method ⥠Use start() Start a thread run() Thread logic sleep(ms) Pause execution join() Wait for thread interrupt() Interrupt thread setDaemon(true) Make daemon thread đ Synchronization Prevents race conditions when multiple threads share data. synchronized void increment() { count++; } đŹ Thread Communication Use wait(), notify(), and notifyAll() for coordination. đ§ Must be called inside a synchronized block. ⥠Executor Framework (Thread Pooling) Efficient thread reuse for better performance. ExecutorService ex = Executors.newFixedThreadPool(3); ex.submit(() -> System.out.println("Task executed")); ex.shutdown(); đ Pro Tips â Prefer Runnable / ExecutorService â Handle InterruptedException â Avoid deprecated methods (stop(), suspend()) â Use java.util.concurrent for safe multithreading đą Boost Your Skills đ #Java #Threads #Multithreading #Concurrency #JavaDeveloper #JavaInterview #JavaCoding #JavaProgrammer #CodeNewbie #ProgrammingTips #DeveloperCommunity #CodingLife #LearnJava #JavaCheatSheet #ThreadPool #TechInterview #SoftwareEngineer #CodeWithMe #JavaExperts #BackendDeveloper #JavaLearning #JavaBasics #OOP #CodeDaily #CodingJourney #DevCommunity #JavaLovers #TechCareers #CodeSmarter #100DaysOfCode
To view or add a comment, sign in
-
-
" đ Day 45 of 50 â Java LeetCode Challenge " Todayâs challenge was â56. Merge Intervalsâ â a classic array and sorting problem that sharpens your understanding of interval manipulation and greedy algorithms. The goal is to merge all overlapping intervals into distinct non-overlapping ranges. â± Todayâs Task â Merge Intervals đ Problem Statement: You are given an array of intervals where each interval is represented as [start, end]. Your task is to merge all overlapping intervals and return an array of the merged non-overlapping intervals. đ§Ș Examples: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] â Explanation: [1,3] and [2,6] overlap, so they merge into [1,6]. Input: intervals = [[1,4],[4,5]] Output: [[1,5]] â Explanation: [1,4] and [4,5] are overlapping. Input: intervals = [[4,7],[1,4]] Output: [[1,7]] â Explanation: Sorted first, then merged into one continuous interval. đ§ Constraints: 1 <= intervals.length <= 10⎠intervals[i].length == 2 0 <= start †end †10⎠đ» My Java Solution: import java.util.*; class Solution {   public int[][] merge(int[][] intervals) {     if (intervals.length == 0) return new int[0][];          Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));     List<int[]> result = new ArrayList<>();          int[] current = intervals[0];     result.add(current);          for (int[] interval : intervals) {       if (interval[0] <= current[1]) {         current[1] = Math.max(current[1], interval[1]);       } else {         current = interval;         result.add(current);       }     }          return result.toArray(new int[result.size()][]);   } } đ Takeaways: â Smart use of sorting to arrange intervals by start time â Use of greedy merging to efficiently combine overlapping ranges â Clean and efficient O(n log n) time complexity due to sorting â Handles all edge cases including adjacent and nested intervals đĄ Core Concepts: đ Sorting arrays with custom comparators đ Interval overlap detection đ Greedy algorithms and array manipulation đŻ Day 45 completed â 5 more to go! đ #Java #50DaysOfCode #LeetCode #ProblemSolving #Arrays #Sorting #GreedyAlgorithm #JavaProgramming #Day45 #CodeNewbie #LearnInPublic
To view or add a comment, sign in
-
-
đđđŻđ đđđŠđšđ«đČ đđđ§đđ đđŠđđ§đ: đđđđđ€, đđđđ© đđ§đ đđđ«đđđ đ đđšđ„đ„đđđđąđšđ§ Memory is fundamental for Java applications. To handle data effectively, Java divides its memory into two main areas: đĄâđ đ đĄđđđ and đĄâđ âđđđ. Each plays a specific role in storing and managing data, impacting both performance.  1. đŠđđźđ°đž đ đČđșđŒđżđ The stack is a special area of memory used for temporary data and method calls. Every time a method is called in Java, the JVM creates a stack frame to hold the methodâs local variables, parameters and references to objects stored in the heap.  đŸđđđ đđ đđ đđđđ ? đžFor storing primitive types đžFor storing references to objects in the heap  đŸđđđ đđ đđđđđđ đđ đđđ đđđđđ? đžLocal variables đžMethod parameters đžReturn addresses đžReferences to heap objects  đŹđđđđđđ: đđąđđđđ đđđđ đ đđĄđđđđžđ„đđđđđ {    đđąđđđđ đ đĄđđĄđđ đŁđđđ đđđđ(đđĄđđđđ[] đđđđ ) {        đđđĄ đ = 10;          // đ đĄđđđđ đđ đ đĄđđđ        đđđĄ đ = 20;          // đ đĄđđđđ đđ đ đĄđđđ        đđđĄ đ đąđ = đ + đ;     // đ đĄđđđđ đđ đ đĄđđđ        đđŠđ đĄđđ.đđąđĄ.đđđđđĄđđ(đ đąđ);    } } đČđđ đđđđđđ: đžStack memory is fast, working in LIFO (Last In, First Out) order. đžData is temporary â once the method finishes, the stack frame is removed automatically. đžStack size is limited, so too many method calls can cause a StackOverflowError.  2. đđČđźđœ đ đČđșđŒđżđ  While the stack stores temporary data and method calls, the heap is where Java objects and instance variables live. Every time you create a new object using the new keyword, Java allocates space for it in the heap.  đŹđđđđđđ: đđđđ đđ đđđđ đđ = đđđ€ đđđđ đđ("đœđâđ"); // đđđđđđĄ đ đĄđđđđ đđ âđđđ Here, the variable person is stored on the stack, while the actual Person object resides in the heap. Heap memory is slower because managing dynamic memory is more complex, but Garbage Collector automatically frees unreferenced objects.  đČđđ đđđđđđ: đžHeap stores objects and instance variables đžData can live longer than the method that created it  đđźđżđŻđźđŽđČ đđŒđčđčđČđ°đđŒđż In Java, the Garbage Collector (GC) automatically removes objects in the heap that are no longer referenced. This means you donât have to manually free memory. đČđđ đđđđđđ: đžFrees memory for unused objects đžRuns in background, letting developers focus on code  In conclusion, having a clear understanding of Javaâs memory structure helps developers make better decisions when optimizing applications. đâđ đ đĄđđđ manages temporary data and method calls, while đĄâđ âđđđ stores objects and instance variables, managed automatically by the Garbage Collector. Understanding how these memory areas work helps developers write more stable and memory-safe programs.
To view or add a comment, sign in
-
-
đ đđđŻđ đđđŹđŹđšđ§đŹ đšđ đđĄđ đđđČ â đđĄđđ„đ„đšđ° đđšđ©đČ đŻđŹ đđđđ© đđšđ©đČ! âđĄ Today, I explored something that looks simple but quietly causes some of the most confusing bugs in real-world Java projects đ Letâs talk about how đđšđ©đČđąđ§đ đ°đšđ«đ€đŹ đąđ§ đđđŻđâ and how đ©đ«đąđŠđąđđąđŻđ and đđźđŹđđšđŠ đđđđ đđČđ©đđŹ behave completely differently! đ§© đđĄđ đđšđ©đČđąđ§đ đđ„đ„đźđŹđąđšđ§ When we âcopyâ data in Java, we assume weâve created a brand-new version. But often, we just copy the address â not the actual data. đč đđ«đąđŠđąđđąđŻđ đđđđ đđČđ©đđŹ â đđđđ đđ§đ đđ§đđđ©đđ§đđđ§đ Primitives like int, double, and boolean create đđ«đźđ đđšđ©đąđđŹ. Each variable holds its own value â changes donât affect the other. đ§ Think of it as two separate notebooks đđ â write in one, the other stays untouched. đž đđđŁđđđđŹ / đđźđŹđđšđŠ đđđđ đđČđ©đđŹ â đđĄđ đđ§đđđ€đČ đđ§đđŹ Objects (arrays, lists, or user-defined classes) behave differently. When you copy them, Java usually copies đ«đđđđ«đđ§đđđŹ, not real data. So if you modify one object, both variables reflect the change! đŹ đĄ Itâs like giving two people the same house key đ đ â if one paints the wall blue, the other walks in and sees blue too! âïž đđĄđđ„đ„đšđ° đđšđ©đČ đŻđŹ đđđđ© đđšđ©đČ đđĄđđ„đ„đšđ° đđšđ©đČâ Copies references (shared memory). đđđđ© đđšđ©đČâ Creates completely new objects (independent memory). Or simply put đ đ§ Shallow Copy = same juice, two glasses đč Deep Copy = two separate juices, no mix-ups! đ ⥠đđČ đđđ€đđđ°đđČ Next time I âcopyâ something in Java, Iâll pause and ask: âAm I copying the data or just the pointer?â Because understanding this can save hours of debugging and hundreds of surprise bugs. đŹ đđšđ° đČđšđźđ« đđźđ«đ§ â đȘđźđąđđ€ đđđŻđ đ©đźđłđłđ„đ đđšđ« đČđšđź! What do you think will be printed here? đ class Person {   String name;   Person(String name) { this.name = name; } } public class ObjectArrayCopyDemo {   public static void main(String[] args) {     Person[] team = {       new Person("Alice"),       new Person("Bob"),       new Person("Charlie")     };     Person[] copy = team.clone();     copy[0].name = "Alex";     System.out.println("team[0].name = " + team[0].name);   } } đ€ Tell me your answer in the comments â what will this print and why? #Java #Coding #LearningJourney #SoftwareEngineering #Programming #Developers #Backend #100DaysOfCode #CareerGrowth
To view or add a comment, sign in
-
-
Java ke woh secrets jo creators ke alawa kisi ko nahi pata! đ„ --- Post 1: Java Compiler ka "Constant Folding" magic!đ€Ż ```java public class ConstantMagic { public static void main(String[] args) { String result = "J" + "a" + "v" + "a"; System.out.println(result); } } ``` Compile Time pe yeh ban jata hai: ```java String result = "Java"; // â Compiler ne sab combine kar diya! ``` Secret: Java compiler compile time pe constant expressions evaluate kar leta hai! đ --- Post 2: Java ka "String Pool" internal working!đ„ ```java String s1 = "Java"; String s2 = "Java"; String s3 = new String("Java").intern(); // â Pool mein daalta hai System.out.println(s1 == s2); // true â System.out.println(s1 == s3); // true â ``` Internal Magic: · String s1 = "Java" â String Pool check karta hai · new String("Java") â Heap mein new object banata hai · .intern() â String Pool mein daalta hai đĄ --- Post 3: Java ka "Auto-boxing" cache secret!đ ```java Integer i1 = 10; Integer i2 = 10; Integer i3 = 1000; Integer i4 = 1000; System.out.println(i1 == i2); // true â (-128 to 127 cache) System.out.println(i3 == i4); // false â (cache range ke bahar) ``` Kya scene hai? Java-128 se +127 tak Integer values cache karta hai! Isliye== work karta hai sirf cache range mein! đȘ --- Post 4: Java ka "Static Block" execution order!đź ```java class Parent { static { System.out.println("Parent static block"); } } class Child extends Parent { static { System.out.println("Child static block"); } } public class Test { public static void main(String[] args) { new Child(); } } ``` Output: ``` Parent static block Child static block ``` Secret: Parent class ka static block pehle execute hota hai!đ --- yeh secrets samajhne waala 0.0001% developers mein se ek hoga! đ
To view or add a comment, sign in
-
đĄ Part 2 â String Comparison, Concatenation & Immutability in Java In the previous post, we discussed how Strings, SCP, and intern() work in Java. Now letâs explore how they behave when we compare or modify them đ đč 1ïžâŁ == vs .equals() String s1 = "Java"; String s2 = "Java"; System.out.println(s1 == s2); // true â System.out.println(s1.equals(s2)); // true â đ == â compares references (memory addresses) đ .equals() â compares values (content) If both strings come from the String Constant Pool (SCP), == can return true. But when we create new objects using new, they live in heap, so: String s1 = new String("Java"); String s2 = "Java"; System.out.println(s1 == s2); // false â (different memory) đč 2ïžâŁ Compile-time vs Runtime Concatenation String s1 = "Ja" + "va"; String s2 = "Java"; System.out.println(s1 == s2); // true â đ Concatenation of string literals happens at compile-time, so both refer to the same object in SCP. But when concatenation happens at runtime, a new object is created: String part = "Ja"; String s3 = part + "va"; System.out.println(s2 == s3); // false â đč 3ïžâŁ Immutability of Strings String s = "abc"; s.concat("xyz"); System.out.println(s); // abc â (unchanged) Strings are immutable â every modification creates a new object. To reflect the change, you must reassign: s = s.concat("xyz"); System.out.println(s); // abcxyz â đč 4ïžâŁ Using intern() for Optimization If you want to make sure your heap string refers to SCP: String s1 = new String("Java"); String s2 = s1.intern(); System.out.println("Java" == s2); // true â đ intern() makes your string memory-efficient and reusable. đ§ Quick Recap â == â reference check â .equals() â content check â Compile-time concat â stored in SCP â Runtime concat â new object in heap â Strings are immutable â Use intern() for SCP optimization #Java #String #Coding #JavaDeveloper
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