Wednesday, July 17, 2019

Effective Java Study Note - Avoid creating unnecessary objects


  1. Reuse is faster, an object can always be reused when it's immutable. For example, use String abc = "abc" instead of String abc = new String("abc"), the later would unnecessarily create an object.
  2. Or the static factory method is preferred, for example, use Boolean.valueOf("true") instead of new Boolean("true").
  3. Avoid repeated creating objects, for example, use Pattern.compile("xxx") instead of String.match("xxx") because String.match would internally create a new Pattern object.
  4. Avoid use boxed primitive type repeated when you can use primitive type directly.

No comments:

Post a Comment