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.
分享到:
相关推荐
在IT领域,尤其是在编程语言的学习与应用中,C#(C Sharp)作为一款由微软开发的面向对象的、类型安全的、垃圾回收的程序设计语言,其在创建和销毁对象方面的处理方式是开发者必须掌握的核心技能之一。本文将基于...
Contents Introduction Course Materials..................Creating and Destroying Objects...........................................................................16 Demonstration: Creating Classes.....
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 ...
Creating and Destroying Objects.......................................................................16 Demonstration: Creating Classes..................................................................
Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...
Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...
Destroying..1 Destroying..0 该资源摘要信息提供了一个完整的C++语言程序设计期末考试试题,涵盖了面向对象编程、函数重载、递归调用、拷贝构造函数、继承、多态性等C++编程概念,为C++语言学习者和考试者提供了一...
- Destroying..1 - Destroying..0 程序首先创建了一个Samp类型的动态数组,然后逐个设置元素值并输出,最后释放内存。析构函数在每个对象被删除时执行,显示其内部存储的i的值。 10. Vector类是一个动态数组的...
7. **创建与销毁对象(Creating and Destroying Objects)**:"CSharp_Module 9_Creating and Destroying Objects.pdf"将涵盖对象的生命周期,包括构造函数、析构函数和垃圾回收机制,以及何时和如何适当地分配和释放...
3. "Instead, he thinks research efforts should concentrate ________ creating new tissues and organs that can be used to cure diseases like cancer." ——此处应填"on",意为研究应该集中在创造新的组织和...
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 ...
7. **Creating and Destroying Objects**:学习如何创建和销毁对象,理解对象生命周期和垃圾回收机制。 8. **Inheritance in C#**:子类继承父类的属性和方法,实现代码重用和扩展。 9. **Aggregation, Namespaces...
- **Trigger Volumes and Destroying Actors**: Detecting when actors should be removed. - **Debugging Our Blueprints**: Identifying and fixing issues. - **Masking Our Destruction with Particles**: ...
TongWeb是东方通科技推出的一款高性能的企业级Java应用服务器,它支持最新的Servlet规范和其他Web技术,如JSP和JSF,以帮助开发者构建复杂的Web应用程序。 ### Servlet Servlet是一种Java编程接口,用于扩展服务器...
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 ...
模块9:《C# - Module 9_Creating and Destroying Objects.pdf》 本模块讲解了对象的生命周期,包括构造函数和析构函数的使用,以及垃圾回收机制。学习者将了解如何在适当的时间创建和释放对象,以优化内存管理。 ...
django-failedfirsttestrunner ...Destroying test database for alias ' default ' ... Creating test database for alias ' default ' ... :woman_dancing::woman_dancing:.......................
Java小程序的生命周期是一个重要的概念,理解它对于编写和调试Java Applet至关重要。Applet的生命周期由四个主要状态组成:初始化(Initialization)、加载(Loading)、启动(Starting)、绘画(Painting)、停止...
Java版电子宠物游戏代码,package Petgame; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Petgame extends MIDlet { private static Petgame instance; private ...
1. "Weather permitting, they will go and visit the zoo." 这里"Weather permitting"就是一个独立主格结构,用来表示条件,意思是“如果天气允许的话”。其中"weather"是逻辑主语,"permitting"是现在分词,表示...