Day - 21 : Copy on write Arraylist Copy on write means that whenever a write operation like adding or removing an element, without directly modifying the existing list. A new copy of the list is created , and the modification is applied to that copy.This ensures that other threads reading the list while it's being modified are unaffected. Example : List<String> shoppinglist = new CopyOnWriteArraylist<>(); shopping.add("Egg"); shopping.add("Fruit"); System.out.println("Initial shopping " +shoppinglist); for( String item : shoppinglist){ System.out.println(item); } if(item.equals("Egg")){ shoppinglist.add("Butter"); } } System.out.println("updated shoppinglist " +shoppinglist); } } #java #backend #programming #learning #Corejava EchoBrains
Java CopyOnWriteArrayList Example: Thread-Safe List Modification
More Relevant Posts
-
Java Brain Teaser: Are you declaring what you think you're declaring? Take a look at these two lines of code. They look almost identical, but they behave very differently: Scenario A: int[] ids, types; Scenario B: int ids[], types; 🔍 The Breakdown In Scenario A, you get exactly what you’d expect: two int arrays. ids ➡️ int[] types ➡️ int[] In Scenario B, things get weird. By moving the brackets to the variable name, you change the scope of the array declaration: ids ➡️ int[] (Array) types ➡️ int (Simple primitive!) 💡 Why does this happen? Brackets on the Type: Apply to every variable in that declaration line. Brackets on the Name: Apply only to that specific variable. >>The "Clean Code" Takeaway Always place brackets on the type (int[] ids). Avoid declaring multiple variables of different types (or dimensions) on a single line. #Java #Programming #CleanCode #SoftwareDevelopment #CodingTips
To view or add a comment, sign in
-
Java Brain Teaser: Are you declaring what you think you're declaring? Take a look at these two lines of code. They look almost identical, but they behave very differently: Scenario A: int[] ids, types; Scenario B: int ids[], types; 🔍 The Breakdown In Scenario A, you get exactly what you’d expect: two int arrays. ids ➡️ int[] types ➡️ int[] In Scenario B, things get weird. By moving the brackets to the variable name, you change the scope of the array declaration: ids ➡️ int[] (Array) types ➡️ int (Simple primitive!) 💡 Why does this happen? Brackets on the Type: Apply to every variable in that declaration line. Brackets on the Name: Apply only to that specific variable. >>The "Clean Code" Takeaway Always place brackets on the type (int[] ids). Avoid declaring multiple variables of different types (or dimensions) on a single line. #Java #Programming #CleanCode #SoftwareDevelopment #CodingTips
To view or add a comment, sign in
-
♻️ Why should you always close Scanner in Java? Scanner is used to read input, from the console, a file, or a stream. But a lot of beginners (including me, early on) never bother closing it after use. Here's why that's a problem: ->it holds onto system resources even after your program is done with them ->for file-based Scanners, it can lock the file or cause data not to be flushed properly ->in larger programs, unclosed Scanners can quietly lead to resource leaks The fix is simple, either call: ✔️ sc.close() at the end ✔️ or use a try-with-resources block so Java closes it automatically While practicing Java basics, I realized the code worked either way… but one way was responsible, and the other wasn't. That's something no compiler warning will tell you. Writing correct code and writing clean, responsible code are two different things. Learning the difference early makes you a better developer. Learning in public, improving step by step 🤍 #Java #ResourceManagement #LearningInPublic #Programming
To view or add a comment, sign in
-
Interfaces also allow a class to follow multiple contracts at the same time. Unlike classes, where a class can extend only one parent class, a class in Java can implement multiple interfaces. Things that became clear : • a class can implement more than one interface • each interface can define a different set of behaviours • the implementing class must provide implementations for all the methods • this approach allows combining different capabilities in a single class • it helps achieve flexibility while avoiding multiple inheritance of classes A simple example shows how multiple interfaces can be implemented : interface ICalculator { void add(int a, int b); void sub(int a, int b); } interface IAdvancedCalculator { void mul(int a, int b); void div(int a, int b); } class CalculatorImpl implements ICalculator, IAdvancedCalculator { public void add(int a, int b) { System.out.println(a + b); } public void sub(int a, int b) { System.out.println(a - b); } public void mul(int a, int b) { System.out.println(a * b); } public void div(int a, int b) { System.out.println(a / b); } } This structure allows the class to support operations defined by multiple interfaces while keeping responsibilities organized. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
Day - 30 : Deque in java 1) Allows insertion and removal of elements from both ends. 2) Versatile than regular queue and stack because they support all operations of both. ● Insertion methods : 1) addFirst : Inserts the specified elements at the front. 2) addLast : Inserts the specified element at the last. 3) offerFirst : Inserts the specified element at the front if possible. 4) offerLast : Inserts the specified element at the end if possible. ● Removal methods : 1) removeFirst () : Retrieves and removes the first element. 2) removeLast () : Retrieves and removes the last element. 3) pollfirst () : Retrieves and removes the first element or returns null if empty. 4) pollLast() : Retrieves and removes the last element or returns null if empty. #Java #CoreJava #coding #programming #learning #Deque
To view or add a comment, sign in
-
-
𝗗𝗔𝗬 𝟭 – 𝗛𝗼𝘄 𝗝𝗮𝘃𝗮 𝗕𝗲𝗰𝗮𝗺𝗲 𝗣𝗹𝗮𝘁𝗳𝗼𝗿𝗺 𝗜𝗻𝗱𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝘁 𝐋𝐞𝐭’𝐬 𝐬𝐭𝐚𝐫𝐭 𝐟𝐫𝐨𝐦 𝐳𝐞𝐫𝐨. 𝐁𝐞𝐟𝐨𝐫𝐞 𝐉𝐚𝐯𝐚... 𝐛𝐞𝐟𝐨𝐫𝐞 𝐜𝐨𝐝𝐞... 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗮 𝗽𝗿𝗼𝗴𝗿𝗮𝗺? A program is simply a set of instructions. 𝗪𝗵𝗮𝘁 𝗶𝘀 𝘀𝗼𝗳𝘁𝘄𝗮𝗿𝗲? A collection of programs designed to solve a problem. 𝗡𝗼𝘄 𝗵𝗲𝗿𝗲’𝘀 𝘁𝗵𝗲 𝗿𝗲𝗮𝗹 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻: If we write instructions in English-like code… 𝐇𝐨𝐰 𝐝𝐨𝐞𝐬 𝐚 𝐦𝐚𝐜𝐡𝐢𝐧𝐞 𝐮𝐧𝐝𝐞𝐫𝐬𝐭𝐚𝐧𝐝 𝐢𝐭? Machines only understand 𝗯𝗶𝗻𝗮𝗿𝘆 - 𝟬𝘀 𝗮𝗻𝗱 𝟭𝘀. So somewhere, our human-readable code must be translated into machine-understandable instructions. Most languages use: → 𝗔 𝗰𝗼𝗺𝗽𝗶𝗹𝗲𝗿 or → 𝗔𝗻 𝗶𝗻𝘁𝗲𝗿𝗽𝗿𝗲𝘁𝗲𝗿 𝗝𝗮𝘃𝗮 𝘂𝘀𝗲𝘀 𝗯𝗼𝘁𝗵. And that’s where the story gets interesting. 𝗪𝗵𝗲𝗻 𝘄𝗲 𝘄𝗿𝗶𝘁𝗲 𝗮 𝗝𝗮𝘃𝗮 𝗽𝗿𝗼𝗴𝗿𝗮𝗺: 𝗦𝘁𝗲𝗽 𝟭: The javac compiler (from JDK) converts our .java file into a .class file. This .class file contains 𝗯𝘆𝘁𝗲𝗰𝗼𝗱𝗲. 𝘽𝙮𝙩𝙚𝙘𝙤𝙙𝙚 𝙞𝙨 𝙣𝙤𝙩 𝙝𝙪𝙢𝙖𝙣 𝙡𝙖𝙣𝙜𝙪𝙖𝙜𝙚. 𝙄𝙩’𝙨 𝙣𝙤𝙩 𝙧𝙖𝙬 𝙢𝙖𝙘𝙝𝙞𝙣𝙚 𝙘𝙤𝙙𝙚 𝙚𝙞𝙩𝙝𝙚𝙧. 𝙄𝙩’𝙨 𝙖𝙣 𝙞𝙣𝙩𝙚𝙧𝙢𝙚𝙙𝙞𝙖𝙩𝙚 𝙡𝙖𝙣𝙜𝙪𝙖𝙜𝙚 𝙙𝙚𝙨𝙞𝙜𝙣𝙚𝙙 𝙨𝙥𝙚𝙘𝙞𝙛𝙞𝙘𝙖𝙡𝙡𝙮 𝙛𝙤𝙧 𝙨𝙤𝙢𝙚𝙩𝙝𝙞𝙣𝙜 𝙘𝙖𝙡𝙡𝙚𝙙 𝙩𝙝𝙚 𝙅𝙑𝙈. Now enters the hero: 𝗝𝗩𝗠 (Java Virtual Machine) The JVM reads the bytecode and converts it into machine-level instructions for the specific operating system it is running on. 𝗛𝗲𝗿𝗲’𝘀 𝘁𝗵𝗲 𝗽𝗼𝘄𝗲𝗿𝗳𝘂𝗹 𝗽𝗮𝗿𝘁: The same .class file can run on Windows, Linux, or Mac - as long as that system has 𝗶𝘁𝘀 𝗼𝘄𝗻 𝗝𝗩𝗠. That’s why Java is called: 𝗣𝗹𝗮𝘁𝗳𝗼𝗿𝗺 𝗜𝗻𝗱𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝘁 (Write Once, Run Anywhere) Security, huge libraries, built-in APIs, memory management - all of that matters. 𝗕𝘂𝘁 𝘁𝗵𝗶𝘀 𝗮𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗲? This is what made Java revolutionary. 𝗧𝗼𝗺𝗼𝗿𝗿𝗼𝘄 (𝗗𝗮𝘆 𝟮): We’ll break down: • What exactly is JDK, JRE, JVM? • How they relate to each other • And what actually happens when you run 𝙹𝙰𝚅𝙰 𝙷𝙴𝙻𝙻𝙾𝚆𝙾𝚁𝙻𝙳 𝙏𝙝𝙞𝙨 𝙞𝙨 𝙟𝙪𝙨𝙩 𝙩𝙝𝙚 𝙗𝙚𝙜𝙞𝙣𝙣𝙞𝙣𝙜. Notes credit goes to my mentor Banti Chouhan sir 🥹🙏🏻 #Java #Backend #Programming #LearnInPublic #Day1
To view or add a comment, sign in
-
-
💡 Why does System.out.print(); sometimes give a compile-time error? While learning Java, you might write something like this: System.out.println("A"); System.out.print(); System.out.println("X"); and get a compile-time error. Why does this happen? 🤔 ✔️ The reason: System.out.print(); prints output without moving to a new line, but this method requires an argument. When you leave it empty, the compiler doesn’t know what it should print, so it throws a compile-time error. In simple words 👉 print() must be given something to print. ✔️ How to fix it: 📍 If you want a blank line: System.out.println(); 📍 If you want to print a space: System.out.print(" "); Small mistakes like this help us understand how Java methods really work 💻✨ What other small Java mistakes helped you understand programming better? Drop them in the comments 👇 #Java #Programming #LearningJava #CodingBasics #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 10/100 — Scanner User Input in Java ⌨️ One common issue every Java beginner faces while taking user input using the Scanner class. ⚠️ The nextLine() bug after nextInt() When we use nextInt() and then immediately call nextLine(), the nextLine() may get skipped. This happens because nextInt() does not consume the newline character left in the input buffer. ✔️ Solution: Add an extra sc.nextLine() to clear (flush) the buffer. Example: Scanner sc = new Scanner(System.in); System.out.print("Enter your marks: "); int marks = sc.nextInt(); sc.nextLine(); // flush the leftover newline System.out.print("Enter your name: "); String name = sc.nextLine(); 💻 Challenge I worked on today: Build a simple Report Card program that takes student details and marks using Scanner input. Step by step learning — improving Java fundamentals every single day 🚀 #Java #CoreJava #Scanner #JavaLearning #Programming #100DaysOfCode
To view or add a comment, sign in
-
-
Another concept that appears when working with constructors is constructor overloading. It allows a class to have multiple constructors so that objects can be created in different ways depending on the information available. Things that became clear : • constructor overloading means having multiple constructors in the same class • each constructor must have a different parameter list • it allows objects to be initialized with different sets of values • Java decides which constructor to use based on the arguments passed during object creation • it helps make classes more flexible and easier to use A simple example shows how different constructors can be used : class Student { String name; int age; Student() { System.out.println("Default constructor"); } Student(String name, int age) { this.name = name; this.age = age; } } In this case, objects can be created either without values or with specific values depending on which constructor is called. Understanding constructor overloading made it clearer how classes can support different ways of creating objects. #java #oop #programming #learning #dsajourney
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