== vs .equals() — looks same, isn’t # Java tip that saves you from silly bugs: 👉 == vs .equals() I used to think both are same… they’re not. "==" → checks memory ".equals()" → checks value Example: String a = new String("hello"); String b = new String("hello"); a == b ❌ false a.equals(b) ✅ true But: String x = "hello"; String y = "hello"; x == y ✅ true (String Pool magic) 💡 Simple rule: Use ".equals()" for comparing values. Small detail… big difference. #Java #Coding #Developers #TechTips #DesignPatterns #SystemDesign #Programming #BackendDevelopment
Java == vs equals()
More Relevant Posts
-
🚀 Java Streams: Find Numbers That Appear Only Once Here’s a common problem: 👉 Given a list of integers, find the numbers that appear only once. List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5, 6, 6); List<Integer> uniqueNumbers = numbers.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet().stream() .filter(entry -> entry.getValue() == 1) .map(Map.Entry::getKey) .collect(Collectors.toList()); System.out.println(uniqueNumbers); 🧾 Output [1, 3, 5] Streams help write concise, readable, and functional-style code — but always consider performance when choosing an approach. #Java #JavaStreams #Coding #Programming #Developers #InterviewPrep #BackendDevelopment
To view or add a comment, sign in
-
🚀 Java Streams in Action: Sum of Squares of Even Numbers Ever wondered how to write clean and functional-style code in Java? Here's a simple yet powerful example using Streams API 💡 🎯 Problem: From a list of integers, calculate the sum of squares of even numbers. 💻 Solution using Streams: List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); int sum = numbers.stream() .filter(n -> n % 2 == 0) // Step 1: Filter even numbers .map(n -> n * n) // Step 2: Square each number .reduce(0, Integer::sum); // Step 3: Sum all values System.out.println("Sum of squares of even numbers: " + sum); 🔍 How it works: filter() → selects only even numbers map() → transforms each number into its square reduce() → aggregates the result into a single sum ✨ Output: Sum of squares of even numbers: 56 🔥 Why use Streams? Cleaner and more readable code Functional programming style Easy to parallelize 💬 Have you tried solving similar problems using Streams? Share your thoughts or alternative approaches below! #Java #Streams #Coding #FunctionalProgramming #Developers #JavaDeveloper #Programming #Tech
To view or add a comment, sign in
-
🚨 Java fact about constructors 👇 Constructors don’t have a return type… But something still returns an object 🤯 Example: class User { User() { System.out.println("Constructor called"); } } User user = new User(); 👉 What’s actually happening? - "new" keyword creates the object - Allocates memory - Calls the constructor - Returns the reference 👉 Important: Constructor itself does NOT return anything 👉 "new" keyword returns the object --- 👉 This is NOT a constructor: class User { void User() { } ❌ } 👉 It becomes a normal method 💡 Many people think constructor returns object… but actually new keyword does that Did you know this? 👇 #Java #CoreJava #Programming #BackendDeveloper #Coding #TechLearning
To view or add a comment, sign in
-
This Java snippet trips up even experienced developers 👇 Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; System.out.println(a == b); System.out.println(c == d); One prints true. One prints false. At first glance, both comparisons look identical. Same type. Same assignment. Same operator. So what’s going on? If you know the reason, you’ve got a deeper grasp of how Java actually works under the hood. Drop your explanation below — no Googling 👇 #Java #Programming #SoftwareEngineering #DevCommunity #CodeChallenge
To view or add a comment, sign in
-
🚨 Java Interview Trap You Should Know Here’s a small snippet that can trip up even experienced developers: public class TestFinally { public static void main(String[] args) { System.out.println(check()); } static int check() { try { return 10; } finally { return 20; } } } 🤔 What do you think the output will be? #Java #InterviewPrep #Coding #SoftwareEngineering #Developers
To view or add a comment, sign in
-
🚀 One Java Performance Mistake You Might Be Making Looks simple… but can hurt performance 👇 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } 👉 What’s the issue? - "list.size()" is called every iteration - In some implementations, it can be costly ✅ Better approach: for (int i = 0, size = list.size(); i < size; i++) { System.out.println(list.get(i)); } OR even better 👇 for (String item : list) { System.out.println(item); } ✔ Cleaner ✔ More readable ✔ Avoids unnecessary calls 🔥 Real impact: Small optimizations matter in large-scale systems. 💡 Lesson: Write efficient loops — they run more than you think. Do you prefer traditional loops or enhanced for-loops? 👇 #Java #Performance #CodingTips #CleanCode #Developers #Programming
To view or add a comment, sign in
-
Same method… different behaviors? 🤯 Welcome to Method Overloading in Java! Why write multiple methods when one name can handle it all? ✅ Cleaner code ✅ Better readability ✅ Smarter programming Start thinking like a developer, not just a coder 💻 #Java #Programming #OOP #CodingLife #Developers #LearnToCode
To view or add a comment, sign in
-
🚀 100 Days of Java Tips — Day 17 Tip: Avoid returning "null" from methods ❌ This is one of the most common causes of bugs in Java. Example: Bad: return null; Better: return Collections.emptyList(); ✅ Or: return Optional.empty(); ✅ Why it matters: • Reduces chances of NullPointerException • Makes your code safer • Improves readability for other developers Real problem: If someone forgets to check for null your code will crash in production ⚠️ Best practice: • Return empty collections instead of null • Use Optional for nullable values • Make your methods predictable Good code is not just working code it’s safe and reliable code Do you return null or avoid it? 👇 #Java #JavaTips #Programming #Developers #BackendDevelopment #CleanCode #SoftwareEngineering #BestPractices #Coding #Tech
To view or add a comment, sign in
-
-
Hello Everyone👋👋 What is the purpose of a default constructor? The purpose of the default constructor is to assign default values to the objects. If no constructor is defined in the class, the Java compiler creates a default constructor. #Java #backend #frontend #FullStack #software #developer #programming #code #class #object #lambda #Array #ArrayList #collections #super #constructor #SpringBoot #SpringAI #AI #GenAI #interface #abstract #Nodejs #React #Angular #Java26 #interview
To view or add a comment, sign in
-
Hello Everyone👋👋 What are the different types of access modifiers in Java? Public: Accessible from anywhere. Protected: Accessible within the same package or by subclasses in other packages. Default (package-private): Accessible only within the same package. Private: Accessible only within the same class. #Java #backend #frontend #FullStack #software #developer #programming #code #class #object #Array #ArrayList #collections #SpringBoot #SpringAI #OpenAI #GenAI #AI #Nodejs #React #Angular #multithreading #interface #abstract #super #constructor #interview
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