finalize

Should Java 9 Cleaner be preferred to finalization?

我们两清 提交于 2019-11-28 23:26:11
In Java, overriding the finalize method gets a bad rap, although I don't understand why. Classes like FileInputStream use it to ensure close gets called, in both Java 8 and Java 10. Nevertheless, Java 9 introduced java.lang.ref.Cleaner which uses the PhantomReference mechanism instead of GC finalization. At first, I thought it was just a way add finalization to third-party classes. However, the example given in its javadoc shows a use-case that can easily be rewritten with a finalizer. Should I be rewriting all of my finalize methods in terms of Cleaner? (I don't have many, of course. Just

How is an object marked as finalized in Java (so that the finalize method wouldn't be called the second time)?

最后都变了- 提交于 2019-11-28 20:56:35
The main question is in the topic but let me show my vision of finalization proccess in Java so that I can ask you a little more. Well the gc starts garbage collection by marking all live objects. When all reachable objects are marked as "live". All other objects are unreachable. The next step is to check every unreachable object and determine whether it can be sweeped right now or it should be finalized at first. The gc thinks the next way if the object's finalize method has a body then this object is finalizable and should be finalized; if the object's finalize method has an empty body

how to destroy an object in java?

风流意气都作罢 提交于 2019-11-28 16:59:20
I encountered this question in an interview with following options: How to destroy an object in java? a. System.gc(); b. Runtime.getRuntime.gc(); c. object.delete(); d. object.finalize(); e. Java performs gc by itself, no need to do it manually. The answer should be e? what if e was not there? then ? clearly c is not the answer. a and b will do gc for the whole application(question requires for one object). I think it is d because finalize() is called just prior to gc(but is it necessary that after finalize gc is invoked ?) or I am wrong ? e must be there to answer this question ? Answer E is

java基础——异常

若如初见. 提交于 2019-11-28 16:12:55
Exception(异常):是程序本身可以处理的异常。Exception 类有一个重要的子类 RuntimeException。RuntimeException 类及其子类表示“JVM 常用操作”引发的错误。例如,若试图使用空值对象引用、除数为零或数组越界,则分别引发运行时异常(NullPointerException、ArithmeticException)和 ArrayIndexOutOfBoundException。 Exception(异常)分两大类:运行时异常和非运行时异常(编译异常)。程序中应当尽可能去处理这些异常。 1.运行时异常:都是RuntimeException类及其子类异常,如NullPointerException(空指针异常)、IndexOutOfBoundsException(下标越界异常)等,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。这些异常一般是由程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生。运行时异常的特点是Java编译器不会检查它,也就是说,当程序中可能出现这类异常,即使没有用try-catch语句捕获它,也没有用throws子句声明抛出它,也会编译通过。 2.非运行时异常 (编译异常):是RuntimeException以外的异常,类型上都属于Exception类及其子类。从程序语法角度讲是必须进行处理的异常

java常识

六月ゝ 毕业季﹏ 提交于 2019-11-28 16:10:25
java异常 java中广义的异常是指Throwable接口,这个接口下有两个实现类:Error和Exception。 Error表示严重的错误,一旦产生,则不做处理或者重写代码; 如果是Exception,分为检查异常和运行异常。检查异常在出现时必须处理,或者抛出或者捕获;运行异常在出现时可以处理可以不处理。 1 2 3 多线程 多线程是指一个进程中多个逻辑任务的执行,会产生安全问题,为了解决这个问题,可以利用同步代码块或者lock()方法. 1 java中的String String类是用final修饰的,因此不可被继承,其中重写了equals和hashCode方法,因此调用equals方法时比较的是两个字符串的值;String提供了比较多的构造方法,需要注意不同的构造方法所产生的对象的个数也不一样。用+连接运算拼接字符串的时候,调用的是StringBuilder中的append方法 1 Java中的String, StringBuffer, StringBuilder 这三个类都是final修饰的,都不可以被继承。String这个类是线程不安全的,其中的+连接运算用的是StringBuilder中的append方法,每一次的+都会产生一个新的StringBuilder,concat方法是将字符串转化为字符数组之后再进行合并,然后转化为字符串

java对象的生命周期

别来无恙 提交于 2019-11-28 15:53:29
在java中,对象的生命周期包括以下几个阶段: 1. 创建阶段(Created) 2. 应用阶段(In Use) 3. 不可见阶段(Invisible) 4. 不可达阶段(Unreachable) 5. 收集阶段(Collected) 6. 终结阶段(Finalized) 7. 对象空间重分配阶段(De-allocated) 图1. JavaObject Life Cycle 1.创建阶段(Created) 在创建阶段系统通过下面的几个步骤来完成对象的创建过程 l 为对象分配存储空间 l 开始构造对象 l 从超类到子类对static成员进行初始化 l 超类成员变量按顺序初始化,递归调用超类的构造方法 l 子类成员变量按顺序初始化,子类构造方法调用 一旦对象被创建,并被分派给某些变量赋值,这个对象的状态就切换到了应用阶段 2.应用阶段(In Use) 对象至少被一个强引用持有着。 3.不可见阶段(Invisible) 当一个对象处于不可见阶段时,说明程序本身不再持有该对象的任何强引用,虽然该这些引用仍然是存在着的。 简单说就是程序的执行已经超出了该对象的作用域了。 举例如下图:本地变量count在25行时已经超出了其作用域,则在此时称之为count处于不可视阶段。当然这种情况编译器在编译的过程中会直接报错了。 图2. 不可见阶段示例 4.不可达阶段(Unreachable)

In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil?

三世轮回 提交于 2019-11-28 15:08:03
In Java, what purpose do the keywords final , finally and finalize fulfil? final final can be used to mark a variable "unchangeable" private final String name = "foo"; //the reference name can never change final can also make a method not "overrideable" public final String toString() { return "NULL"; } final can also make a class not "inheritable". i.e. the class can not be subclassed. public final class finalClass {...} public class classNotAllowed extends finalClass {...} // Not allowed finally finally is used in a try/catch statement to execute code "always" lock.lock(); try { //do stuff }

Java 方法使用

こ雲淡風輕ζ 提交于 2019-11-28 13:36:29
那么什么是方法呢? Java方法是语句的集合,它们在一起执行一个功能。 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建,在其他地方被引用 方法的优点 1. 使程序变得更简短而清晰。 2. 有利于程序维护。 3. 可以提高程序开发的效率。 4. 提高了代码的重用性。 方法的命名规则 1.方法的名字的第一个单词应以小写字母作为开头,后面的单词则用大写字母开头写,不使用连接符。例如: addPerson。 2.下划线可能出现在 JUnit 测试方法名称中用以分隔名称的逻辑组件。一个典型的模式是: test<MethodUnderTest>_<state>,例如 testPop_emptyStack。 方法的定义 一般情况下,定义一个方法包含以下语法: 修饰符 返回值类型 方法名(参数类型 参数名){ ... 方法体 ... return 返回值; } 方法包含一个方法头和一个方法体。下面是一个方法的所有部分: 修饰符: 修饰符,这是可选的,告诉编译器如何调用该方法。定义了该方法的访问类型。 返回值类型 : 方法可能会返回值。returnValueType 是方法返回值的数据类型。有些方法执行所需的操作,但没有返回值。在这种情况下,returnValueType 是关键字 void 。 方法名: 是方法的实际名称。方法名和参数表共同构成方法签名。 参数类型:

Dispose() for cleaning up managed resources?

試著忘記壹切 提交于 2019-11-28 05:01:52
In this answer I found, Cleanup the unmanaged resources in the Finalize method and the managed ones in the Dispose method, when the Dispose/Finalize pattern has been used in your code. And later I found this nice article about finalize and dispose and got a clear idea about them. The article has the following code( Page 3 ), to explain the concepts: class Test : IDisposable { private bool isDisposed = false; ~Test() { Dispose(false); } protected void Dispose(bool disposing) { if (disposing) { // Code to dispose the managed resources of the class } // Code to dispose the un-managed resources of

.NET Framework学习笔记(十一)

拥有回忆 提交于 2019-11-27 22:49:36
19 、自动内存管理(垃圾收集) 内存 管理 :新对象创建、生存期管理、内存资源回收 访问一个资源所需要的步骤: § 调用 IL 中的 newobj 指令,分配内存 § 初始化,设置资源初始状态(类型的实例构造器) § 通过访问类型成员来使用资源 § 销毁资源状态,执行清理工作( Finalize, Dispose, Close ) § 释放引用实例所占的内存(垃圾收集器),而值类型实例的内存随堆栈空间的消亡而自动消失。 垃圾收集算法: 1. 垃圾收集只有在 第 0 代对象充满时 才会出现。 2. 每组应用程序都有一组 根 ,一个根是一个存储位置,其中包含着一个指向引用类型的内存指针。同时,当 JIT 编译器编译一个方法的 IL 代码时,除产生本地 CPU 代码外, JIT 编译器还会创建一个内部表,该表的每个条目包含一个方法的本地 CPU 指令的字节偏移范围,及该范围中的一组包含跟的内存地址。 3. 当垃圾收集器开始执行时,它先假设应用程序中没有一个根引用着托管堆中的对象。然后,遍历所有的根,构造出 一个包含所有可达对象的图 。任何不在该图中的对象都将是应用程序不可访问的对象 ,也是可以被执行垃圾收集的对象。 为什么垃圾收集的功能如此强大,而 ANSI C++ 没有采用它? 这是因为 垃圾收集器必须能够识别出应用程序的根,并且还要找到所有的对象指针 。非托管 C++