Day 11: Scope & Memory – Mastering the 3 Types of Variables in Java 🧠📍 Today was all about understanding where data "lives" within my code. I learned that in Java, where you declare a variable completely changes its life cycle and how it can be accessed. Here is my breakdown of the three types of variables: 1. Local Variables (The Temporary Workers) ⏱️ These are declared inside a method. Access: They can only be used within that specific method. Memory: They are created when the method starts and destroyed once it finishes. Key Rule: They do not get default values; you must initialize them before use! 2. Instance Variables (The Object’s Properties) 🏠 These are declared inside the class but outside any method. Access: To access these, you must create an object (Instance) of the class. Memory: Every object gets its own separate copy of these variables. If I change the value in Object A, Object B remains unchanged. 3. Static Variables (The Shared Knowledge) 🌐 These are declared inside the class, outside methods, with the static keyword. Access: You can access them directly using the Class Name—no object is required! Memory: There is only one copy shared across all objects of that class. If one object changes a static variable, it changes for everyone. My Takeaway: Understanding Variable Scope is my first real step into Memory Management. It’s not just about storing data; it’s about knowing exactly where that data is accessible! #JavaFullStack #CoreJava #CodingJourney #VariableScope #MemoryManagement #Day11 #LearningInPublic 10000 Coders Meghana M
Java Variables: Local, Instance, Static Explained
More Relevant Posts
-
Java forces you to be explicit about 𝐡𝐨𝐰 𝐝𝐚𝐭𝐚 𝐥𝐢𝐯𝐞𝐬 𝐢𝐧 𝐦𝐞𝐦𝐨𝐫𝐲. That’s why primitive types exist. When you use primitives like 𝗶𝗻𝘁, 𝗱𝗼𝘂𝗯𝗹𝗲, or 𝗯𝗼𝗼𝗹𝗲𝗮𝗻, you’re working directly with values — not objects, not references. This matters more than it looks. Primitive types: • Are faster to access • Use less memory • Behave predictably Compare that with objects. Objects live on the heap, come with overhead, and are accessed through references. Early on, this difference feels theoretical. Later, it explains performance issues, memory leaks, and why certain designs don’t scale. Understanding primitives teaches an important habit: 𝗰𝗵𝗼𝗼𝘀𝗲 𝘁𝗵𝗲 𝘀𝗶𝗺𝗽𝗹𝗲𝘀𝘁 𝗿𝗲𝗽𝗿𝗲𝘀𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻 𝘁𝗵𝗮𝘁 𝘀𝗼𝗹𝘃𝗲𝘀 𝘁𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺. Not everything needs to be an object. Not everything needs abstraction. Today was about: • What primitive data types really are • How they differ from reference types • Why memory awareness matters in Java Good performance doesn’t start with optimization. It starts with understanding fundamentals. #Java #Performance #MemoryManagement #Programming #SoftwareEngineering #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Built a CLI-Based Prefix Search Engine and Word Suggestion Tool Using Trie (Java) Lately, I’ve been focusing not just on solving DSA problems, but on actually building things using data structures. So I built a small project using Trie in Java. • The user can enter a paragraph • The paragraph is preprocessed and stored in a Trie • The user can then enter a prefix • The system returns word suggestions based on that prefix It’s completely CLI-based and written in pure Java — no frameworks, just core logic. This project helped me understand:- • How prefix search works internally • How autocomplete systems are structured • How to separate business logic (Trie) from user interaction (CLI) • How to move from “problem solving” to “system building” It’s a small project, but a big step toward thinking like an engineer rather than just a problem solver. GitHub: https://lnkd.in/gEXDPeH3 Open to feedback and suggestions! #realdsa #Java #DSA #Trie #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
Day 5 1️⃣ Typecasting in Java Typecasting is converting one data type into another. Sounds simple — but it can silently break logic if misunderstood. Two types: ✔ Implicit (Widening Casting) Smaller → Larger data type Handled automatically by Java. Examples: byte → short short → int int → long long → float char → int Important insight: long → float happens implicitly, but precision can be lost. Just because it compiles doesn’t mean it’s safe. That’s a blind spot many beginners miss. ✔ Explicit (Narrowing Casting) Larger → Smaller data type Requires manual instruction from the programmer. Examples: double → float long → int int → byte Here, Java forces you to take responsibility. If data is lost — that’s on you. That distinction matters in real-world applications where financial values, counters, and IDs are involved. 2️⃣ Increment & Decrement Operators Understanding the difference between: Pre-increment (++a) → increments first, then uses the value Post-increment (a++) → uses the value first, then increments Small syntax difference. Big logical impact inside loops and expressions. 3️⃣ Wrapping (Overflow Behavior) For example in byte: Maximum value: 127 If you add 1 → it wraps to -128 #FullStackDeveloper #JavaJourney #AppAcademy #BackendDevelopment #SoftwareEngineering #CodingLife
To view or add a comment, sign in
-
-
🚀 Day 12 – Java Full Stack Journey | Deep Dive into Data Types & Operators Today’s session was all about understanding how Java actually evaluates expressions behind the scenes. Not just writing code — but thinking like the compiler. 🧠 --- 🔹 1️⃣ Integer Division & Truncation int a = 25; int b = 2; System.out.println(a / b); // 12 ✔ int / int → result is always int ✔ Decimal part gets truncated (12.5 → 12) Java does not round — it truncates. --- 🔹 2️⃣ Type Promotion in Arithmetic Operations In Java, when arithmetic operators are used: > The result is promoted to the larger data type. byte x = 5; x = x * 10; // ❌ Error Why? Because x * 10 becomes int, and Java cannot store int into byte without explicit casting. --- 🔹 3️⃣ Assignment Operators (+=, -=, *=, /=) byte x = 5; x += 10; // ✅ Works ✔ Assignment operators automatically handle implicit casting. ✔ Important difference from normal arithmetic expressions. --- 🔹 4️⃣ Pre & Post Increment – Real Understanding ++x // Pre → Change first, then use x++ // Post → Use first, then change Mastering this avoids logical mistakes in complex expressions. --- 🔹 5️⃣ Octal & Integer Literals Trap int y = 010; // Octal → Decimal 8 ✔ A leading 0 makes it octal ✔ Small details can completely change the output --- 💡 Today’s Realization Java is strict about: • Data types • Type casting • Operator behavior • Expression evaluation Small misunderstandings can lead to wrong outputs — especially in interviews. Learning to analyze before assuming the output. 🚀 TAP Academy #Day12 #Java #FullStackJourney #CoreJava #DataTypes #TypeCasting #JavaDeveloper #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 8 – Core Java | Data Types from Memory Perspective Today’s session completely changed the way I look at data types. Instead of memorizing int, float, long, we understood why data types exist. 🔑 Key Learnings: ✔ RAM is a collection of bytes ✔ Bytes → Bits → Transistors ✔ Transistors understand only 0 & 1 ✔ Real-world data must be converted into binary before storage 💡 Big insight: Data types are not “types of data” Data types are converters that transform real-world data into 0s and 1s ✔ Understood primitive data types: Integer: byte, short, int, long Real numbers: float, double char, boolean ✔ Learned why multiple integer data types exist → Memory efficiency matters → Small choices scale massively in real applications ✔ Practical understanding of: Memory allocation Data ranges Why correct data type selection matters in industry 🎯 Interview takeaway: Don’t give textbook answers. Explain concepts from memory & system perspective. This session laid the foundation for thinking like a real developer, not just writing code 🚀 #CoreJava #DataTypes #MemoryManagement #JavaFundamentals #DeveloperMindset #LearningJourney
To view or add a comment, sign in
-
Everyone talks about how to use the this keyword in Java… but very few explain why we actually need it. Let’s understand this with a simple thought process 👇 🔴 The Problem (without this) Imagine a class where the constructor parameters have the same name as instance variables. Java gets confused: 👉 Am I assigning the value to the variable inside the constructor or to the object itself? Result? The object’s data does not get updated the way you expect. 🟡 What’s Really Going Wrong? Java always gives priority to local variables over instance variables. So without clarity, the object never knows “which variable is mine?” 🟢 The Solution (this keyword) The this keyword clearly tells Java: 👉 “I’m talking about the current object’s variable.” Now the assignment is clear, clean, and correct. 💡 In simple words: this removes confusion between object data and temporary data. Once you understand why this exists, you’ll never forget how to use it. #Java #JavaBasics #OOP #LearningJava #ProgrammingConcepts #CodingJourney
To view or add a comment, sign in
-
-
𝐓𝐡𝐢𝐬 𝐜𝐨𝐝𝐞 𝐬𝐧𝐢𝐩𝐩𝐞𝐭 𝐢𝐬 𝐞𝐧𝐨𝐮𝐠𝐡 𝐭𝐨 𝐜𝐨𝐧𝐟𝐮𝐬𝐞 𝐬𝐨𝐦𝐞𝐨𝐧𝐞 𝐰𝐢𝐭𝐡 𝐉𝐚𝐯𝐚. 😁 Let me introduce you to the concept of wrapper classes and autoboxing first. 𝐖𝐑𝐀𝐏𝐏𝐄𝐑 𝐂𝐋𝐀𝐒𝐒𝐄𝐒: These are classes used to wrap primitive values so we can treat them like objects. This is essential for the Collection framework (like ArrayList), which only works with objects. 𝐀𝐔𝐓𝐎𝐁𝐎𝐗𝐈𝐍𝐆: The process of converting primitive values to wrapper objects is implicit. We don't need the new keyword. Now let's discuss the code: 𝐂𝐎𝐃𝐄 1: 𝘪𝘯𝘵 𝘢 = 100; 𝘪𝘯𝘵 𝘣 = 100; These are primitive values. For primitives, the == operator compares the actual values. Since both are 100, the output is 𝐭𝐫𝐮𝐞. 𝐂𝐎𝐃𝐄 2: Integer wrapper classes have an Integer Cache. By default, Java caches all Integer objects in the range of -128 to 127. 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘱 = 100; 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘲 = 100; Since 100 is in the cache, Java points both variables to the same object memory location. The output is 𝐭𝐫𝐮𝐞. 𝐂𝐎𝐃𝐄 3: 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘺 = 128; 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘻 = 128; Because 128 is outside the cache range, Java creates two distinct objects in memory. Since == compares memory references for objects, the output is 𝐟𝐚𝐥𝐬𝐞. ~Anuprash Gautam 🍵 𝘒𝘦𝘦𝘱 𝘭𝘦𝘢𝘳𝘯𝘪𝘯𝘨 𝘢𝘯𝘥 𝘣𝘳𝘦𝘸𝘪𝘯𝘨 𝘤𝘰𝘧𝘧𝘦𝘦.😊 #Java #programming #autoboxing #oops
To view or add a comment, sign in
-
-
Headline: Back to Basics: Visualizing the "Stack" vs. "Heap" in Java 🧠📝 "Why did my original array change when I only modified the copy?" If you’ve ever debugged a "ghost" issue where data seemed to change on its own, you’ve likely run into the difference between Primitive Types and Reference Types. Sometimes, the best way to really cement these concepts is to close the IDE and draw it out on paper. I sketched this diagram to visualize exactly what happens under the hood in memory: 1️⃣ The Stack (Primitives) When you assign int num2 = num1, Java creates a distinct copy in the Stack. They are independent neighbors. Changing num2 leaves num1 untouched. 2️⃣ The Heap (References) Arrays are objects. When you do int B[] = A, you aren't copying the data; you are copying the reference (the address). As shown in my sketch, both A and B point to the exact same memory block in the Heap. The Result: If you change B[3], you are changing A[3] too. Mastering this flow—knowing when you are passing a value vs. sharing a reference—is day one stuff, but it remains crucial for writing bug-free code at any level. Who else finds that drawing out the Call Stack and Heap makes complex logic much easier to digest? 👇 #Java #SoftwareEngineering #DataStructures #CodingLife #MemoryManagement #LearningToCode
To view or add a comment, sign in
-
-
#Day 64:- LeetCode 1200 #Code import java.util.*; class Solution { public List<List<Integer>> minimumAbsDifference(int[] arr) { // Step 1: Sort the array to bring numerically close values together Arrays.sort(arr); List<List<Integer>> result = new ArrayList<>(); int minDiff = Integer.MAX_VALUE; // Step 2: Iterate once to find the minimum difference and collect pairs for (int i = 0; i < arr.length - 1; i++) { int currentDiff = arr[i+1] - arr[i]; if (currentDiff < minDiff) { // Found a new smaller difference; reset result and update minDiff minDiff = currentDiff; result.clear(); result.add(Arrays.asList(arr[i], arr[i+1])); } else if (currentDiff == minDiff) { // Found another pair with the same minimum difference result.add(Arrays.asList(arr[i], arr[i+1])); } } return result; } } #Day 63/100: Today I solved LeetCode 1200 - Minimum Absolute Difference. In software engineering, we often face problems that look like O(N^2) at first glance. Finding the 'closest' elements in a dataset is a classic example. But by introducing Order (Sorting), we can collapse that complexity significantly.
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