📌 Day 18,19,20 – Understanding Arrays in Java On Day 18,19,20, I learned one of the most fundamental static data structures in Java — Arrays. 🔹 What is an Array? An array is an object used to store and retrieve multiple elements of the same data type efficiently. It helps organize data and enables faster access using index positions. 🔹 Key Observations About Arrays: Dimensionality → 1D, 2D, 3D arrays Homogeneous Data → Arrays store only the same data type Structure → Regular arrays & Jagged arrays 🔹 Array Syntax: int[] a = new int[5]; Arrays are created using the new keyword, which allocates memory in the Heap segment. 🔹 Important Concepts: Array index always starts from 0 Traversing elements from start to end is called array traversal Arrays are objects, not primitive data types 🔹 Types of Arrays: 1D Array → Single row structure 2D Array → Rows and columns 3D Array → Blocks, rows, and columns 🔹 Regular vs Jagged Array: Regular Array → Equal number of columns in every row Jagged Array → Unequal number of columns in rows 🔹 Returning an Array from a Method: When an array is returned from a method, the JVM creates an object, and the reference of that object is returned to the calling method. Understanding arrays builds a strong foundation for DSA, memory management, and real-world problem solving 🚀 #Java #CoreJava #Arrays #DataStructures #JVM #HeapMemory #LearningJourney #Day18
Java Arrays Fundamentals: Understanding 1D, 2D, 3D Arrays
More Relevant Posts
-
Merge Sort in Java may look complex, but most mistakes happen because: • we don’t clearly understand how the array is divided • we mix up the merge logic • we forget that sorting actually happens during the merge step The core idea is straightforward: divide the array into smaller parts, sort them recursively, and then merge them back in sorted order. What really happens: – The array is repeatedly divided until each part has one element – A single element is already sorted by definition – Then, pairs of sorted subarrays are merged to form bigger sorted arrays So: – First, size-1 arrays are merged into size-2 sorted arrays – Then size-2 into size-4 – Then size-4 into size-8 – And so on, until the whole array is sorted The key insight: 👉 All the real sorting happens during merging, not during splitting. Once you understand that: • recursion just breaks the problem into smaller pieces • merge logic ensures order • time complexity stays O(n log n) in all cases Merge Sort isn’t about simplicity — it’s about guaranteed performance and clean divide-and-conquer thinking. That’s what makes it a foundation algorithm for: ✔ large datasets ✔ external sorting ✔ stable sorting requirements #Java #MergeSort #DSA #Algorithms #DivideAndConquer #ProblemSolving #BackendEngineering
To view or add a comment, sign in
-
Day 10 of Java & Now I Know Where My Array Actually Lives 🧠💻 Today was not about writing arrays… It was about understanding what happens inside memory. And honestly this was powerful. 👉 Arrays are non-primitive (reference) types. That means: When we write int[] arr = new int[5]; • The variable arr lives in Stack memory • The actual array data lives in Heap memory • arr stores the reference (address) of that heap location So basically… We’re pointing to memory. 🔥 Contiguous Storage Array elements are stored next to each other in one continuous block. 5 integers = 5 × 4 bytes = 20 bytes All in one straight line. That’s why arrays are fast. ⚡ Random Access Java doesn’t search for elements. It calculates their address: Base Address + (Index × Size of Data Type) That’s why accessing the 1st element takes the same time as the 1,000,000th. O(1) access. Instant. Big realization today? Arrays aren’t just collections. They’re structured memory blocks optimized for speed. Day 10 and now I’m not just using arrays… I understand how they work internally. Leveling up every day 🚀🔥 Special thanks to Rohit Negi sir and Aditya Tandon sir🙌🏻🙌🏻 #Java #CoreJava #Arrays #Programming #LearningJourney #Developers #BuildInPublic
To view or add a comment, sign in
-
-
🔍 Pattern Matching in Java: Stop Writing Boilerplate, Start Writing Intent How many times have you written this? if (obj instanceof String) { String s = (String) obj; System.out.println(s.toUpperCase()); } Java 16+ eliminated this ceremony forever. ✅ if (obj instanceof String s) { System.out.println(s.toUpperCase()); } But that's just the beginning. With Java 21, Pattern Matching exploded into something extraordinary via switch expressions: String result = switch (shape) { case Circle c -> "Area: " + Math.PI * c.radius() * c.radius(); case Rectangle r -> "Area: " + r.width() * r.height(); case Triangle t -> "Area: " + 0.5 * t.base() * t.height(); default -> "Unknown shape"; }; No casting. No NPE traps. No noise. Just pure, readable logic. 🔥 Why does this matter? → It closes the gap between Java and functional languages like Scala or Kotlin → Works beautifully with sealed classes — the compiler ensures exhaustiveness → Reduces cognitive load and bug surface area simultaneously → Makes your domain model express behavior, not just data Guarded patterns take it even further: case Integer i when i > 0 -> "Positive: " + i; case Integer i -> "Non-positive: " + i; This isn't syntactic sugar. It's a paradigm shift in how we model logic in Java. The language is finally letting us write what we mean, not what the compiler demands. Are you already using Pattern Matching in production? What's your favorite use case? Drop it below 👇 #Java #Java21 #PatternMatching #CleanCode #SoftwareEngineering #JVM #BackendDevelopment
To view or add a comment, sign in
-
-
Stop writing boilerplate. Let the compiler do the heavy lifting. In modern Java, you can replace a verbose data class with a single line: public record Car(String name) {} Behind the scenes, Java automatically generates: ✅ Private final fields (Immutability by default). ✅ Canonical constructor (State initialization). ✅ Accessor methods (e.g., car.name()). ✅ equals() and hashCode() (Value-based equality). ✅ toString() (Readable output). // What the JVM actually sees: public final class Car extends java.lang.Record { privatefinal String name; public Car(String name) { this.name = name; } public String name() { returnthis.name; } @Overridepublic boolean equals(Object o) { ... } @Overridepublic int hashCode() { ... } @Overridepublic String toString() { ... } } The Result? Cleaner code, fewer bugs, and zero "boilerplate fatigue." Are you still using traditional POJOs, or have you switched to Records? 👇 #Java #CleanCode #SoftwareEngineering #ProgrammingTips #ModernJava
To view or add a comment, sign in
-
#Day47 of #100DaysDSAChallenge 🚀 Today I solved Reversing a Doubly Linked List in Java. 🔎 Brute Approach (Using Stack) Traverse the list, push node values into a stack, then overwrite values while popping to reverse the list data. Time Complexity: O(n) Space Complexity: O(n) ⚡ Better / Optimal Approach (In-place Pointer Swap) Swap prev and next for every node and update the head at the end. This reverses the actual structure without extra memory. Time Complexity: O(n) Space Complexity: O(1) 🔗 Solutions Repository: https://lnkd.in/g_rSFCh8 #100DaysOfDSA #DSA #LeetCode #Java #Algorithms #DataStructures #ProblemSolving #CodingJourney #LearningInPublic #InterviewPrep #PlacementPreparation #KunalKushwaha #Striver #GeeksForGeeks
To view or add a comment, sign in
-
-
StackOverflowError vs. OutOfMemoryError: A JVM Memory Primer Understanding the difference between these two Java runtime errors is crucial for effective debugging and performance tuning. Both signal exhaustion, but in distinct memory areas of the JVM. Q1: What is the fundamental distinction between them? A: The core difference lies in the memory pool they deplete. A StackOverflowError is related to stack memory, which is per-thread and stores method calls, local primitives, and object references. It's typically caused by deep or infinite recursion, where a method calls itself repeatedly until the thread's fixed stack size is exhausted. An OutOfMemoryError concerns the heap memory, the shared runtime data area where all Java objects and class instances are allocated. This error occurs when the heap is full and the Garbage Collector cannot reclaim enough space for a new object. Q2: How do their symptoms and debugging approaches differ? A: A StackOverflowError is often easier to diagnose. The exception stack trace is repetitive, clearly showing the cyclic pattern of method calls. Fixing it usually involves correcting the recursive algorithm's base case or converting it to an iterative solution. In contrast, an OutOfMemoryError is more complex. The root cause could be a genuine memory leak (objects unintentionally held in references), an undersized heap for the application's needs, or inefficient object creation. Debugging requires tools like heap dumps, profilers (VisualVM, YourKit), and analyzing GC logs to identify what's filling the heap and why those objects aren't being collected. Key Insight: Think of it as depth vs. breadth. StackOverflow is about the depth of your execution chain in a single thread. OutOfMemory is about the breadth of object allocation across the entire application. Have you tackled a tricky OOM lately? What's your go-to strategy for heap analysis? #Java #JVM #PerformanceTuning #Debugging #SoftwareDevelopment #Programming
To view or add a comment, sign in
-
🚀 𝗔𝗿𝗿𝗮𝘆𝗟𝗶𝘀𝘁 𝘃𝘀 𝗟𝗶𝗻𝗸𝗲𝗱𝗟𝗶𝘀𝘁 — 𝗪𝗵𝗶𝗰𝗵 𝗢𝗻𝗲 𝗦𝗵𝗼𝘂𝗹𝗱 𝗪𝗲 𝗨𝘀𝗲? While revising the Java Collection Framework, I realized something important. We often use ArrayList by default. But do we really understand when to use LinkedList instead? Both implement the List interface, but internally they are completely different. 🔹 𝗔𝗿𝗿𝗮𝘆𝗟𝗶𝘀𝘁 ArrayList is backed by a dynamic array. That means: • Accessing elements using index is very fast • But inserting or deleting in the middle requires shifting elements So it works best when: ✔ We mostly read data ✔ Random access is frequent 🔹 𝗟𝗶𝗻𝗸𝗲𝗱𝗟𝗶𝘀𝘁 LinkedList is backed by a doubly linked list. That means: • Insertion and deletion are faster • Accessing elements by index is slower So it works best when: ✔ We frequently add/remove elements ✔ We modify data often 𝗦𝗶𝗺𝗽𝗹𝗲 𝗖𝗼𝗱𝗲 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 ->List<Integer> list1 = new ArrayList<>(); ->List<Integer> list2 = new LinkedList<>(); Same interface. Different internal working. Different performance behavior. 💡 𝗪𝗵𝗮𝘁 𝗜 𝗟𝗲𝗮𝗿𝗻𝗲𝗱 Choosing the right data structure is not about syntax. It’s about understanding the use case. The more I revise Collections, the more I realize that fundamentals matter more than memorizing methods. #Java #CollectionFramework #ArrayList #LinkedList #Programming #DSA #LearningJourney
To view or add a comment, sign in
-
-
📌 Arrays or ArrayLists in Java — which one should you use? Here’s a simple side-by-side comparison covering size behavior, performance and data storage.👇 #Java #Coding #LearningJava #DeveloperTips #DataStructures
To view or add a comment, sign in
-
📝 Day 19/30 – LeetCode #33 (Search in Rotated Sorted Array) | Java This problem combines binary search with array rotation, making it more about logic than syntax. The main challenge was identifying which half of the array is sorted at every step and deciding whether the target lies within that range. By comparing boundary values and narrowing the search space accordingly, the problem can be solved efficiently in O(log n) time. This reinforced how adaptable binary search becomes when combined with careful condition checks. Another reminder that mastering patterns is far more important than memorizing solutions. #LeetCode #Java #DSA #BinarySearch #Arrays #ProblemSolving #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