blog
  • 吕系小的博客
  • Java
    • 读书笔记
      • Thinking in Java
        • Object
          • 面向对象
          • 对象的创建与生命周期
          • 对象与基本类型的存储位置
        • 控制流程
          • 实现类似goto的操作
        • 初始化与清理
          • 对象的创建过程
          • 垃圾回收器如何工作
          • JIT
        • 复用类
          • 继承与初始化
          • final关键字
        • 多态
          • 构造器和多态
          • 方法和调用绑定
      • Java核心技术卷1
        • 面向对象
        • Unicode和char类型
        • Object:所有类的超类
        • 理解方法调用
        • 代理
        • lambda
        • 接口
        • 异常
        • 并发
          • 线程
          • volatile
          • 阻塞队列
          • 线程安全的集合
          • Callable与Future
          • 同步
          • 执行器
      • Java核心技术卷2
        • 流库
          • 流
          • 并行流
          • 基本类型流
          • 约简操作
          • 群组、分区、下游收集器
          • 收集结果
          • Optional
        • IO
          • 输入、输出流
          • 组合流过滤器
          • 文本输入与输出
          • 字符编码方式
          • 读写二进制数据
          • 随机访问文件
          • 对象输入、输出流与序列化
          • Path
          • 目录
          • 内存映射文件
          • Files
        • 日期和时间
          • 时间线
          • 本地时间
          • 日期调整期
          • 时区时间
          • 格式化和解析
        • 脚本、编译与注解处理
          • 脚本
          • 编译器
          • 注解
          • 标准注解
          • 源码级注解处理
        • 安全
          • 类加载器
          • 安全管理器与访问权限
          • 数字签名
      • 深入理解Java虚拟机
        • Java内存区域
        • 垃圾收集机制
      • Head First Java
        • Chapter1.Breaking the Sur face
        • Chapter2.A Trip to Objectville.There will be objects.
        • Chapter3.Know Your Variables
        • Chapter4.How Objects Behave
        • Chapter7.Better Living in Objectville
        • Chapter9.constructors and garbage collection
        • Chapter10.statics
      • 深入分析JavaWeb技术内幕
        • 深入web请求过程
        • 深入分析JavaIO的工作机制
      • Spring IN ACTION
        • Spring之旅
          • 依赖注入
          • IOC 容器
        • 装配bean
          • 组件扫描与自动装配
          • 通过Java代码装配bean
        • 高级装配
          • profile
          • 条件化bean
          • 处理自动装配的歧义性
          • bean的作用域
          • Environment
          • 属性占位符
    • 前后端那些事
      • 跨域访问
      • Token验证机制
    • 学习笔记
  • 数据库
    • MySQL
      • 基础篇
        • SQL分类及帮助文档
        • MySQL架构
        • 存储引擎
        • MySQL支持的数据类型
        • 数据类型的选择
        • 常用函数
      • 索引篇
        • 索引
      • 事务篇
      • 实际问题
        • 成绩分组取最大值问题
  • 计算机操作系统
    • 计算机操作系统组成与分类
    • 进程
      • 进程
      • 进程管理
      • 多线程结构进程
      • KLT与ULK
      • 处理器调度
    • 存储管理
      • 存储管理的主要模式
      • 存储管理的功能
      • 虚拟存储器的概念
      • 存储器的硬件支撑
      • 页式存储管理的基本原理
      • 页式虚拟存储管理
      • 页面调度
      • 反置页表
      • 段式存储管理
      • 段式虚拟存储
      • 段页式存储管理
    • 设备管理
      • IO缓冲区
    • 文件管理
      • 文件的存储
      • 文件的逻辑结构
      • 文件的物理结构
Powered by GitBook
On this page
  • The Stack and the Heap
  • 栈和堆
  • Instance Variables
  • 实例变量
  • Local Variables
  • 本地变量
  • Methods are stacked
  • 方法会被堆在一起
  • What about local variables that are objects?
  • 关于引用型变量
  • The role of superclass constructors in an object's life.
  • 父类构造函数在对象生命中的角色
  • this() and super()
  • this() 和 super()
  • The difference between life and scope for local variables
  • 本地变量“life”与“scope”的差别
  • How does variable life affect object life?
  • 变量生命周期怎么影响对象生命周期
  1. Java
  2. 读书笔记
  3. Head First Java

Chapter9.constructors and garbage collection

The Stack and the Heap

In java,we care about two areas of memory--the one where objects live(the heap),and the one where method invocations and local variables live(the stack).When a JVM starts up,it gets a chunk of memory from the underlying OS,and uses it to run your Java program.

栈和堆

在Java里,我们关系两个内存区域--一个是对象生存的地方(堆),一个是方法调用与本地变量生存的地方(栈)。当虚拟机启动,它会从OS中获得一块内存,然后用来运行你的Java程序。

