A variable is a container used to store data that can change during the execution of a program. In Java, every variable must have a data type, which defines what kind of data it can store (like int, double, String, etc.). 🔸 Types of Variables in Java 1️⃣ Local Variable A local variable is declared inside a method, constructor, or block. ✔️ Scope: Accessible only within that method/block ✔️ Lifetime: Exists only while the method is executing ✔️ Must be initialized before use 2️⃣ Instance Variable An instance variable is declared inside a class but outside any method. ✔️ Scope: Accessible throughout the class ✔️ Lifetime: Exists as long as the object exists ✔️ Each object has its own copy 🔎 Quick Difference 🔹 Local Variable → Belongs to a method 🔹 Instance Variable → Belongs to an object #Java #Programming #SoftwareDevelopment #Coding #LearningJourney #100DaysOfCode
Java Variable Types: Local & Instance Variables
More Relevant Posts
-
Refactoring for Clarity: Array Manipulation in Java 👨💻 I’ve just finished a practical exercise on array manipulation. While the goal was simple (summing two arrays), I used it as an opportunity to apply improvements. - Method decomposition: Separated concerns into specialized methods (Read, Calculate, Display). - Input validation: Built a robust loop to handle invalid inputs and prevent crashes. - Data Formatting: Used printf to create a clear, readable results table. Small improvements in logic and organization make a huge difference in software quality. #Java #Algorithms #CleanCode #Backend #LearningToCode
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
-
Functional Optics for Modern Java – Part 6: From Theory to Practice. In the final instalment of the series, Magnus Smith brings the concepts of functional optics into real-world Java development. The blog explores how ideas like composability and effect-aware updates can move beyond theory and be applied to complex domain models in a practical, maintainable way. A thoughtful conclusion to the series that balances functional programming principles with the realities of modern Java. Read the full blog here: https://buff.ly/tvH7t66 #ModernJava #FunctionalOptics #JavaDevelopement
To view or add a comment, sign in
-
When an object is created in Java, it needs some initial values to start working. Who assigns those values? 𝑆𝑎𝑑𝑙𝑦… 𝐽𝑎𝑣𝑎 𝑑𝑜𝑒𝑠𝑛’𝑡 𝑟𝑒𝑎𝑑 𝑜𝑢𝑟 𝑚𝑖𝑛𝑑𝑠 𝑦𝑒𝑡 😅 That’s where constructors come in. ⚙️ 𝐂𝐨𝐧𝐬𝐭𝐫𝐮𝐜𝐭𝐨𝐫𝐬 A constructor is a special method in Java that is automatically executed when an object is created. Its main purpose is to initialize the object with the required values. For example, when we create a mobile object 📱, it may need values like: • brand • price Without constructors, we would have to create the object first and then assign values separately. A constructor allows us to set those values at the time of object creation, making the code cleaner and easier to manage. 🔹 𝐓𝐲𝐩𝐞𝐬 𝐨𝐟 𝐂𝐨𝐧𝐬𝐭𝐫𝐮𝐜𝐭𝐨𝐫𝐬 𝐢𝐧 𝐉𝐚𝐯𝐚 • Default Constructor – assigns default values to an object • Parameterized Constructor – allows passing values while creating the object I’ve attached a simple example in the code snippet below to show how constructors work 👇 #Java #CoreJava #OOP #Constructors #Programming #LearningJourney #BuildInPublic
To view or add a comment, sign in
-
-
📺 YouTube: https://lnkd.in/du8dCrS4 Watching this session gave me a powerful insight: Java code should not be seen only as text, but as a behavior model. For years we’ve focused on syntax. But this talk showed me that to truly understand code, we need to look at three layers: 🌳 AST (Abstract Syntax Tree) → the syntactic structure of the code 🔀 CFG (Control Flow Graph) → the execution flow 🔢 Symbolic Model → operations represented as mathematical objects Take this simple example: int sum = 0; for (int i = 0; i < n; i++) { sum += i; if (sum > 10) break; } return sum; As text, it’s just a loop and a condition. As a behavior model: • 🌳 AST → for, if, sum += i, return sum • 🔀 CFG → Entry → Loop check → Body → Condition → Break/Continue → Exit • 🔢 Symbolic Model → • var.load sum • var.load i • add sum i • var.store sum • gt sum 10 → if true → java.break What did this give me? • 🚀 Optimization – spotting and removing redundant operations • ✨ Refactoring – making code more readable and maintainable • 🛡️ Safety – stronger flow and type checks • 💡 Productivity – less time wasted on debugging and analysis My takeaway: Java code is not only written, it must also be modeled to be truly understood.
Symbolic Modeling and Transformation of Java Code #JVMLS
https://www.youtube.com/
To view or add a comment, sign in
-
How to Optimize Java Memory for Large Datasets? Two aspects that we should consider when coding are: 1) The garbage collector should be able to effectively identify and remove any objects or classes that are no longer needed; 2) Memory should be used efficiently. This article explain about planning and coding for high volumes, as well as testing and live monitoring considerations: https://lnkd.in/gm5EypHf
To view or add a comment, sign in
-
-
I just completed an intensive session focused on the core of Java's efficiency: Immutable Strings and Memory Management. While we often use strings daily, understanding what happens under the hood is what separates a coder from a developer. Key Takeaways from the session: The Power of Command Line Arguments: We explored how the String[] args in the main method actually functions, learning how to pass dynamic data into applications via the CLI—a crucial skill for building professional-grade tools . Strings as Objects: In Java, strings aren't just data; they are objects . I learned the three distinct ways to initialize them: using the new keyword, using string literals, and converting character arrays . Memory Architecture (SCP vs. Heap): This was a game-changer. I now understand that Java optimizes memory by using the String Constant Pool (SCP) for literals to prevent duplicates, while the Heap Area allows for duplicate objects when the new keyword is used . The Comparison Trap: I finally mastered the difference between reference comparison and value comparison. Using == compares the memory address (reference), while the .equals() method compares the actual content . Immutability: We began exploring why certain data, like birthdays or names, are best handled as immutable strings—meaning they cannot be changed once created in memory . I'm looking forward to the next phase of this journey: Object-Oriented Programming (OOP)! . #Java #SoftwareDevelopment #Programming #MemoryManagement #TechLearning #JavaDeveloper #CodingJourney #Tapacadmey
To view or add a comment, sign in
-
-
Day 9 of Learning Java, From Single Values to Collections 📦 Till now, I was storing one value at a time. Today? I learned how to store many values together. 👉 Arrays. Because creating 100 separate variables? That’s chaos. Arrays = Organized memory. 🔥 1D Array Store multiple values in one variable. And yes… Java arrays start from index 0 (important!). 📊 2D & 3D Arrays Now things feel next level. 2D → Like a table (rows & columns). 3D → Data inside layers. Accessing elements with: arr[i][j] arr[i][j][k] Now I can literally structure data properly. 🔁 Traversal Using loops to go through each element. This is where loops + arrays connect perfectly. 💬 Introduction to Strings String str = "Hello"; Not just text… But a sequence of characters stored in memory. Big realization today? Arrays make code scalable. Clean structure = clean thinking. Day 9 and now my code can handle real data 🚀🔥 Big thanks to Aditya Tandon sir and Rohit Negi sir #Java #CoreJava #Arrays #Programming #LearningJourney #Developers
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 4/30 – Java DSA Challenge 🔎 Problem 32: 1343. Number of Sub-arrays of Size K and Average ≥ Threshold (LeetCode – Medium) Today’s problem is a classic Sliding Window (Fixed Size) pattern 🔥 🧠 Problem Statement Given: An integer array arr An integer k (window size) An integer threshold We must return the number of subarrays of size k whose average ≥ threshold. 💡 Key Insight Instead of calculating average every time: 👉 We maintain a running window sum 👉 When window size becomes k, check: (sum/k)≥threshold Even better optimization: Instead of dividing every time, we can compare: sum≥k×threshold This avoids division and makes it faster. ⏱ Time Complexity O(n) → Each element enters and leaves the window once 📦 Space Complexity O(1) → No extra data structures used 📌 Key Learning Fixed-size sliding window avoids recalculating sums Converting average condition to sum comparison improves efficiency Medium problems often test pattern clarity, not complexity 32 Problems Completed 🔥 Day 4 Going Strong 💪🚀 #Day4 #30DaysOfCode #Java #DSA #LeetCode #SlidingWindow #Arrays #ProblemSolving #CodingJourney #Consistency
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
@ @