Java Concept Explained Simply: final with Objects Many developers get confused about what happens when we use final with objects in Java. Let’s break it down with a simple example class Person { String name = "Alex"; } public class Test { public static void main(String[] args) { final Person p = new Person(); p.name = "Sam"; System.out.println(p.name); } } Explanation: The keyword final means you cannot reassign the reference p to another object. ✅ p = new Person(); → ❌ Compilation Error But you can modify the internal state of the object that p is pointing to. ✅ Changing p.name is perfectly allowed. So, when we run this program: 👉 Output: Sam #Java #CodingConcepts #InterviewPreparation #JavaDeveloper #LearnWithExample
Java final with objects: A simple explanation
More Relevant Posts
-
Feels Good To Be Back!! Today, I wrote a simple Java program that takes a character input and checks whether it’s a vowel or consonant. It’s a small yet fundamental logic-building exercise that strengthens understanding of conditional statements, character handling, and Scanner input in Java. KEY POINTS ‣User Input Handling: ‣Character Normalization: ‣Conditional Logic: ‣Logical OR Operator: ‣Input Validation Concept (optional to add): ‣Efficient and Simple Design: ‣Resource Management: Here is the code snippet!👇 #Java #LearningToCode #Practice #Buildinpublic #JavaDevelopment
To view or add a comment, sign in
-
-
✨ Difference Between String and StringBuffer In Java, both String and StringBuffer are used to handle text data. However, they differ in mutability, performance, and thread-safety — which makes choosing the right one important for your application. 💡 🧩 1️⃣ String Immutable → Once created, it cannot be changed. Every modification (like concatenation) creates a new object. Slower when performing many modifications. Not thread-safe (since it doesn’t change, this isn’t a problem). ⚙️ 2️⃣ StringBuffer Mutable → Can be modified after creation. Performs operations (append, insert, delete) on the same object. Faster for repeated modifications. Thread-safe → All methods are synchronized. Use String when the content never changes. Use StringBuffer when your program modifies text frequently — especially in multi-threaded applications. Thank you to Anand Kumar Buddarapu Sir for guiding me through this concept and helping me understand Java fundamentals more deeply. #Java #StringVsStringBuffer #CodingBasics #LearningJourney
To view or add a comment, sign in
-
-
💡 Difference between == and .equals() in Java — and why it still confuses even experienced devs In Java, many developers think == and .equals() do the same thing... but they don’t 👇 ⚙️ == — The == operator compares memory references. It checks whether two variables point to the same object. String a = new String("Java"); String b = new String("Java"); System.out.println(a == b); // false 🚫 Here, a and b are two different objects, even if their content is identical. 🧠 .equals() — The .equals() method compares the logical content of the objects (when properly implemented). System.out.println(a.equals(b)); // true ✅ Both Strings contain “Java”, so the result is true. 🧩 Extra tip: Primitive types (int, double, boolean, etc.) use == because they don’t have .equals(). Objects (String, Integer, List, etc.) should use .equals() unless you need to check if they’re the same object in memory. 💬 Conclusion: Use == ➡️ to compare references Use .equals() ➡️ to compare values 💭 Have you ever fallen into this trap? Share your experience below 👇 #Java #Backend #CleanCode #DeveloperTips #SpringBoot #Programming #Learning
To view or add a comment, sign in
-
🔍 Java Trivia: Can an Interface Have a main() Method? interface Main { public static void main(String[] args) { System.out.println("Hello from interface!"); } } 🧠 Here's a quirky little Java snippet. No class. Just an interface. And yet—it has a main() method. Now the real question is: Will this compile and run? Will it throw a NoClassDefFoundError? Will the JVM complain about missing public class Main? Or will it print "Hello from interface!" like a rebel? 💬 Drop your answer in the comments: ✅ What will be the output? ❌ Will it fail at compile-time or runtime? 🧪 Have you ever used an interface main() for dry-run demos or utility testing? Let’s see who’s got their Java fundamentals dialed in 🔥 #Java #InterviewPrep #CodeTrivia #DryRunLogger #InterfaceMagic #AskDinesh Waiting for your comments….
To view or add a comment, sign in
-
Reverse of a number : To reverse a given number in Java, use a loop to extract digits from the number one by one and build the reversed number. Here’s a clear Java program using a while loop, commonly recommended for beginners. import java.util.Scanner; public class ReverseNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number to reverse: "); int number = scanner.nextInt(); int reverse = 0; while (number != 0) { int digit = number % 10; // Get last digit reverse = reverse * 10 + digit; // Shift and add number /= 10; // Remove last digit } System.out.println("Reversed Number: " + reverse); } } Explanation: The loop extracts the last digit using number % 10.It multiplies the reversed number by 10 and adds the extracted digit.The original number is divided by 10 in each iteration to remove the last digit. #JavaProgramming #LearnJava #JavaBasics #CodingChallenge #ProgrammingTips #CodeNewbie #DeveloperLife #100DaysOfCode #JavaDeveloper #SoftwareDevelopment #CodingPractice #TechLearning #ProgrammingCommunity
To view or add a comment, sign in
-
💻 public static void main(String[] args) 🌟 Every Java application needs a starting line, and that's precisely what the public static void main(String[] args) method is! Think of it as the main switch 💡 that the Java Virtual Machine (JVM) flips to kick off your program's execution. Without it, your code is just a collection of instructions with no starting point! Here is a spotlight on each component's vital role: • public 🔓: This is an access modifier. It makes the method accessible from anywhere, including outside the class. The JVM needs to call this method to start your program, so it must be publicly accessible. • static 🏛️: This keyword means the method belongs to the class itself, rather than any specific object of that class. This is crucial because it allows the JVM to call the main method directly without having to first create an instance (object) of the class. • void 🛑: This specifies the return type is empty. The main method does not return any value. Once the program's execution within this method is complete, the program simply terminates. • main 🎯: This is the standard, recognized name for the entry point method in Java. The JVM specifically looks for a method named main to begin execution. Don't change this name! • (String[] args) 🗣️: This declares a parameter that is an array of String objects. This array is how your program receives command-line arguments that users can pass to the Java program when it's executed. #Java #Programming #JDK #JRE #JVM #CoreJava Codegnan Anand Kumar Buddarapu
To view or add a comment, sign in
-
-
Method overloading in Java is when a class has multiple methods with the same name but different parameters (either in number or type). This allows you to perform similar but slightly different tasks using the same method name, improving code readability and reducing redundancy. java example : class Calculator { // Adds two integers public int add(int a, int b) { return a + b; } // Adds three integers public int add(int a, int b, int c) { return a + b + c; } // Adds two double values public double add(double a, double b) { return a + b; } } public class Test { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 10)); // calls add(int, int) System.out.println(calc.add(5, 10, 15)); // calls add(int, int, int) System.out.println(calc.add(5.5, 3.2)); // calls add(double, double) } } Here, the add method name is overloaded with different parameter lists. The compiler decides which method to call based on arguments given. Summary: Method overloading means same method name, different parameters.Improves code clarity; no need for different method names for similar actions.Compiler selects correct method based on argument types/count. #Java #MethodOverloading #ProgrammingConcepts #CodingTips #JavaBasics #JavaDevelopment #100DaysOfCode #Day6ofcoding
To view or add a comment, sign in
-
Reverse of a string in Java This method is like rewriting a word from the end to the beginning, letter by letter class ReverseString { public static void main(String[] args) { String original = "Hello"; String reversed = ""; for (int i = 0; i < original.length(); i++) { reversed = original.charAt(i) + reversed; } System.out.println("Reversed String: " + reversed); } } #java #Coding
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