Java finally made a 𝗳𝗶𝗻𝗮𝗹 keyword mean 𝗙𝗶𝗻𝗮𝗹 and everyone is talking about it... Previously, the final keyword in Java indicated that a variable's value cannot be changed once initialized. However, through the use of the deep reflection 𝘀𝗲𝘁𝗔𝗰𝗰𝗲𝘀𝘀𝗶𝗯𝗹𝗲 and 𝘀𝗲𝘁 methods of the 𝗷𝗮𝘃𝗮.𝗹𝗮𝗻𝗴.𝗿𝗲𝗳𝗹𝗲𝗰𝘁.𝗙𝗶𝗲𝗹𝗱 class, it was possible to override this behavior. Consider the following example: ``` // A normal class with a final field class C { final int x; C() { x = 100; } } // 1. Perform a deep reflection over the final field in C java.lang.reflect.Field f = C.class.getDeclaredField("x"); f.setAccessible(true); // Make C's final field mutable // 2. Create an instance of C C obj = new C(); System.out.println(obj.x); // Prints 100 // 3. Mutate the final field in the object f.set(obj, 200); System.out.println(obj.x); // Prints 200 f.set(obj, 300); System.out.println(obj.x); // Prints 300 ``` However, with the upcoming Java 26 release, the final keyword will truly mean final. This change is one of the intriguing updates in Java 26, and I look forward to discussing more of the new features. For further details, check out the official JEP: https://lnkd.in/gsKzzr6R #java
Java 26 Final Keyword Truly Means Final
More Relevant Posts
-
I thought my Java code was efficient… until it slowed down at scale. I was using this inside a loop: String result = ""; for (int i = 0; i < 10000; i++) { result += i; } Worked fine for small data. But with large input? Painfully slow. 💡 Why? Because Strings are immutable in Java. Every “+” creates a NEW object. So this loop created thousands of objects. The fix? Use StringBuilder: StringBuilder result = new StringBuilder(); for (int i = 0; i < 10000; i++) { result.append(i); } 💡 Deeper insight: Performance issues often hide in “simple” code. This wasn’t a syntax issue. It was a memory + object creation problem. ✅ Practical takeaway: In Java: • Use StringBuilder for loops • Avoid string concatenation in heavy operations • Think about object creation cost That one change made my code 10x faster.
To view or add a comment, sign in
-
Starting from JDK25, you can write a simple Java entry point (JVM main method) program as, ``` void main() { IO.println("Hello, World!"); } ``` and more interactively, ``` void main() { String name = IO.readln("Please enter your name: "); IO.print("Pleased to meet you, "); IO.println(name); } ``` 1. Both the above variants are single-file source-code programs (compact source file) 2. Automatically imports commonly used base packages such as java.io, java.math, and java.util via automatic import of the java.base module 3. To evolve a compact source file into a ordinary source file, all you need to do is wrap its fields and methods in an explicit class declaration and add an import declaration Assuming this program is in the file HelloWorld.java, you can run it directly with the source-code launcher: $ java HelloWorld.java Previously, the above simple Java program would look like below, ``` public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } ``` where, 1. "public" access modifier & "class" declaration provides for proper encapsulation boundaries 2. "static" modifier provides for a class-object modelling 3. String[] args parameter provides for the program execution inputs 4. System.out.println provides utilities for printing to the console More details in the JEP512 - https://lnkd.in/g5JBAWwe advocating the simplification
To view or add a comment, sign in
-
-
Why Java uses references instead of direct object access ? In Java, you never actually deal with objects directly. You deal with references to objects. That might sound small - but it changes everything. When you create an object: You’re not storing the object itself. You’re storing a reference (address) to where that object lives in memory. Why does Java do this? 1️⃣ Memory efficiency Passing references is cheaper than copying entire objects. 2️⃣ Flexibility Multiple references can point to the same object. That’s how shared data and real-world systems work. 3️⃣ Garbage Collection Java tracks references - not raw memory. When no references point to an object, it becomes eligible for cleanup. 4️⃣ Abstraction & Safety Unlike languages with pointers, Java hides direct memory access. This prevents accidental memory corruption. When you pass an object to a method, you’re passing the reference by value - not the object itself. That’s why changes inside methods can affect the original object. The key idea: Java doesn’t give you objects. It gives you controlled access to objects through references. #Java #JavaProgramming #CSFundamentals #BackendDevelopment #OOP
To view or add a comment, sign in
-
-
Day 3 of practicing Java basics. Today I worked on reversing a Map (swapping key and value). A small exercise, but good for understanding how Map and entrySet() work internally. Focusing on strengthening core concepts step by step. ================================================= // Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; class Main { public static void main(String[] args) { Map<String,Integer>ogMap=new HashMap<String,Integer>(); ogMap.put("EmpId of Madhukar",11011); ogMap.put("EmpId of Jatin",13013); ogMap.put("EmpId of Akshay",15031); System.out.println(ogMap); Map<Integer,String>reverseMap=new HashMap<Integer,String>(); for(Map.Entry<String,Integer> data :ogMap.entrySet()) { reverseMap.put(data.getValue(),data.getKey()); } System.out.println(reverseMap); } }
To view or add a comment, sign in
-
-
Two Java strings look exactly the same… But sometimes == returns false. Why? The answer lies in String Pool and Heap memory. 👉 What is a String literal? A string literal is a value written directly in quotes. Example: String s1 = "hello"; 👉 What is String Pool? String Pool is a special memory area in Java where string literals are stored and reused. 👉 What is Heap memory? Heap is the memory where objects are created at runtime. 👉 Example: String s1 = "hello"; String s2 = "hello"; Java checks the pool: "hello" already exists → reuse it So: s1 == s2 → true ✅ 👉 Now this: String s3 = new String("hello"); String s4 = new String("hello"); new always creates objects in Heap. So: s3 == s4 → false ❌ (different references) s3.equals(s4) → true ✅ (same value) 👉 Important point • String Pool manages memory (reuse objects) • == compares reference (same object or not) • equals() compares value (same content or not) 👉 Simple way to remember "hello" → reused from pool new String("hello") → always new object 👉 Real-world Java example: public class Test { public static void main(String[] args) { String a = "java"; String b = "java"; String c = new String("java"); System.out.println(a == b); // true System.out.println(a == c); // false System.out.println(a.equals(c)); // true } } 👉 Conclusion String Pool helps save memory, while == and equals() behave differently based on reference vs value. Understanding this avoids common bugs in Java. Had you come across this before? #Java #BackendEngineering #JavaTips #SoftwareEngineering #LearningInPublic
To view or add a comment, sign in
-
Day 2 of improving my Java basics. Tried something different today — finding the length of a string without using length() method. Used exception handling and charAt() to figure it out. Small problem, but it really made me think about how things work internally. =============================================== // Online Java Compiler // Use this editor to write, compile and run your Java code online class Main { public static void main(String[] args) { String s="Madhukar Pandey"; int count =0; boolean b=true; while(b) { try { s.charAt(count); count++; } catch(StringIndexOutOfBoundsException e ) { System.out.println("Length of string is:"+count); b=false; } } } }
To view or add a comment, sign in
-
-
Is Java a compiled language or an interpreted one? Both. Confused? Yes, Java is both compiled as well as interpreted language. This is what makes it platform-independent. Java Code >> Compile (javac) >> Bytecode (portable code) >> Interpret (platform specific) >> Machine Code >> Execute. Normal: 1. Java Compiler (javac) compiles Java code to Bytecode (which is platform-independent). 2. Java Interpreter (java) interprets this bytecode (line-by-line) & convert it to Machine language. 3. Execute. Exception: JIT (Just In Time) Compiler 1. JVM maintains the count for no of times a function is executed. 2. If it exceeds the limit then JIT directly compiles the Java code into Machine language. No Interpretation. In General, • Compile: Source code >> Optimized Object Code (can be machine code or other optimized code) • Interpret: Source Code >> Machine Code (to be executed)
To view or add a comment, sign in
-
I’ve compiled 5000+ REAL-TIME Interview Questions asked in top companies like PwC, Cognizant, TCS, Infosys, Deloitte, EY & startups. 🚀 Not just a question bank — a Complete Interview Preparation System ✅ Detailed, beginner-friendly answers ✅ STAR method for real-time questions ✅ Confidence-building explanation guidance ✅ Lifetime access + doubt support + FREE updates 📚 Includes: Selenium | Java (300+ Programs) | Manual Testing | BDD Cucumber | SQL | API (Postman) | Rest Assured | Git | Jenkins | Jira | Agile | Playwright | Javascript | Typescript (Upcoming) ✔ 1500+ Selenium Practical Exercises ✔ 500+ API Testing Exercises ✔ 500+ Rest Assured Exercises ✔ 100+ Behavioural & Scenario-Based Questions ✔ Real-Time Projects (Banking + E-commerce) 👩💻 Perfect for Freshers | 1–6 Years | Manual → Automation Switch 🎁 ONE PDF = COMPLETE INTERVIEW PREPARATION 🔗 Notes Link:--- https://lnkd.in/dRMaNzSk
Is Java a compiled language or an interpreted one? Both. Confused? Yes, Java is both compiled as well as interpreted language. This is what makes it platform-independent. Java Code >> Compile (javac) >> Bytecode (portable code) >> Interpret (platform specific) >> Machine Code >> Execute. Normal: 1. Java Compiler (javac) compiles Java code to Bytecode (which is platform-independent). 2. Java Interpreter (java) interprets this bytecode (line-by-line) & convert it to Machine language. 3. Execute. Exception: JIT (Just In Time) Compiler 1. JVM maintains the count for no of times a function is executed. 2. If it exceeds the limit then JIT directly compiles the Java code into Machine language. No Interpretation. In General, • Compile: Source code >> Optimized Object Code (can be machine code or other optimized code) • Interpret: Source Code >> Machine Code (to be executed)
To view or add a comment, sign in
-
🔥 Day 14: Immutable Class (How String is Immutable in Java) One of the most important concepts in Java — especially for interviews 👇 🔹 What is an Immutable Class? 👉 Definition: An immutable class is a class whose objects cannot be changed once created. 🔹 Example: String String s = "Hello"; s.concat(" World"); System.out.println(s); // Hello (not changed) 👉 Why Because String is immutable 🔹 How String Becomes Immutable? ✔ String class is final (cannot be extended) ✔ Internal data is private & final ✔ No methods modify the original object ✔ Any change creates a new object 🔹 Behind the Scenes String s1 = "Hello"; String s2 = s1.concat(" World"); System.out.println(s1); // Hello System.out.println(s2); // Hello World 👉 s1 remains unchanged 👉 s2 is a new object 🔹 Why Immutability is Important? ✔ Thread-safe (no synchronization needed) ✔ Security (safe for sharing data) ✔ Caching (String Pool optimization) ✔ Reliable & predictable behavior 🔹 How to Create Your Own Immutable Class? ✔ Make class final ✔ Make fields private final ✔ No setters ✔ Initialize via constructor only ✔ Return copies of mutable objects 🔹 Real-Life Analogy 📦 Like a sealed box — once created, you cannot change what’s inside. 💡 Pro Tip: Use immutable objects for better performance and safety in multi-threaded applications. 📌 Final Thought: "Immutability = Safety + Simplicity + Performance" #Java #Immutable #String #Programming #JavaDeveloper #Coding #InterviewPrep #Day14
To view or add a comment, sign in
-
-
Can you update a private final variable in Java using Reflection? Most developers will say NO… But the real answer is a bit more interesting 👇 ⸻ In Java: • private → restricts access within the class • final → prevents reassignment after initialization So ideally, a private final variable should remain unchanged ❌ ⸻ 🔍 But Reflection changes the game… It allows you to bypass access control and even modify a final field: class Person { private final int age = 21; } Field field = Person.class.getDeclaredField("age"); field.setAccessible(true); Person p = new Person(); field.set(p, 30); System.out.println(field.get(p)); // 30 😮 ⚠️ Before you get excited… read this: • JVM may inline final values → results can be unpredictable • Behavior can differ across Java versions • Breaks immutability and encapsulation • Can introduce hard-to-debug issues ⸻ 🚫 Why you should avoid this in real projects: ❌ Violates clean code principles ❌ Unsafe and not future-proof ❌ Makes your codebase harder to maintain ⸻ 🚀 Better approach: If a value needs to change: 👉 Don’t mark it final 👉 Or redesign your class for proper mutability
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