Instance Variables

Instance avriables are declared inside a class but not inside a method.They represent the "fields" that each individual object has(which can be filled with different values for each instance of the class).Instance variables live inside the object they belong to.

实例变量

实例变量被声明在类里面但不在方法里。它们代表每个独立对象的“字段”(每个对象的实例变量能有不同的值)。实例变量生活在所属的对象里。

Local Variables

Local variables are declared inside a method,including method parameters.They're temporary,and live only as long as the method is on the stack(in other words,as long as the method has not reached the closing curly brace).

本地变量

本地变量被声明在方法里,包括方法参数。它们是暂时的,只生活在方法被放在栈上的时间(也就是方法调用至执行完毕之前)。

Methods are stacked

When you call a method,the method lands on the top of a call stack.That new thing that's actually pushed onto the stack is the stack frame,and it holds the state of the method including which line of code is executiong,and the values of all local variables.

方法会被堆在一起

当你调用一个方法时,该方法会被放在栈的顶端。实际被推上栈的是堆栈块,它带有方法的状态,包括执行到哪一行代码以及所有本地变量的值。

What about local variables that are objects?

a non-primitive variable holds a reference to an object,not the object itself.It doesn't matter where they're declared or created.If the local variable is a reference to an object,only the variable goes ont the stack.The object itself still goes in the heap.

关于引用型变量

一个非基本类型的变量只是保存对象的引用,不是对象本身。不论对象被声明或者创造,如果本地变量是对象的引用,只有变量会被放在栈上。对象只会存在于堆上。

The role of superclass constructors in an object's life.

All the constructors in an object's inheritance tree must run when you make a new object.

That means every superclass has a constructor(because every class has a constructor),and each constructor up the hierarchy runs at the time an object of a subclass is created.

A subclass might inherit methods that depend on superclass state(in other words,the value of instance variables in the superclass).For an object to be fully-formed,and that's why the super constructor must run.All instance variables from every class in the inheritance tree have to be declared and initialized.The superclass constructor must finish before its subclass constructor.

When a constructor runs,it immediately calls its superclass constructor,all the way up the chain until you get to the class Object constructor.Even abstract classes have constructors.

父类构造函数在对象生命中的角色

当你创建一个对象时,所有对象继承树中的构造函数都会执行

这意味着每个父类都有构造函数(因为每个类都至少有一个构造函数),且每个构造函数都会在子类对象创建时执行。

子类必须根据父类的状态(也就是父类的实例变量)来继承方法。因为对象完整成型需要父类的实例变量,这就是父类构造函数必须执行的原因。继承树上的所有实例变量必须被声明和初始化。父类构造函数必须在子类构造函数之前执行完成。

当构造函数执行,便立即调用父类的构造函数,这回连锁反应到Object为止。即使是抽象类也有构造函数。

this() and super()

  • Use this() to call a constructor from another overloaded constructor in the same class.

  • The call to this() can be used only in a constructor,and must be the first statement in a constructor.

  • A constructor can have a call to super() OR this(),but never both!

this() 和 super()

  • 使用this()调用同一个类另一个被重载的构造函数。

  • 调用this()必须在构造函数中,必须是构造函数中第一行语句。

  • super()与this()不能出现在同一个构造函数中。

The difference between life and scope for local variables

Life

A local variable is alive as long as its Stack frame is on the Stack.In other words,until the method completes.

Scope

A local variable is in scope only within the method in which the variable was declared.When its own method calls another,the variable is alive,but not in scope until its method resumes.You can use a variable only when it is scope.

本地变量“life”与“scope”的差别

Life

只要本地变量的堆栈块还在栈上,它就算活着。也就是说,活到方法执行完毕为止。

Scope

本地变量的范围只限于该变量被声明的方法内。当此方法调用其它方法,本地变量活着,但是不在范围内,直到此方法继续执行。你只能在本地变量的范围内使用它。

How does variable life affect object life?

An objecy is alive as long as there are live references to it.If a reference variable goes out of scope but is still alive,the object it refers to is still alive on the Heap.And then you have to ask..."What happens when the Stack frame holding the reference gets popped off the Stack at the end of the mehod?"

If that was the only live reference to the object,the object is now abandoned on the Heap.The trick is to know the point at which an object becomes eligible for garbage collection.

变量生命周期怎么影响对象生命周期

只要对象的引用活着,对象也就活着。如果引用变量超出范围但还活着,对象也会继续活在堆上。然后你会问:“当拥有引用变量的方法从栈中弹出会发生什么?”

如果这是该对象的唯一引用变量,那么对象现在会在堆中被抛弃。关键是要知道对象成为有资格被垃圾收集器回收。

PreviousChapter7.Better Living in ObjectvilleNextChapter10.statics

Last updated 6 years ago