Think in java笔记 final

Think in java笔记 final

Many programming languages have a way to tell the compiler that a piece of data is “constant.” A constant is useful for two reasons:
1. It can be a compile-time constant that won’t ever change.
2. It can be a value initialized at run time that you don’t want changed.

很多语言都有一定的方法告诉编译器,一块数据是常量,使用常量(final)的原因有两个:
1. 可以是一个编译的常量,这样子有些计算就可以直接在编译阶段完成。
2. 可以是一个不可以改变的常量。

在用于基本类型和对象类型是不同的效果:
1. 基本类型必须一般要在定义时赋值,否则会报编译错误。这里的作用主要是为了避免以后程序的异常。这里有一种异常情况:

Java allows the creation of blank finals, which are fields that are declared as final but are not
given an initialization value. In all cases, the blank final must be initialized before it is used,
and the compiler ensures this. However, blank finals provide much more flexibility in the use
of the final keyword since, for example, a final field inside a class can now be different for
each object, and yet it retains its immutable quality.
Java允许创建空白final(即未定义的final变量),空白final提供了在使用的时候很大的灵活性,但是这种情况下编译器会在使用前检查,通常的做法就是:空白final,编译器赋值。

  1. 对象类型只是强调引用不能变,就是存在栈中的引用,而不是引用的对象不能变化。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值