finalize

java继承

允我心安 提交于 2019-12-10 00:11:11
子类 1.通过extends关键字,可以直接拥有来自于父类的属性和行为 (但是能不能访问要受访问修饰符的限制) 分清楚,,拥有和能不能直接操作到是两个不同的概念 2 公式: 子类 extends 父类 父亲的属性和行为 子类字自动继承; 但是子类不能继承父类的构造方法; 原因1:构造方法的语法要求是方法名必须和类名保持一致. 如果子类继承了父亲的构造,那么子类就会有一个叫父类的名的构造方法. 这一点与构造语法的语法冲突. 原因2: 构造方法的作用是产生对象的.父类和子类是分开的. 3.虽然父类的构造方法没有被子类继承,但在构造子类对象的过程中, 会先在内存中产生父类对象部分,再叠加子类特有部分,从而构造一个完整的子类对象. 也就是说new一个子类对象时,会先调用父类构造,在调用子类构造 类似于"内存叠加的方式" 4.如果子类定义了和父类一模一样的属性,那么子类对象身上会有两个该属性,一个在父类一个在子类 (1) 不应该这么设计,因为设计父类的时候已经考虑了共有,设计子类应该考虑特有. (2) 用super.属性名 代表的是定义在父类部分的同名属性名 (3) 用this.属性名 代表的是定义在子类部分的同名属性名 5.如果子类定义了和父亲一模一样的方法呢? 这叫做方法的"重写' 它是指所有类都有的共同行为,所以我们要定义在父类中 但是,不同的子类又有不同的实现

when will finalize() be called on my class instance in this scenario?

a 夏天 提交于 2019-12-07 18:28:18
问题 I know that finalize() is called whenever a class instance is collected by the garbage collector. However, I am a little bit confused when passing an instance of a class to another thread via a queue. Let's say this is a skeleton of Thread1: for(i=0; i<1000; i++) { Packet pkt = new Packet(); // instance of class pkt.id = i; thread2.queue.put(pkt); } Then, thread 2 will remove the packet from the queue and perform lengthy operations. Does this second thread get a "copy" of the packet, or is it

Best practice for implementing in Ada (2005 or 2012) an equivalent of the java finalize block

ⅰ亾dé卋堺 提交于 2019-12-07 03:27:13
问题 Java has the finalize block which allows to execute some statements after a block is left (executed even if an exception is raised). Example: try { ... } catch (Exception e) { ... } finally { ... // any code here } Ada has the controlled objects which allows to implement a Finalize operation but there is no finalize block equivalent as in java. This is useful for logging, closing files, transactions and so on (without having to create a specific tagged type for each possible block). How would

How to use PhantomReference as finalize() Replacement

試著忘記壹切 提交于 2019-12-07 03:09:44
问题 Javadoc 8 for PhantomReference states: Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism. So I tried creating a thread that is calling the close() method of a Test Object that is eligible for garbage collection. The run() tries to get all Test Objects pre-mortem . Actually the retrieved Test Objects are all null . The expected behavior is, that the Test Objects are retrieved and the

when will finalize() be called on my class instance in this scenario?

孤街醉人 提交于 2019-12-06 11:27:40
I know that finalize() is called whenever a class instance is collected by the garbage collector. However, I am a little bit confused when passing an instance of a class to another thread via a queue. Let's say this is a skeleton of Thread1: for(i=0; i<1000; i++) { Packet pkt = new Packet(); // instance of class pkt.id = i; thread2.queue.put(pkt); } Then, thread 2 will remove the packet from the queue and perform lengthy operations. Does this second thread get a "copy" of the packet, or is it by some form of reference? The importance is that, if it is by copy, the finalize() on the instance

Java连载57-equals重写、finalize方法、hashCode方法​

我的梦境 提交于 2019-12-06 11:06:04
一、关于java语言中如何比较两个字符串是否一致 1.不能使用双等号来比较两个字符串是否相等,应该使用equals方法进行比较,如例子 package com.bjpowernode.java_learning; ​ public class D57_1_ { public static void main(String[] args){ String s1 = new String("ABC"); String s2 = new String("ABC"); System.out.println(s1 == s2);//false,这是因为这个两个对象作比较,比较的是对象的地址 System.out.println(s1.equals(s2));//equals方法才是比较字符串里面的值 } } 2.重写类的equals可以达到判断对象内部的是否一致的效果,而不是使用类原来的对比地址的作用 package com.bjpowernode.java_learning; ​ public class D56_2_equalsMethodAnalysis { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); boolean b1 = o1.equals

when finalize() is being executed? [duplicate]

喜欢而已 提交于 2019-12-06 09:41:25
This question already has answers here : When is the finalize() method called in Java? (17 answers) Closed 5 years ago . In an interview i was asked,suppose JVM runs gc when object of class A is not in used. class A{ //some code here protected void finalize(){ //code here } } does it guarantee the execution of finalize(). I said yes The next ques was if obj of Class A is being used, if now JVM runs GC does it execute finalize(). I said no, it'll not execute this finalize() as JVM does not collect A's object. However she did not comment anything but looked disappointed. Does i interpret it

如何确定垃圾

馋奶兔 提交于 2019-12-06 05:38:37
一、 判断对象“已死”   1. 引用计数法 :给对象中添加一个引用计数器,每当有一个地方引用他,计数器值+1;当引用失效,计数器 -1。计数器为0则表示,对象没有被使用。     缺陷:循环引用问题。   2. 可达性分析算法 :通过一系列的称为“GC Roots”的对象作为起始点,从这些节点开始向下搜索,搜索所走过的路径称为引用链(Reference Chain),当一个对象到GC Roots没有任何引用链相连(用图论的话来说,就是从GC Roots到这个对象不可达)时,则证明此对象是不可用的。     对象object 5、object 6、object 7虽然互相有关联,但是它们到GC Roots是不可达的,所以它们将会被判定为是可回收的对象。     在Java语言中,可作为GC Roots的对象包括下面几种: 虚拟机栈(栈帧中的本地变量表)中引用的对象。 方法区中类静态属性引用的对象。 方法区中常量引用的对象。 本地方法栈中JNI(即一般说的Native方法)引用的对象。 二、 引用 强引用 :指在程序代码之中普遍存在的,类似“Object obj=new Object()”这类的引用,只要强引用还存在,垃圾收集器永远不会回收掉被引用的对象。 软引用 :用来描述一些还有用但并非必需的对象。对于软引用关联着的对象,在系统将要发生内存溢出异常之前

根父类:Object 类

混江龙づ霸主 提交于 2019-12-05 15:33:37
一、Object类   Java中规定: 如果一个类没有显式声明它的父类(即没有写extends xx),那么默认这个类的父类就是java.lang.Object。   类 Object 是类层次结构的 根类 。每个类都使用 Object 作为超类。    如何理解根父类?     (1)所有对象(包括数组)都实现这个类的方法,即Object类中声明的方法,所有引用数据类型(包括数组)中都有。     (2)所有类的对象的实例化过程,都会调用 Object 的实例初始化方法。     (3)所有的对象都可以赋值给 Object 的变量,或者说 Object 类型的变量,形参,数组可以接受任意类型的对象。 二、常用方法   1、toString() 方法 public String toString()      用于返回对象的信息,建议所有的子类都重写。如果没有重写:返回的字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。     如果直接打印一个对象,或者用对象与字符串进行拼接,默认情况下自动调用这个对象的 toString() 方法。   2、getClass() 方法 public final Class getClass()      该方法返回此 Object 的运行时类。(分为编译时类型与运行时类型)   3