DEV Community

Kudzai Murimi
Kudzai Murimi

Posted on

20+ Java Gotchas: Things You Might Not Know (But Should!)

Hey Dev Community! πŸ‘‹

Whether you're just getting started with Java or you've been in the game for a while, there are always little things that can trip you , quirks, subtleties, and things that just don’t work the way you'd expect.

Let’s go through a friendly, developer-first list of Java features and differences that many developers overlook. This list is here to save you from hours of debugging and head-scratching. And please, share your own gotchas in the comments. Let's help each other out!

1. == vs .equals()

  • == compares object references.
  • .equals() compares values.
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b);      // false
System.out.println(a.equals(b)); // true
Enter fullscreen mode Exit fullscreen mode

πŸ“Ž Reference: Java Equals and HashCode

2. String (double quotes) vs char (single quotes)

  • Use double quotes (") for String.
  • Use single quotes (') for char.
char letter = 'A';
String word = "A"; // Not the same!
Enter fullscreen mode Exit fullscreen mode

πŸ” A common beginner error is to confuse the two.

3. ArrayList vs LinkedList

  • ArrayList is faster for access.
  • LinkedList is faster for insertion/removal in the middle.

πŸ“Ž ArrayList vs LinkedList Performance

4. static vs non-static

  • static = belongs to the class.
  • Non-static = belongs to the instance.
static int count;
int instanceVar;
Enter fullscreen mode Exit fullscreen mode

5. final, finally, finalize()

  • final: can't be changed.
  • finally: used in try blocks.
  • finalize(): method called before garbage collection (now deprecated).

πŸ“Ž Java Final Keyword

6. String vs StringBuilder vs StringBuffer

  • String: immutable.
  • StringBuilder: mutable (not thread-safe).
  • StringBuffer: mutable (thread-safe).

πŸ“Ž Difference Between String, StringBuilder, and StringBuffer

7. Checked vs Unchecked Exceptions

  • Checked: must be handled (e.g. IOException).
  • Unchecked: runtime errors (NullPointerException).
try {
    // risky code
} catch(IOException e) {
    // must catch!
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Ž Checked vs Unchecked Exceptions

8. Primitive vs Wrapper Types

  • int vs Integer, boolean vs Boolean

πŸ” Wrappers can be null, primitives can't.

Integer x = null;
int y = x; // NullPointerException!
Enter fullscreen mode Exit fullscreen mode

9. Constructors vs Methods

  • Constructor: same name as class, no return type.
  • Method: can have any name and return type.

10. this() vs super()

  • this() calls a constructor in the same class.
  • super() calls a constructor in the parent class.
public Child() {
    super(); // calls parent constructor
    this.setup();
}
Enter fullscreen mode Exit fullscreen mode

11. Inheritance vs Composition

  • Inheritance: is-a
  • Composition: has-a

πŸ” Prefer composition for flexibility.

12. interface vs abstract class

  • Interface: only method signatures (until Java 8 added default methods).
  • Abstract class: can have implementations, fields.

πŸ“Ž Abstract Class vs Interface

13. var keyword (Java 10+)

  • var enables type inference.
var name = "Kudzai"; // compiler infers String
Enter fullscreen mode Exit fullscreen mode

Still statically typed! var just helps reduce verbosity.

14. Enhanced switch (Java 14+)

String result = switch(day) {
    case MONDAY -> "Start";
    case FRIDAY -> "End";
    default -> "Midweek";
};
Enter fullscreen mode Exit fullscreen mode

πŸ“Ž Switch Expressions

15. Lambda Expressions (Java 8+)

List<String> list = Arrays.asList("a", "b");
list.forEach(item -> System.out.println(item));
Enter fullscreen mode Exit fullscreen mode

πŸ“Ž Lambdas in Java

16. Streams API

  • Functional-style processing of collections.
list.stream().filter(e -> e.length() > 3).collect(Collectors.toList());
Enter fullscreen mode Exit fullscreen mode

πŸ“Ž Java Streams

17. Method Overloading vs Overriding

  • Overloading: same method name, different params.
  • Overriding: same method signature in subclass.

18. Java Memory Model (Heap vs Stack)

  • Stack: stores method calls and local variables.
  • Heap: stores objects.

19. Optional (Java 8+)

  • Avoid null by using Optional.
Optional<String> name = Optional.of("Kudzai");
name.ifPresent(System.out::println);
Enter fullscreen mode Exit fullscreen mode

πŸ“Ž Java Optional

20. Access Modifiers

  • public, protected, private, (default/package-private)

Know their scope well β€” it's essential for clean API design.

21. Serialization and transient

  • transient fields are not serialized.
private transient String password;
Enter fullscreen mode Exit fullscreen mode

πŸ“Ž Java Serialization Guide

✨ Your Turn!

These are just a few of the Java quirks, differences, and design choices that are easy to miss.

πŸ‘‰ What tripped you up when you were learning Java?
πŸ‘‰ Did we miss your favorite gotcha?
πŸ‘‰ Have a tip or experience to share?

Now, it's a high time we need to let’s learn from each other. πŸ’¬

Thanks for reading, and happy coding!

Top comments (0)