🚀 #Headline: Day 10 — How Arrays Work in Java (Deep Dive into Memory) After learning array syntax in Part 1, today's lecture went beneath the surface. This wasn't about writing more code – it was about understanding what actually happens inside the JVM when you write new int[5]. This is the kind of knowledge that separates surface-level coders from developers who truly understand their tools. Covered in this lecture: ✔️ Why arrays are stored in contiguous memory ✔️ How arrays are allocated in Heap memory (not Stack!) ✔️ Stack vs Heap – what goes where ✔️ What happens inside the JVM during array creation ✔️ How memory indexing actually works (base address + offset) ✔️ Why array access is O(1) – constant time random access ✔️ Performance implications of contiguous storage ✔️ Special case of boolean arrays ✔️ How 2D arrays are stored in memory ✔️ Array of Strings – reference behavior The "aha" moment came when the instructor explained the math behind indexing: address = base + (index × size). This is why arrays are so fast – no traversal needed, just a direct calculation. Understanding that array references live on the Stack while the actual data lives on the Heap clarifies so many concepts about memory management. This lecture truly delivered on its promise: "This is not just coding. This is internal understanding." Learning from: 👨🏫 Aditya Tandon (Instructor) 🚀 Rohit Negi (CoderArmy) 📺 Source: https://lnkd.in/gpFpcpDs Let's connect 🤝 Pankaj Kumar #Java #100DaysOfCode #ProgrammingJourney #JavaBasics #BuildInPublic
Java Arrays: Contiguous Memory, Heap Allocation, and O(1) Access
More Relevant Posts
-
DSA – Day 1 (Revision): Array Basics (Java) Today, I revised the basic concepts of arrays by asking simple questions and understanding them with small examples. 1. What is an array? An array is used to store multiple values of the same data type using a single variable name. Example: int[] numbers = {10, 20, 30}; 2. Why do we use arrays? When we have many values of the same type,creating separate variables is difficult. Arrays help store and manage such data easily. Example: int[] marks = {75, 80, 90}; 3. What is array declaration and initialization? Declaration defines the data type and name of the array.Initialization allocates memory and sets the size or values. Example: int[] a = new int[5]; // declaration + initialization int[] b = {1, 2, 3}; // direct initialization 4.What is an index and why does it start from 0? An index represents the position of an element in an array.Index starts from 0 because the first element is stored at the starting memory location. Example: a[0] // first element 5.Why is array size fixed? When an array is created, memory is allocated based on its size.This size cannot be changed later. Example: int[] a = new int[3]; // array size is fixed to 3 Revising these basics helped me clearly understandhow arrays work in Java. #DataStructures #Algorithms #Coding #Programming #ProblemSolving #CodingLife #CodeDaily
To view or add a comment, sign in
-
-
🚀 DAY 76/150 — CHECKING SUBTREES WITH RECURSION! 🌳 Day 76 of my 150 Days DSA Challenge in Java and today I solved a problem that strengthens my understanding of tree comparison and recursion 💻🧠 📌 Problem Solved: Subtree of Another Tree 📌 LeetCode: #572 📌 Difficulty: Easy–Medium The task is to determine whether one binary tree is a subtree of another binary tree. A subtree must match both structure and node values exactly. 🔹 Approach Used I used a recursive DFS approach: • Traverse each node of the main tree • For every node: Check if the subtree starting at that node is identical to the given tree • Use a helper function to compare two trees: If both nodes are null → true If values differ → false Recursively compare left and right subtrees ⏱ Complexity Time Complexity: O(n × m) (n = nodes in main tree, m = nodes in subtree) Space Complexity: O(h) (recursion stack) 🧠 What I Learned • Tree problems often require combining traversal + comparison • Recursion is powerful for handling hierarchical structures like trees • Breaking the problem into smaller checks simplifies the solution 💡 Key Takeaway This problem taught me how to: Compare two trees efficiently Apply recursion for structural matching Think in terms of subproblems within trees 🌱 Learning Insight As I continue my Binary Tree journey, I’m understanding that: Most tree problems are based on DFS, BFS, or recursion patterns Mastering these basics makes complex tree problems easier ✅ Day 76 completed 🚀 74 days to go 🔗 Java Solution on GitHub: 👉 https://lnkd.in/gZNXaW-C 💡 In trees, solving smaller parts correctly leads to the full solution. #DSAChallenge #Java #LeetCode #BinaryTree #Recursion #DFS #150DaysOfCode #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Switched from Java to Python for DSA practice. First thing that broke my code? The modulo operator. Not because I didn't know how it works — but because it works differently depending on the language, and I had no idea. In Java/C++, % follows the sign of the dividend: -7 % 3 → -1 In Python, % follows the sign of the divisor: -7 % 3 → 2 I was solving a prefix sum problem. Logic was right, approach was right, wrong answers. Took me embarrassingly long to find it. Once I did, I started noticing more: → Division: Java's 7/2 = 3 (integer). Python's 7/2 = 3.5 (decimal). Use // for integer division. → Floor vs truncate: Java truncates toward zero. Python floors toward negative infinity. -7 // 2 → Java: -3 | Python: -4 Same symbols. Different contracts. No errors thrown. Fewer lines of code. More silent assumptions waiting to bite you. #Python #Java #DSA #LearningInPublic #CompetitiveProgramming
To view or add a comment, sign in
-
🔥 𝗗𝗮𝘆 𝟵𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟱𝟯𝟵. 𝗞𝘁𝗵 𝗠𝗶𝘀𝘀𝗶𝗻𝗴 𝗣𝗼𝘀𝗶𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿 | 🟢 Easy | Java Marked as Easy — but the optimal solution is pure binary search brilliance. 🎯 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Given a sorted array, find the kth missing positive integer. Linear scan works — but can we do O(log n)? 💡 𝗧𝗵𝗲 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 At index i, the value arr[i] should be i+1 in a complete sequence. So missing numbers before arr[i] = arr[i] - 1 - i This lets us binary search on the count of missing numbers! ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 ✅ If arr[mid] - 1 - mid < k → not enough missing numbers yet, go right ✅ Else → too many missing, go left ✅ After the loop, left + k gives the exact answer 𝗪𝗵𝘆 𝗹𝗲𝗳𝘁 + 𝗸? After binary search, left is the index where the kth missing number falls beyond. left numbers exist in the array before that point, so the answer is left + k. No extra passes needed. ✨ 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(log n) — vs O(n) linear scan 📦 Space: O(1) This is a perfect example of binary searching on a derived condition, not just a value. A real upgrade from the naive approach. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gVYcjNS6 𝟲 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗧𝗵𝗲 𝗳𝗶𝗻𝗶𝘀𝗵 𝗹𝗶𝗻𝗲 𝗶𝘀 𝗿𝗶𝗴𝗵𝘁 𝘁𝗵𝗲𝗿𝗲! 🏁 #LeetCode #Day94of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
🚀 Ever wondered what actually happens under the hood when you run a Java program? It’s not just magic; it’s the Java Virtual Machine (JVM) at work. Understanding JVM architecture is the first step toward moving from "writing code" to "optimizing performance." Here is a quick breakdown of the core components shown in the diagram: 1️⃣ Classloader System The entry point. It loads, links, and initializes the .class files. It ensures that all necessary dependencies are available before execution begins. 2️⃣ Runtime Data Areas (Memory Management) This is where the heavy lifting happens. The JVM divides memory into specific areas: Method/Class Area: Stores class-level data and static variables. Heap Area: The home for all objects. This is where Garbage Collection happens! Stack Area: Stores local variables and partial results for each thread. PC Registers: Keeps track of the address of the current instruction being executed. Native Method Stack: Handles instructions for native languages (like C/C++). 3️⃣ Execution Engine The brain of the operation. It reads the bytecode and executes it using: Interpreter: Reads bytecode line by line. JIT (Just-In-Time) Compiler: Compiles hot spots of code into native machine code for massive speed boosts. Garbage Collector (GC): Automatically manages memory by deleting unreferenced objects. 4️⃣ Native Interface & Libraries The bridge (JNI) that allows Java to interact with native OS libraries, making it incredibly versatile. 💡 Pro-Tip: If you are debugging OutOfMemoryError or StackOverflowError, knowing which memory area is failing is half the battle won. #Java #JVM #BackendDevelopment #SoftwareEngineering #ProgrammingTips #TechCommunity #JavaDeveloper #CodingLife
To view or add a comment, sign in
-
-
Day 3 of Java with DSA Journey 🚀 📌 Topic: Guess Number Higher or Lower (LeetCode 374) 💬 Quote: "Efficiency is not about doing more; it's about eliminating what doesn't matter." ✨ What I Learned: 🔹 Binary Search Beyond Arrays: Binary Search isn’t limited to arrays — it works perfectly on a number range like [1...n]. 🔹 Working with APIs: Learned how to adapt logic based on API responses: -1 → Guess is too high 1 → Guess is too low 0 → Correct answer 🔹 Power of Efficiency: Even for a huge range (up to 2³¹ - 1), Binary Search finds the answer in ~31 steps 🤯 Compared to Linear Search → practically impossible! 🔹 Complexity: ⏱ Time: O(log n) 📦 Space: O(1) 🧠 Problem Solved: ✔️ Guess Number Higher or Lower 💡 Key Insight: This problem highlights the “Narrowing the Search Space” concept. Each step eliminates half the possibilities — that’s the magic of logarithmic algorithms ⚡ ⚡ Interview Insight (3-Way Decision Logic): Unlike boundary problems, here we deal with three outcomes: 1️⃣ 0 → Found the number (return immediately) 2️⃣ -1 → Move right = mid - 1 3️⃣ 1 → Move left = mid + 1 👉 Use while (left <= right) since the target is guaranteed to exist. 🔑 Takeaway: Consistency beats intensity. Showing up daily is what builds mastery. #DSA #LeetCode #Java #CodingJourney #BinarySearch #ProblemSolving #100DaysOfCode #JavaDeveloper #Algorithms
To view or add a comment, sign in
-
-
🚀 DAY 99/150 — BINARY SEARCH ON ANSWER! 🔥📊 Day 99 of my 150 Days DSA Challenge in Java and today I solved a very important problem that uses Binary Search in a 2D matrix 💻🧠 📌 Problem Solved: Kth Smallest Element in a Sorted Matrix 📌 LeetCode: #378 📌 Difficulty: Medium–Hard 🔹 Problem Insight The task is to find the kth smallest element in an n x n matrix where: • Each row is sorted • Each column is sorted 👉 Instead of flattening and sorting (O(n² log n)), we can do better using Binary Search on Answer. 🔹 Approach Used I used Binary Search on Value Range: • Set: low = smallest element high = largest element • Apply binary search: Find mid Count how many elements are ≤ mid 👉 If count < k → move right (low = mid + 1) 👉 Else → move left (high = mid) ✔️ Continue until low == high → answer found 🔹 Counting Logic (Optimized) • Start from bottom-left corner • If element ≤ mid: All elements above are also ≤ mid Move right • Else: Move up ✔️ This gives O(n) counting per iteration ⏱ Complexity Time Complexity: O(n log (max - min)) Space Complexity: O(1) 🧠 What I Learned • Binary Search is not just for arrays — it can be applied on answer space • Matrix properties can be used for efficient counting • Avoid brute force by leveraging sorted structure 💡 Key Takeaway This problem taught me: How to apply Binary Search on Answer pattern How to optimize 2D problems using structure Thinking beyond direct sorting approaches 🌱 Learning Insight Now I’m improving at: 👉 Recognizing when to use Binary Search beyond arrays 👉 Solving problems with optimized thinking 🚀 ✅ Day 99 completed 🚀 51 days to go 🔗 Java Solution on GitHub: 👉 https://lnkd.in/g-yhc7kH 💡 Don’t search the data — search the answer. #DSAChallenge #Java #LeetCode #BinarySearch #Matrix #Optimization #150DaysOfCode #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Excited to share that I explored some essential Java String Methods under the guidance of G.R Narendra Reddy sir. This session was focused on understanding built-in string functions and their real-time usage. It helped me strengthen my knowledge of string handling and writing efficient code. 🔹 Key Implementations Covered: ✔️ equals() – Comparing content of two strings ✔️ equalsIgnoreCase() – Comparing strings ignoring case sensitivity ✔️ compareToIgnoreCase() – Lexicographical comparison without case sensitivity ✔️ concat() – Joining two strings ✔️ indexOf() – Finding position of characters/substrings ✔️ isBlank() – Checking if string is empty or contains only spaces ✔️ isEmpty() – Checking if string is empty ✔️ length() – Finding length of string ✔️ replace() – Replacing characters or substrings ✨ What I Practiced: ✔️ Using built-in string methods effectively ✔️ Writing clean and optimized Java programs ✔️ Improving logic with real-time examples 💡 Key Learnings: ✔️ Strong understanding of string methods ✔️ Efficient string comparison techniques ✔️ Code readability and performance improvement 🎯 Why It Matters: String methods are widely used in real-world applications like validation, searching, and data processing. 🌱 Consistency in practice turns skills into expertise! G.R NARENDRA REDDY Sir Global Quest Technologies #Java #Programming #Coding #StringMethods #JavaDeveloper #LearningJourney #ProblemSolving #TechSkills #StudentDeveloper
To view or add a comment, sign in
-
LeetCode Practice - 31. Next Permutation The problem is solved using JAVA Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules: If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral. If the value starts with 4 or 9 use the subtractive form representing one symbol subtracted from the following symbol, for example, 4 is 1 (I) less than 5 (V): IV and 9 is 1 (I) less than 10 (X): IX. Only the following subtractive forms are used: 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD) and 900 (CM). Only powers of 10 (I, X, C, M) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (V), 50 (L), or 500 (D) multiple times. If you need to append a symbol 4 times use the subtractive form. Given an integer, convert it to a Roman numeral. Example 1: Input: num = 3749 Output: "MMMDCCXLIX" Explanation: 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places Example 2: Input: num = 58 Output: "LVIII" Explanation: 50 = L 8 = VIII Example 3: Input: num = 1994 Output: "MCMXCIV" Explanation: 1000 = M 900 = CM 90 = XC 4 = IV Constraints: 1 <= num <= 3999 #LeetCode #Java #CodingPractice #ProblemSolving #DSA #Array #DeveloperJourney #TechLearning
To view or add a comment, sign in
-
More from this author
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