是否有“最终”的运行时成本?

时间:2017-08-30 11:37:40

标签: scala

我正在阅读Akka教程http://doc.akka.io/docs/akka/2.5.4/scala/guide/tutorial_3.html

有一行代码final case object ReadTemperature

final修饰符与此处已在SO中讨论的object一起使用What's the point of declaring an object as "final"?,但我想知道final是否也会带来一些运行时成本。

据我所知final仅在编译覆盖检查时有用,但可能在转换为VM代码时存在一些细微差别。很高兴听到意见。

1 个答案:

答案 0 :(得分:3)

没有。两种情况都编译为相同的输出:

$ echo "case object ReadTemperature" > demo1.scala
$ scalac demo1.scala

$ javap -c ReadTemperature.class > demo1.out
$ javap -c ReadTemperature$.class > demo1$.out

$ echo "final case object ReadTemperature" > demo2.scala
$ scalac demo2.scala

$ javap -c ReadTemperature.class > demo2.out
$ javap -c ReadTemperature$.class > demo2$.out

$ diff demo1.out demo2.out
1c1
< Compiled from "demo1.scala"
---
> Compiled from "demo2.scala"

$ diff demo1$.out demo2$.out
1c1
< Compiled from "demo1.scala"
---
> Compiled from "demo2.scala"