Java Tricky Question Most developers think they know what finally does… until they see this. What do you think this program prints? class Test { public static void main(String[] args) { System.out.println(test()); } static int test() { try { return 10; } finally { return 20; } } } Hint: finally always executes but does it override the value returned from try? #Java #CodingChallenge #Developers #ProgrammingPuzzle #JavaTrickyQuestions #LearnJava #BackendDeveloper
Java Tricky Question: Does finally override try return value?
More Relevant Posts
-
🚀 Constructors (Java) A constructor is a special method in a class that is automatically called when an object of that class is created. Its purpose is to initialize the object's state, setting initial values for its attributes. Constructors have the same name as the class and do not have a return type. If you don't define a constructor, Java provides a default constructor with no arguments. Constructors ensure that objects are properly initialized before they are used. #Java #JavaDev #OOP #Backend #professional #career #development
To view or add a comment, sign in
-
-
Java Concept: Constructor Chaining in Inheritance Looks like a simple code, but it’s one many developers overlook class A { A() { System.out.println("A constructor"); } } class B extends A { B() { System.out.println("B constructor"); } } public class Test { public static void main(String[] args) { new B(); } } Explanation: When new B() is called — Java first calls A’s constructor using an implicit super() call. Then executes B’s constructor. ✅ Output: A constructor B constructor #Java #OOPs #CodingTips #LearningJava #InterviewReady #Developers
To view or add a comment, sign in
-
Java devs, quick question on null checks! 🚩 I often see two styles for null checking an object's method result: if (object.getValue() != null) if (null != object.getValue()) My team lead suggests that using null != object.getValue() helps avoid NullPointerExceptions if object is null, while object.getValue() != null won't. But from what I understand, both can throw a NullPointerException if the object itself is null because the method is called on a null reference. So, is this a matter of style only? Or is there some deeper reason or best practice behind preferring one over the other? How do you handle null checks? Would love to hear your views and experiences! #Java #NullCheck #CodingBestPractices #SoftwareEngineering #JavaTips
To view or add a comment, sign in
-
🚀 String Concatenation (Java) String concatenation is the process of combining two or more strings into a single string. In Java, the `+` operator is used for string concatenation. When one of the operands is a string, Java automatically converts the other operand to a string and concatenates them. String concatenation is a common operation for building dynamic messages and formatting output. Learn more on our website: https://techielearns.com #Java #JavaDev #OOP #Backend #professional #career #development
To view or add a comment, sign in
-
-
🚀 Day 4 — The Java Trick That Even Seniors Miss! 😮 Every Java dev thinks they understand method overloading... But this one small detail breaks their confidence 👇 public class OverloadTest { void show(Object o) { System.out.println("Object"); } void show(String s) { System.out.println("String"); } public static void main(String[] args) { OverloadTest test = new OverloadTest(); test.show(null); } } 🧠 Question: What will be the output of this code? Will it print: 1️⃣ Object 2️⃣ String 3️⃣ Compile-time error Most developers get this wrong! 😅 💬 Drop your answer in the comments 👇 Let's see how many of you actually know how Java resolves overloaded methods at compile time! #Java #CodingChallenges #SpringBoot #JavaDevelopers #InterviewQuestion #Day4Challenges #cbfr
To view or add a comment, sign in
-
🚀 Java Tip of the Day: "Collection" vs "Collections" — Know the Difference! If you’ve ever mixed these two up in interviews or code reviews — you’re not alone 😅 Here’s the quick breakdown: ✅ Collection (Interface) → Part of the java.util package, it’s the root interface for List, Set, and Queue. Think of it as the blueprint that defines how groups of objects are handled. ✅ Collections (Class) → A utility class in java.util package that provides static methods to operate on collections — like sorting, searching, or synchronizing them. 👉 Example: Collections.sort(list); // Using Collections class Collection<String> names; // Using Collection interface In short: Collection = Framework’s foundation Collections = Toolkit for manipulating data 💬 What’s your favorite Collections method you use often? Drop it in the comments 👇 #Java #CodingTips #InterviewPreparation #Developers #Programming #SpringBoot #ReactJS #SoftwareEngineering
To view or add a comment, sign in
-
Modern Java Simplified: Records & Pattern Matching Write cleaner, more efficient, and readable code with Java’s evolving syntax! 🔹 Records help you reduce boilerplate and focus on data. 🔹 Pattern Matching streamlines type checks and logic flow. Modern Java = Less code, more clarity. 🚀 #Java #Records #PatternMatching #AdvancedJava #CleanCode #SoftwareDevelopment #CodingBestPractices #TechInnovation
To view or add a comment, sign in
-
-
☕ Day 13 of my “Java from Scratch” Series – “Unary Operators in Java” Unary operators are the ones that work on a single operand (variable). They’re used to change the sign, increase/decrease a value, or reverse a boolean condition. 1️⃣ +a 2️⃣ -a → if a = 5, -a will be -5 3️⃣ ! → if a = true, !a will be false 4️⃣ Increment operator (++) → increases the value by 1. a. Post increment: first the operation will be performed and then the value will be increased. 📘 Example: int a = 20; int b = a++; ✅ Result: b = 20, a = 21 b. Pre increment: first the value will be increased and then the operation will be performed. 📘 Example: int a = 20; int b = ++a; ✅ Result: a = 21, b = 21 5️⃣ Decrement operator (--) → decreases the value by 1. a. Post decrement: first the operation will be performed and then the value will be decremented. 📘 Example: int a = 20; int b = a--; ✅ Result: b = 20, a = 19 b. Pre decrement: first the value will be decremented and then the operation will be performed. 📘 Example: int a = 20; int b = --a; ✅ Result: a = 19, b = 19 💡 In short: Unary operators act on one operand to either change its value or its sign. 👉 Which one do you find tricky — pre or post increment? Comment below 👇 #Java #Programming #Coding #Tech #JavaDeveloper #SoftwareEngineering #Learning #Developers #UnaryOperatorsInJava #JavaFromScratch #NeverGiveUp
To view or add a comment, sign in
-
☕ Day 11 of my “Java from Scratch” Series – "Assignment Operators in Java". In Java, Assignment Operators are used to assign values to variables. We can assign values directly or perform operations and assign them in short form. 1. 🔹 = (Simple Assignment) Assigns a value to a variable. 📘 Example: int a = 5; int b = a; Here, the value 5 is assigned to a, and the value of a is assigned to b. 2. 🔹 += (Add and Assign) Short way to write a = a + value 📘 Example: int a = 3; a += 3; ✅ Result: a = 3 + 3 => a = 6 3. 🔹 -= (Subtract and Assign) Short way to write a = a - value 📘 Example: int a = 10; a -= 3; ✅ Result: a = 10 - 3 => a = 7 4. 🔹 *= (Multiply and Assign) Short way to write a = a * value 📘 Example: int a = 10; a *= 3; ✅ Result: a = 10 * 3 => a = 30 5. 🔹 /= (Divide and Assign) Short way to write a = a / value 📘 Example: int a = 10; a /= 5; ✅ Result: a = 10 / 5 => a = 2 (Quotient) 6. 🔹 %= (Modulo and Assign) Short way to write a = a % value 📘 Example: int a = 10; a %= 5; ✅ Result: a = 10 % 5 => a = 0 (Remainder) 💡 In short: Assignment operators make your code cleaner, shorter, and easier to read. 👉 Which assignment operator do you use most often in your code? Comment below! 👇 #Java #Programming #Coding #JavaDeveloper #Learning #SoftwareEngineering #JavaFromScratch #InterviewQuestions #Developers #AssignmentOperatorsInJava #Tech #NeverGiveUp
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
True: The finally block always runs — it’s like a rule in Java that it must execute before finishing the method. False: The value from try isn’t final — because if finally also returns something, it replaces whatever try was going to return.