`
leonzhx
  • 浏览: 793436 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 5: Avoid creating unnecessary objects

阅读更多

1.    An object can always be reused if it is immutable

 

2.    The statement: 

String s = new String("stringette");

 creates a new String instance each time it is executed, and none of those object creations is necessary.

 

3.    You can often avoid creating unnecessary objects by using static factory methods in preference to constructors on immutable classes. (Boolean.valueOf() over Boolean(boolean) )

 

4.    You can also reuse mutable objects if you know they won’t be modified.

 

5.    If you don’t change local objects after initialization in the method, you can declare them as final static instead of local variables and initialize them in the static initializer so that you can reuse them instead of create new instances of them in the method.


6.    An adapter is an object that delegates to a backing object, providing an alternative interface to the backing object. Because an adapter has no state beyond that of its backing object, there’s no need to create more than one instance of a given adapter to a given object. i.e. The keySet method of the Map interface returns a Set view of the Map object, consisting of all the keys in the map. Naively, it would seem that every call to keySet would have to create a new Set instance, but every call to keySet on a given Map object may return the same Set instance. Although the returned Set instance is typically mutable, all of the returned objects are functionally identical: when one of the returned objects changes, so do all the others because they’re all backed by the same Map instance. While it is harmless to create multiple instances of the keySet view object, it is also unnecessary.

 

7.    Prefer primitives to boxed primitives, and watch out for unintentional autoboxing.

 

8.    The creation and reclamation of small objects whose constructors do little explicit work is cheap, especially on modern JVM implementations. (Small and short lived objects are easy to collect in young generation.)

 

9.    Avoiding object creation by maintaining your own object pool is a bad idea unless the objects in the pool are extremely heavyweight (i.e. database connections).

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 6: Avoid creating unnecessary objects Item 7: Eliminate obsolete object references Item 8: Avoid finalizers and cleaners Item 9: Prefer try-with-resources to try-finally 3 Methods Common to All ...

    Effective C#

    **Item 16: Avoid Creating Unnecessary Objects** - **Impact:** Reducing object creation can improve performance by reducing memory pressure. - **Approach:** Reuse objects when possible and avoid ...

    effectice_java第二版 英文

    5. **条目5:重用对象以减少装箱开销(Avoid Creating Unnecessary Objects)** 对于装箱基本类型,重复创建对象会增加内存消耗和性能开销。尽可能重用对象,或者使用原始类型代替。 6. **条目6:消除过期的对象...

    Kastri For Delphi 通用Delphi FMX 扩展库

    Is structured in a way so as to avoid creating unnecessary dependencies Follows strict coding standards to ensure consistency and readability Is kept as simple as practicable Is named after the place ...

    Efficient MIDP Programming

    - **Reusing Graphics Objects**: Avoid creating new graphics objects repeatedly, which can be costly in terms of memory and processing time. - **Reducing Screen Refresh Rates**: Reducing the frequency ...

    Unnecessary Code Detector

    当我们需要清理代码并删除那些不需要的代码碎片时,Unnecessary Code Detector(https://marketplace.eclipse.org/content/unnecessary-code-detector/metrics) 就可以完成这项工作。当你点击按钮该工具就会出现标志...

    The Elements of Style

    - 示例:Avoid unnecessary repetition.(避免不必要的重复。) - 作用:精简语言。 14. **避免连续使用松散句子**: - 示例:Avoid using loose sentences one after another. - 作用:提高文章质量。 15. **...

    baidu_api-lbs_cloud

    # :sk is unnecessary if you are not authenticating with SN BaiduApi :: LbsCloud :: V3 :: Geodata . setup ak : 'xxxx' , sk : 'xxxxx' # Also :ak and :sk can be Proc if you want load them dynamically ...

    Android代码-FOSS Browser

    The app also does not need any unnecessary permissions. A simple Android browser based on webview. The base is "Ninja" (https://github.com/mthli/Ninja). The intention is to provide a simple and light...

    Yunus, Muhammad - Creating A World Without Poverty; Social Business and the Future of Capitalism

    Creating a World Without Poverty: Social Business and the Future of Capitalism By Muhammad Yunus Publisher: PublicAffairs Number Of Pages: 296 Publication Date: 2008-01-07 ISBN-10 / ASIN: ...

    spring framework4

    Testing: mock objects, TestContext framework, Spring MVC Test, WebTestClient. Data Access: transactions, DAO support, JDBC, ORM, Marshalling XML. Spring MVC and Spring WebFlux web frameworks ...

    Maximizing ASP.NET

    Creating Web services from existing code and using remote services just like local objects Using ASP.NET 2.0's rich security and membership classes Personalizing siteswithout unnecessary complexity ...

    EurekaLog_7.5.0.0_Enterprise

    9)....Added: Detection/logging Delphi objects in disassembly view 10)..Added: Support for multi-monitor info 11)..Added: Support for detection of Windows 10 updates 12)..Added: OS edition detection 13...

    JavaScript in 10 Minutes

    1. **Why `new` is Awful**: Discusses the drawbacks of using the `new` operator, such as the creation of unnecessary objects and the lack of flexibility. 2. **Why `new` isn't Quite so Awful**: Explains...

    is-unnecessary:好吧,是吗?

    标题 "is-unnecessary:好吧,是吗?" 指向的是一个名为 "is-unnecessary" 的 JavaScript 库。这个库很可能提供了一些功能,用于检查某个对象、变量或条件是否真的必要,或者帮助开发者识别和消除代码中的冗余部分。...

    Learning React A Hands-On Guide to Building Web Applications

    5 Creating Complex Components 6 Transferring Properties 7 Meet JSX…Again! 8 Dealing with State in React 9 Going from Data to UI in React 10 Events in React 11 The Component Lifecycle 12 Accessing DOM...

    cirris operation manual

    2. **Deleting Programs**: Remove outdated or unnecessary programs. #### Using CTLWIN CTLWIN is a software application that facilitates advanced management of test programs: 1. **Common Uses**: Use ...

Global site tag (gtag.js) - Google Analytics