058: Effective Java for Android Developers – Item #11: Override clone Judiciously

In this mini-Fragment, Donn talks about Item #11 of the Effective Java series – Override clone Judiciously. You’ll learn about the extralinguistic behavior of clone and the Cloneable interface. Clone and Cloneable are very special, and this episode helps explain the nuances.

Download directly

Show Notes

Contact

044 – Effective Java for Android Developers – Item #10

In this mini-Fragment, Donn talks about Item #10 of the Effective Java series – Always Override toString. You’ll learn why it’s important for your own sanity, future developers, and overall developer happiness.

This episode is sponsored by Hired.com.

Download directly

Show Notes

Sponsor

Contact

034: Effective Java for Android Developers – Item #9

In this mini Fragment, we introduce Joshua’s ninth Item. After the last somewhat mind boggling item, this is a much welcomed simple, practical yet important one: Always override hashCode when you override equals.

Donn goes into the importance of implementing hashCode and why it’s so important to override it for maintaining harmony with the equals method. Also 42 and the answer to life ? He then goes into some tips on implementing a good hashCode and a standard recipe for the same.

Go forth and override them hashCodes!

Download directly

This episode is brought to you by Rollbar. Go to rollbar.com/fragmented to get their Bootstrap plan for free for 90 days. Stay tuned for more items from our “Effective Java for Android developers” Fragment series.

Contact

031: Effective Java for Android Developers – Item #8

In this mini Fragment, we introduce Joshua’s eighth Item. This one is a doozy, probably one of the longest items in the group of the effective Java series, but most definitely quite important.

This episode is brought to you by Rollbar. Go to rollbar.com/fragmented to get their Bootstrap plan for free for 90 days.

Download directly

Stay tuned for more items from our “Effective Java for Android developers” Fragment series.

Show Notes

Obey the general contract when overriding equals

When to not override equals:

  • Each instance of the class is inherently unique.
  • You don’t care whether the class provides a “logical equality” test.
  • A superclass has already overridden equals, and the superclass behavior is appropriate for this class.

The equals method implement an equivalence relation which states it must be:

  • Reflexive
  • Symmetric
  • Transitive
  • Consistent
  • For any non-null reference x, x.equals(null) must return false.

A recipe for a high-quality equals method is as such:

  • Use the == operator to check for references to this object.
  • Use the instanceof operator to check if the argument has the correct type 
  • Cast to the correct type.
  • Check all field types and corresponding field types.
  • Finally, when done, ask yourself – is this method symmetric, transitive and consistent?

Caveats

  • Always override hashcode when you override equals
  • Don’t be too clever!
  • Don’t substitute another type for Object in the equals declaration.

Contact

025: Effective Java for Android developers : Item 7

In this mini Fragment, we introduce Joshua’s seventh Item and a momentous end to the first chapter: Avoid finalizers

Stay tuned for more items from our “Effective Java for Android developers” Fragment series.

Download

Show Notes

Avoid finalizers

  • If you don’t know what they are, ignorance is bliss. If you know what they are, avoid them!
  • Finalizers in Java != destructors in C++ (C++ counterparts to constructors).
  • In C++ destructors
    • you reclaim resources here (Java has GC)
    • you also reclaim non-memory resources (use the try-finally block in Java)
  • (unpredicatable amt of time between object becoming unreachable and finalizer being executed) Never do anything time critical in finalizer!
    • System.gc + System.runFinalization increase chances – no guarantee
    • System.runFinalizersOnExit + Runtime.runFinalizersOnExit are the ones that do – but they are fatally flawed
  • Java 7 has try with resources, which is also interesting and auto-closeables. [Android] devs can only dream of these.
  • If an uncaught exception is thrown in a finalizer, it is ignored, and the finalization abruptly terminates.
  • Severe performance penalty for using finalizers – (one e.g.) time to create and destroy simple object goes from 5.6ns -> 2400ns
  • Only valid use: as a safety net or to terminate noncritical native resources.
  • [Android] you’re probably better off using Android’s lifecycle methods.

Contact