While solving LeetCode today, I noticed an interesting Java behavior. Arrays in Java are automatically initialized to default values (0, false, null), but local variables don’t get any default value at all — using them without initialization causes a compile-time error. I dug into why this happens and learned that Java enforces this to improve type safety and prevent bugs caused by unpredictable or garbage values. This is actually pretty useful in practice: it forces us to be explicit with local variables, catches mistakes early at compile time, and helps avoid subtle runtime bugs — especially in algorithms and system-level code. It might be something many people already know, but revisiting these fundamentals while practicing DSA has been surprisingly valuable(atleast to me). #Java #LeetCode #DataStructures #SoftwareEngineering #LearningInPublic
Java Local Variables Require Initialization
More Relevant Posts
-
🚀 Minimum Absolute Difference (Optimized Java Approach) Today I revisited a classic array problem and implemented a clean, optimized solution using sorting and a two-pass strategy. Leetcode Problem link:https://lnkd.in/gWtQD_FX 💡 Key idea: Sort the array first Adjacent elements now give the minimum possible differences Use: Pass 1 → find the minimum absolute difference Pass 2 → collect all pairs with that difference ✅ Why this approach? Easy to reason about Avoids unnecessary condition checks Optimal time complexity ⏱️ Complexity: Time: O(n log n) (sorting dominates) Space: O(1) extra (excluding output) #Java #DataStructures #Algorithms #LeetCode #ProblemSolving #CleanCode #LinkedList #Sorting #OptimizedCode
To view or add a comment, sign in
-
-
Day25 - LeetCode Journey Solved LeetCode 344: Reverse String in Java ✅ This was a simple problem on the surface, but it perfectly highlights how powerful clean logic can be. The goal was to reverse a string in-place using O(1) extra space, which means no extra arrays and no shortcuts. Just pure two-pointer logic. Using the left and right pointers and swapping characters step by step felt very satisfying. It’s one of those patterns that looks small but appears everywhere in interviews and real-world problems. Mastering this makes many string and array problems much easier later on. What I liked about this problem is how it teaches efficiency. Instead of creating new memory, we directly modify the existing array. That mindset of optimizing space is extremely important in DSA. Key takeaways: • Strong practice of the two-pointer technique • In-place operations with constant extra space • Clean and readable swapping logic • Reinforced fundamentals of string manipulation ✅ Accepted successfully ✅ 0 ms runtime with optimal performance Sometimes the simplest problems build the strongest foundations. Consistency with basics is what makes complex problems feel easier later 💪 #LeetCode #DSA #Java #Strings #ProblemSolving #Algorithms #CodingJourney #InterviewPreparation #TwoPointers #Consistency #DailyPractice
To view or add a comment, sign in
-
-
🚀 Day -10/100 #100DaysOfLeetCode 📌Problem-1 : Find the Index of the First Occurrence in a String. 🔗Problem Link : https://lnkd.in/gp6NbvNR 💡 Approach: To solve this problem, I needed to find the first occurrence of the substring needle inside the string haystack. 🔹 First, I checked whether haystack contains needle using the built-in contains() method. 🔹 If it exists, I returned the starting index using indexOf(). 🔹 If it does not exist, I returned -1. This approach ensures a simple and efficient solution by leveraging Java’s built-in string methods. #JavaProgramming #LeetCode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 8 of My 90 Days Java Full Stack Challenge Today, I practiced two interesting String problems that helped me strengthen my understanding of string manipulation and logical thinking in Java. 🧩 1️⃣ Reverse Words in a Sentence Input: "Java is fun" Output: "fun is Java" 🔎 Approach: Traverse from the end Extract words Rebuild the sentence in reverse order Handle edge cases like multiple spaces 💡 Learned how to manage string traversal without relying completely on inbuilt methods. 🧩 2️⃣ Check Rotation of String Example: "abcd" & "cdab" 🔎 Key Insight: If s2 is a rotation of s1, then s2 must be a substring of (s1 + s1). ✔ Length check first ✔ Then verify using substring logic This problem improved my understanding of pattern recognition in strings. 🧠 Key Takeaways: Two-pointer & traversal techniques Importance of handling edge cases Writing optimized logic instead of brute force Understanding how string concatenation helps solve rotation problems 📅 Next step: Continue exploring more intermediate-level string problems. Consistency > Motivation 💪 #90DaysJavaFullStack #Java #StringManipulation #ProblemSolving #LearningInPublic #DeveloperJourney
To view or add a comment, sign in
-
Day31 - LeetCode Journey Solved LeetCode 392: Is Subsequence in Java ✅ This was a clean and intuitive problem focused on understanding relative order in strings. The goal was simple: check whether one string appears as a subsequence of another without disturbing character order. I used the classic two-pointer approach. One pointer moves through the smaller string, the other through the larger one. Whenever characters match, we move ahead. If we can fully traverse the smaller string, it’s a valid subsequence. What I liked about this problem is how it reinforces the idea that not every problem needs extra space or complex logic. Sometimes, careful traversal is all you need. Key takeaways: • Strengthened understanding of two-pointer technique • Improved handling of ordered string traversal • Focused on writing minimal and efficient logic • Reinforced thinking around constraints and edge cases ✅ All test cases passed ✅ Another solid string problem added to the list Consistency over intensity. One problem at a time 💪 #LeetCode #Java #DSA #Strings #TwoPointers #ProblemSolving #Algorithms #CodingJourney #InterviewPreparation #DailyPractice
To view or add a comment, sign in
-
-
Advanced Java – Day 1 Day 1 was less about “advanced” stuff and more about getting into the core •Revisited the basics that actually matter: •How Java works internally (source code ➡️ bytecode ➡️JVM) •Why Java is platform independent •Difference between JDK, JRE, and JVM •Primitive vs non-primitive data types •Core OOP concepts like class, object, encapsulation, abstraction, and polymorphism •How memory works (stack, heap, static, string pool) I also practised these concepts by building a simple calculator program to understand how logic, methods, and objects actually come together and a constrain based if-else problem. Seeing it run made things clearer. Looking forward to learning more step by step. #Java #LearningInPublic #AdvancedJava #ProgrammingBasics #StudentLife #Consistency
To view or add a comment, sign in
-
-
🚀 Day 1/30 – Java DSA Challenge 🔎 Problem 2: 3110. Score of a String (LeetCode – Easy) Solved my second problem for Day 1 of the 30 Days DSA challenge. 🧠 Problem Statement The score of a string is defined as the sum of the absolute difference between ASCII values of adjacent characters. We need to return the total score. 💡 Example Input: "hello" ASCII values: h = 104 e = 101 l = 108 l = 108 o = 111 Score calculation: |104 − 101| + |101 − 108| + |108 − 108| + |108 − 111| = 3 + 7 + 0 + 3 = 13 👨💻 Approach ✔ Start from index 1 ✔ Compare each character with its previous character ✔ Add the absolute difference to a running sum Since characters in Java are stored using ASCII values internally, we can directly subtract them. ⏱ Time Complexity: O(n) – We traverse the string once. 📦 Space Complexity: O(1) – Only a single variable is used. 📌 Key Learning: Understanding how characters are stored internally (ASCII values) helps simplify string problems. Excited to continue this journey 🚀 #Day1 #30DaysOfCode #Java #DSA #LeetCode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Solved: Two Sum II (Sorted Array | Two-Pointer Technique | Java) NeetCode Implemented an efficient solution to find two numbers in a sorted array that add up to a given target. 🔍 Approach: Used the Two-Pointer technique Initialized one pointer at the beginning and one at the end Compared the sum and adjusted pointers accordingly Returned 1-based indices as required This approach avoids nested loops and keeps the solution clean and efficient. 💡 Key Learnings: Leveraging sorted array properties Writing optimized logic without extra data structures Improving pointer-based problem solving Consistent problem-solving practice strengthens logical thinking and code efficiency. #Java #DSA #CodingPractice #ProblemSolving #SoftwareDevelopment
To view or add a comment, sign in
-
-
Java Collections — Iterator vs forEach 👉small but important 🤖 While working with ArrayList and LinkedList, we usually loop using forEach. So why does Iterator still exist? forEach: - Simple and readable - Best when we only want to read data Iterator: - Allows safe removal while iterating - Avoids ConcurrentModificationException - More control over traversal Example: Iterator<String> it = list.iterator(); while (it.hasNext()) { if (it.next().equals("Java")) { it.remove(); } } forEach looks cleaner, but Iterator is safer when modifying collections. Small concept, but very useful in real code. #Java #Collections #JavaLearning
To view or add a comment, sign in
-
🚀ARRAYS IN JAVA As I dive deeper into Java, I’m realizing that even the fundamentals have some hidden gems. Most of us start with the standard Array, but have you played around with Jagged Arrays? So , Hi LinkedIn 👋 Here’s the breakdown of first time experience: We usually visualize a 2D array like a spreadsheet—every row has the exact same number of columns. It’s clean, it’s structured, but it’s rigid. 💡 The Fascinating Fact: Java’s "Array of Arrays" Here is the eye-opener: In Java, a multi-dimensional array isn't a single block of memory. It is actually an array that holds other arrays. Because of this architecture, Java allows for Jagged Arrays—where every row can have a completely different length. 🤯 🛠️ how it matters?? 🤔 Memory Efficiency Real-World Logic Dynamic Thinking and more... It’s these small nuances that make Java so flexible and powerful. To my fellow developers: did you use jagged arrays in your early projects? #Java #CodingNewbie #SoftwareEngineering #DataStructures #BackendDevelopment #TechLearning 💻 See it in action 👇
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