`
withoutme_hw
  • 浏览: 9404 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

1.Creating and Destroying Objects2——Effective Java 2nd Ed学习笔记

 
阅读更多

Item 4: Enforce noninstantiability with a private constructor

有些类需要防止用户实例化一个类,比如说java.lang.Math 和java.util.Arrays,Attempting to enforce noninstantiability by making a class abstract does not work.A class can be made noninstantiable by including a private constructor:

Item 5: Avoid creating unnecessary objects

String s = new String("stringette"); // DON'T DO THIS!

String s = "stringette"; //DO THIS 每一次运行该代码,JVM都保证只有一个"stringette"实例

1.

2.

On my machine, the original version takes 32,000 ms for 10 million invocations, while the improved version takes 130 ms, which is about 250 times faster.

Changing the declaration of sum from Long to long reduces the runtime from 43 seconds to 6.8 seconds on my machine. The lesson is clear: prefer primitives to boxed primitives, and watch out for unintentional autoboxing.

Item 6: Eliminate obsolete object references

每次执行Stack的pop操作,Stack的大小只是在逻辑上减少了,而Object[]中对pop出来的元素的引用依然存在,所以GC不能将其回收,这样的引用被称作obsolete references,这会导致内存泄露。这样做能够避免上述情况:

但是,Joshua提醒:Nulling out object references should be the exception rather than the norm.Generally speaking, whenever a class manages its own memory, the programmer should be alert for memory leaks.Another common source of memory leaks is caches.A third common source of memory leaks is listeners and other callbacks.

Item 7: Avoid finalizers

1. Finalizers are unpredictable, often dangerous, and generally unnecessary.

2. Never depend on a finalizer to update critical persistent state

3. There is a severe performance penalty for using finalizers.

4. Explicit termination methods are typically used in combination with the try-finally construct to ensure termination.

分享到:
评论

相关推荐

    CSharp - Module 9_Creating and Destroying Objects

    CSharp - Module 9_Creating and Destroying Objects

    VB.Net Programming.pdf

    Creating and Destroying Objects.......................................................................16 Demonstration: Creating Classes..................................................................

    MSDN Traning - VB.NET (VBL).pdf

    Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...

    Programming with Microsoft Visual Basic.NET_Delivery Guide.pdf

    Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...

    Programming with MS VB.NET.pdf

    Contents Introduction Course Materials..................Creating and Destroying Objects...........................................................................16 Demonstration: Creating Classes.....

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

    2 Creating and Destroying Objects Item 1: Consider static factory methods instead of constructors Item 2: Consider a builder when faced with many constructor parameters Item 3: Enforce the singleton ...

    programing C# edition5

    1. C# 3.0 and .NET 3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Evolution of C# 3 The C# Language 4 The .NET Platform 6 2. Getting ...

    深入理解LINUX内核(影印版)(第3版)

    Section A.1. Prehistoric Age: the BIOS Section A.2. Ancient Age: the Boot Loader Section A.3. Middle Ages: the setup( ) Function Section A.4. Renaissance: the startup_32( ) Functions Section A.5. ...

    JAVA小实验雷电(打飞机)

    1 You will find more useful informations in the following lessons: “Java GUI”, “Java Graphics”, “Applet and Multimedia”. The lesson ‘multithreading ’ maybe is useful to some advanced ...

    TongWeb7应用开发手册.pdf

    TongWeb7应用开发手册.pdf TongWeb7应用开发手册.pdf TongWeb7应用开发手册.pdf

    KafkaOffsetMonitor监控工具2017年1月发布的版本

    Instead, closing and destroying the client and re-creating it. Sleeping on error before re-creating client and continuing to process Deal with thread-safety issues on shared memory between threads ...

    CD and DVD Forensics(syngress安全图书)

    CD and DVD Forensics will take the reader through all facets of handling, examining, and processing CD and DVD evidence for computer forensics. At a time where data forensics is becoming a major part ...

    django-failedfirsttestrunner:重新排序测试套件,以便在随后的测试运行中首先运行失败的测试

    django-failedfirsttestrunner ...Destroying test database for alias ' default ' ... Creating test database for alias ' default ' ... :woman_dancing::woman_dancing:.......................

    Java版电子宠物游戏代码

    Java版电子宠物游戏代码,package Petgame; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Petgame extends MIDlet { private static Petgame instance; private ...

    ns2.34的蚁群算法补丁

    The changelog below should be helpful to you in getting Antnet into ns2.34 without destroying your additions. Instructions copy the files provided into NS2.34 directory as is. Run: ./configure make...

    U盘量产工具

    - Option for post image verification for both creating from and writing from usb drives. Previously, writing to drives always was verified. Verification may double the imaging time. - Each image ...

    convmv-1.15.tar.gz

    You have to quote the command and #1 will be substituted by the old, #2 by the new filename. Using this option link targets will stay untouched. Example: convmv -f latin1 -t utf-8 -r --exec "echo #...

    20190201版PythonForDelphi含例程源码.zip

    PythonForDelphi 修改 最后修改日期2019-2-1这一版,需要注意的是python 3.7 要安装32位版 需要在Delphi中先安装上Python...Demo34 Dynamically creating, destroying and recreating PythonEngine. Uses PytonVersions

    TCL TK 语言8.5编程指导

    VIVADO经常使用TCL脚本对FPGA进行调试,通过JTAG转AXI对内部模块进行控制,但是TCL语言书籍比较少,这边是一个英文的TCL语言学习书籍. Preface 1 Chapter 1: The Tcl Shell 5 Introduction 5 The Tcl shell 6 Writing ...

    TMS Pack for FireMonkey2.3.0.1

    Improved : block refreshing columns when destroying the component in TTMSFMXSpinner Fixed : Small issue in HTML Rendering engine for anchor rendering in TTMSFMXHTMLEngine Fixed : Issue with ...

Global site tag (gtag.js) - Google Analytics