🚀 Understanding the var Keyword in Java I recently came across the var keyword in Java, introduced in Java 10, which allows local variable type inference. Let’s break it down 🔹 What is var? var lets the compiler automatically infer the type of a local variable based on the value assigned. It is not a new type, just a syntactic convenience. 🔹 How it Works var message = "Hello Java"; var count = 10; var list = new ArrayList<String>(); Here, the compiler infers message as String, count as int, and list as ArrayList<String>. 🔹 Impact Reduces boilerplate code and improves readability. Makes code more concise, especially for long generic types. Encourages clean, maintainable code without losing type safety. 🔹 Things to Keep in Mind Can only be used for local variables, not class fields or method parameters. Overusing var in complex expressions can hurt readability. #java #javadeveloper #coding #backenddevelopment #springboot #cleanCode #learnjava #javatips #developerexperience
Java var Keyword: Simplifying Local Variables
More Relevant Posts
-
Today I revised the static keyword in Java and cleared a few common misconceptions. - Static Block It runs once when the class is loaded, not when the JVM starts. It executes before main() and before any object is created. - Static Methods Static methods cannot be overridden. They belong to the class, so method calls are decided at compile time method hiding. - Static Variables Static variables are shared across all objects and can change. Only static final variables act like constants. Small concepts, but very important for interviews and real-world Java coding, stay tuned for more tech posts ! 🤩 #Java #LearningJava #OOP #Programming #JavaDeveloper
To view or add a comment, sign in
-
#100DayCodingChallenge #Day53 🔹 Problem Solved: Sorting Strings in Reverse Order using Java Streams I recently solved a problem where the goal was to sort a list of strings in reverse (descending) alphabetical order using Java 8 Streams. Here’s a clear breakdown of how the solution works: ✅ Explanation (Step by Step) A list of strings is created containing multiple fruit names. Instead of using traditional loops or Collections.sort(), the Java Stream API is used for a cleaner and more modern approach. The list is converted into a stream, which allows functional-style operations. A sorting operation is applied using a reverse order comparator, which changes the default ascending sort to descending. The sorted elements are then collected back into a new list. Finally, the result is printed, showing the strings sorted in reverse alphabetical order. #100daysCodingChallenge #ProblemSolving #java #Coding #Day53
To view or add a comment, sign in
-
-
LeetCode 28 – Find the Index of the First Occurrence in a String 🧠 What I learned today: Sometimes the smartest solution is knowing when NOT to overthink 😌 Java’s built-in indexOf() does exactly what the problem asks—clean, efficient, and readable. 💡 Solution Insight: Returns the first index of needle in haystack Automatically returns -1 if not found Time saved = energy saved ⚡ 😄 Fun note: Why write 20 lines of logic when one method call can do the job? indexOf() today, custom logic tomorrow 😉 📈 Consistency > Complexity On to Day 4! 🔥 #100DaysOfCode #DSA #Java #LeetCode #CodingJourney #ProblemSolving #Consistency #LearnByDoing
To view or add a comment, sign in
-
-
Behind the scenes: How Java objects are created:- What happens step by step inside the JVM: The Java source code is compiled and .class files are generated. When the program runs, Animal.class and AnimalMain.class are loaded into the Method Area (Metaspace). A java.lang.Class object is created in the Heap, which holds runtime metadata of the class. JVM creates the main thread and its stack. When, new Animal() is executed: Memory is allocated in the Heap Area. All instance variables are initialized with default values (age = 0, legs = 0). The constructor is executed. A reference to the newly created object is returned. This reference is stored in the stack variable buzo. Note:- Heap → stores objects Stack → stores references and method frames Method Area / Metaspace → stores class metadata Important: ->The reference variable does not store the actual object or a memory address. ->HashCode is generated only when hashCode() is called, not during object creation. Learning Java internals makes concepts much clearer. #Java #JVM #CoreJava #ObjectOrientedProgramming #ComputerScience #objects
To view or add a comment, sign in
-
-
#Day-105) LeetCode Problem #944: Delete Columns to Make Sorted ✨ Today I solved an interesting problem that checks whether columns in a grid of strings are lexicographically sorted. If not, we count how many need to be deleted. 🔹 Approach: Iterate column by column Compare characters row by row Increment count if any column is unsorted 💻 Java Solution: (Attached in the image 👇) 🔑 Takeaway: This problem reinforces the importance of string manipulation and nested iteration logic. Even simple-looking problems can sharpen our ability to think in terms of grids and constraints. #LeetCode #Java #ProblemSolving #CodingChallenge #MERN #BackendDevelopment #LearningEveryday
To view or add a comment, sign in
-
-
If you think “𝐢𝐧𝐭𝐞𝐫𝐟𝐚𝐜𝐞𝐬 𝐬𝐨𝐥𝐯𝐞𝐝 𝐢𝐭 𝐜𝐨𝐦𝐩𝐥𝐞𝐭𝐞𝐥𝐲” — read that again. The Diamond Problem doesn’t just disappear in Java. It can still exist conceptually with interfaces, and Java handles it very intentionally. This short doc. explains: • Where ambiguity can return with interfaces • Why default methods matter • How Java prevents the issue at compile time If you care about understanding Java beyond surface-level rules, this one’s worth a quick read. #Java #CoreJava #JavaConcepts #OOP #Interfaces #SoftwareEngineering #LearnJava #Programming #JavaDevelopers #LearnJava
To view or add a comment, sign in
-
💡 Day 28/30 — Java Memory Model: how threads see shared data Today I learned how the Java Memory Model (JMM) defines what happens when multiple threads read and write shared variables. [Video Link] - https://lnkd.in/gv8ZmzWB 🔄 Key concepts 🔹 Visibility A write in one thread is not immediately visible in another. 🔹 Happens-before If A happens-before B, then: Writes in A are visible in B This ensures correctness. 🧯 Tools to enforce ordering ✔ volatile volatile boolean running = true; Makes writes visible across threads. ✔ synchronized enter monitor critical section exit monitor Prevents race conditions and ensures visibility. ✔ Atomic classes CAS operations for non-blocking safety. 🚫 Without JMM You get: stale reads race conditions unpredictable behavior ⭐ Key takeaway The JMM ensures safety in multi-threaded programs by defining visibility and ordering rules. Tomorrow: Day 29 — Atomic classes & Locks #Java #Concurrency #JMM #Threads #SoftwareEngineering #LearningChallenge
DAY 28 Of "30 Day 30 Interesting Fact about Java Challenge- JMM. #java #challenge #coding
https://www.youtube.com/
To view or add a comment, sign in
-
🚀 Day 9 – Java Strings vs StringBuffer ☕ 🔥 Today’s practice was all about understanding immutability vs mutability in Java using String and StringBuffer. 📌 Key Learnings from the code: 🔹 String • Can be created using String literal or new keyword. • Immutable → any modification creates a new object. • concat() does not change the original string unless reassigned. • equals() compares content, so it works as expected. 🔹 StringBuffer • Mutable → changes happen in the same object. • append() modifies the existing object. • equals() does not compare content (it behaves like ==), so it checks reference, not value. 💡 Important Insight Even if two StringBuffer objects contain the same text, equals() returns false unless they refer to the same object. ✨ I’m grateful to Prasoon Bidua Sir for the continuous mentorship and clear explanations, which made learning these concepts more engaging and practical. This practice helped me clearly understand memory behavior, object references, and method behavior in Java. 🔗 Code implementation for today’s task: https://lnkd.in/eCSDT482 📈 Consistency matters—learning one concept deeply every day! #Day9 #Java #String #StringBuffer #OOP #JavaBasics #LearningInPublic #ProgrammingJourney #CoreJava
To view or add a comment, sign in
-
-
Day 4 – Java Internals 🧠 #Bytecode + JVM = Java’s superpower 💪 how Java code executes behind the scenes? Compiler → Bytecode → JVM → Interpreter → Output This is the secret behind Java’s platform independence. #Compiler: 1. Compiler is an intermediate software which takes source code as an input. 2. Compiler compiles the whole source code at once. 3. It checks for syntactical mistakes, if found it throws a compile time error. 4. Compiler displays all error at once. 5. If there is no syntactical mistake in the code compiler generates a new .class file i.e. known as byte code. #Bytecode: 1. Byte code is an intermediate file generated by the compiler. 2. Byte code name is same as class name. 3.Same bytecode runs on all OS. #JVM: JVM executes bytecode using 1. Class Loader 2. Bytecode Verifier 3. Interpreter 4. JIT Compiler #Interpreter : 1. It checks for the logical mistake. 2. It displays one error at a time. 3. Error given by the interpreter is known as runtime error. Compiler creates bytecode, JVM executes it — that’s the real power of Java. #Java #JVM #JavaDeveloper #LearnJava #BackendDevelopment #Programming #JavaDaily
To view or add a comment, sign in
-
-
📘 Java – Day 04 | JVM Execution Engine How does JVM actually execute our Java code after loading it into memory? Today I learned about the Execution Engine, which is the part of JVM that actually runs our program. Here’s how execution happens in simple terms: - Interpreter starts executing bytecode line by line - Simple but slower because the same code may be interpreted again and again - JIT Compiler (Just-In-Time) improves performance - It detects frequently executed code (hot code) - Converts it into native machine code once - Reuses it directly next time → faster execution - Garbage Collector (GC) runs automatically - Frees heap memory by removing objects that are no longer referenced - Helps prevent memory leaks - JNI (Java Native Interface) acts as a bridge - Allows Java to interact with native code written in C/C++ - Native Method Stack handles execution of these native calls Understanding this explained why Java programs may feel slower at startup but become faster as they continue running. #Java #JVM #ExecutionEngine #CoreJava
To view or add a comment, sign in
-
More from this author
Explore related topics
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