java - java类文件版本之间有什么区别

标签 java maven jvm

在maven的文档中是这样写的

https://maven.apache.org/plugins/maven-compiler-plugin/

"Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler."

我们使用的java版本是java 7。

如果我将目标设置更改为 1.7 会有什么好处?

代码会运行得更快吗?

会不会占用更少的内存?

最佳答案

Java 7 表示 JDK/JRE 1.7。然而,真正的名字是 Java 7(1.X 是 5 之前遗留下来的旧命名约定和/或指的是内部版本号)。

目标版本意味着您编译字节码以符合特定 Java 版本的规范(支持的功能)。所以,如果你想编译将在运行 java 1.6 JVM 的机器上运行的代码,你需要针对 Java 1.6 编译你的代码,你应该限制自己使用 java 1.6 可用的语法/选项。

否则您将获得 java 最小/最大版本。内存使用取决于运行代码的实际 JVM 以及它是服务器 JVM 还是标准 JVM。

一般来说,您应该以目标机器 JVM 支持的目标为目标,而不是更高。您的代码也不得使用比您的目标更高版本中引入的功能(例如 1.6 中的菱形运算符,因为它是1.7 中引入)。

更新:关于命名约定:

看看http://en.wikipedia.org/wiki/Java_version_history .

非常有趣的是,在 Java 5 从 1.5 到 5 之后,命名约定实际上发生了变化。

Codename Tiger. Originally numbered 1.5, which is still used as the internal version number. The number was changed to "better reflect the level of maturity, stability, scalability and security of the J2SE."

Versions - from wikipedia :

  • JDK Alpha 和 Beta(1995)
  • JDK 1.0(1996 年 1 月 23 日)
  • JDK 1.1(1997 年 2 月 19 日)
  • J2SE 1.2(1998 年 12 月 8 日)
  • J2SE 1.3(2000 年 5 月 8 日)
  • J2SE 1.4(2002 年 2 月 6 日)
  • J2SE 5.0(2004 年 9 月 30 日)
  • Java SE 6(2006 年 12 月 11 日)
  • Java SE 7(2011 年 7 月 28 日)
  • Java SE 8(2014 年 3 月 18 日)
  • Java SE 9
  • Java SE 10

关于java - java类文件版本之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23341261/

相关文章:

java - JVM 中的套接字是如何实现的?

java - 运行 Kafka 性能流量时出现错误 "This server is not the leader for that topic-partition"

java - Spring 安全 : mapping of exceptions thrown inside security filter

java - 为什么我的 java swing Action 无法在 src/main/resources 中找到我的图像?

java - "The following artifacts could not be resolved": Maven/Android/Eclipse

maven - 在包含所有依赖项的 uber Jar 之外的 Jar 文件中提交 spark 应用程序

jvm - 启动STS应用程序时缺少tools.jar

java - 使用 JFileChooser 选择文件夹

java - URLConnection 和 POST 方法 android

java - 设计运行时在两个实现类之间进行选择