- 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. - Or the static factory method is preferred, for example, use Boolean.valueOf("true") instead of
new Boolean("true"). - 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. - Avoid use
boxed primitive typerepeated when you can use primitive type directly.
Wednesday, July 17, 2019
Effective Java Study Note - Avoid creating unnecessary objects
Labels:
Effective Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment