- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想这是一道基础题,但为什么没有sload指令呢?为什么可以加载除 short 之外的所有图元? (有saload,但还是...)
对于:
public class ShortTest {
public void test() {
short i = 1;
System.out.print(i);
}
}
编译器仍然使用 iload_1。是因为 short 是 16 位类型而处理器处理更好的 32 位(因为所有现代处理器都是 32/64 位)吗?
最佳答案
引用JVM规范,§2.11.1. Types and the Java Virtual Machine :
Note that most instructions in Table 2.11.1-A do not have forms for the integral types
byte
,char
, andshort
. None have forms for theboolean
type. A compiler encodes loads of literal values of typesbyte
andshort
using Java Virtual Machine instructions that sign-extend those values to values of typeint
at compile-time or run-time. Loads of literal values of typesboolean
andchar
are encoded using instructions that zero-extend the literal to a value of typeint
at compile-time or run-time. Likewise, loads from arrays of values of typeboolean
,byte
,short
, andchar
are encoded using Java Virtual Machine instructions that sign-extend or zero-extend the values to values of typeint
. Thus, most operations on values of actual typesboolean
,byte
,char
, andshort
are correctly performed by instructions operating on values of computational typeint
.
值得回顾的是,在 Java 中,任何不涉及 long
的整数运算都会得到一个 int
结果,无论输入是否为 byte
、char
、short
或 int
。
这样一行
short i = 1, j = 2, k = i + j;
不会编译,但需要类型转换,比如
short i = 1, j = 2, k = (short)(i + j);
并且此类型转换将是涉及 short
的唯一指示符。撇开调试提示不谈,字节码中没有局部变量的正式声明,只有确定其类型的值赋值。所以 short
类型的局部变量根本不存在。上面的代码编译为
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: iload_1
5: iload_2
6: iadd
7: i2s
8: istore_3
与编译后的形式相同
int i = 1, j = 2, k = (short)(i + j);
但请注意,变量的编译时类型可以更改编译器在重载情况下为调用选择的方法。如果类型带有不同的语义,这一点尤其重要,例如 print(boolean)
或 print(char)
。虽然在这两种情况下传递给方法的值都是 int
类型,但结果完全不同。
编译器强制执行的差异的另一个例子是
{
int i = 1;
i++;
}
{
short s = 1;
s++;
}
编译成
0: iconst_1
1: istore_1
2: iinc 1, 1
5: iconst_1
6: istore_1
7: iload_1
8: iconst_1
9: iadd
10: i2s
11: istore_1
因此,由于计算始终以 32 位执行,因此编译器插入必要的代码以将结果截断为 short
以进行第二次增量。再次注意没有变量声明,因此代码与编译后的形式相同
int i = 1;
i++;
i = 1;
i = (short)(i+1);
也值得一看the Verification Type System ,因为验证者将检查所有从局部变量到局部变量的传输的有效性:
The type checker enforces a type system based upon a hierarchy of verification types, illustrated below.
Verification type hierarchy:
top
____________/\____________
/ \
/ \
oneWord twoWord
/ | \ / \
/ | \ / \
int float reference long double
/ \
/ \_____________
/ \
/ \
uninitialized +------------------+
/ \ | Java reference |
/ \ | type hierarchy |
uninitializedThis uninitialized(Offset) +------------------+
|
|
null
因此,与 Java 语言类型相比,类型系统得到了简化,并且验证者不介意,例如如果您将 boolean
值传递给需要 char
的方法,因为两者都是 int
类型。
关于java - JVM指令-sload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673666/
是否可以复制一个完整的 JVM,并且在故障转移的情况下只需将负载转移到复制的 JVM 上? 如果是,那我们该怎么做呢? 最佳答案 如果您的应用程序是 Web 应用程序,请阅读“集群”和“负载平衡”。大
我读了下面的话,但我想知道它们之间的区别...... JVM 规范、JVM 实现、JVM 运行时 最佳答案 JVM 规范:描述 JVM 应如何运行的文档。 JVM 实现:基于 JVM 规范的 JVM
我目前有四个不同的 java 应用程序,它们由 .bat 文件启动的 jar 运行,这些文件位于 Windows XP Embedded 开始菜单的 starup 文件夹中。我还启动了 Firefox
有人能给我一些关于强制 64 位 jvm 作为 32 位 jvm 运行的想法吗? 我需要为蓝牙连接编写一个 jse 桌面应用程序。为此,我需要实现 Bluecove jar 。它只有 32 位文件。所
我看到过关于这个问题的多条评论——有人说是,有人说不是,许多答案模棱两可。任何人都可以用更简单的术语描述它所在的位置吗?在一篇文章中,我什至看到有人说它与类加载器加载类的类内存共享相同的内存位置 -
我正在寻找所有可能的 jvm 退出代码的完整列表(不是 java System.exit(x))。我使用搜索引擎唯一能找到的是 SIGTERM 退出代码列表:http://journal.thobe.
为了监视任何正常的 Java 进程 JVM,我们可以使用 Attach API。是否有可用于监控 WebSphere JVM 的 API? 最佳答案 您可以使用 PMI(性能监控基础设施)来监控 JV
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: Java - C-Like Fork? 我想知道如何从 JDK fork 子 JVM,甚至有可能这样做吗? 一些框架
JVM 上的哪些图灵完备语言实现不使用 JVM 堆栈作为调用堆栈? (我问是因为我想在同一个线程中实现 Scala 和另一种语言之间的协程。) 最佳答案 闪蝶 SISC(方案代码的第二解释者) 曾经不
我看到here除了 Java 之外,还有很多语言可以在 JVM 上运行。我对在 JVM 中运行的其他语言的整个概念有些困惑。所以: 为 JVM 使用其他语言有什么优势? 为 JVM 编写语言/编译器需
我已经运行了 straced JVM (OpendJDK 11): strace -e trace=mmap java -Xms8192m Main 输出是: mmap(NULL, 8192, PRO
我已经运行了 straced JVM (OpendJDK 11): strace -e trace=mmap java -Xms8192m Main 输出是: mmap(NULL, 8192, PRO
我编写了一个简单的数独求解器。为了粗略测试性能,我使用简单的 System.currentTimeMillis 调用。 我在文本文件中准备了一组初始数独配置。该程序读取该文件并解决每个数独配置。运行测
JVM 被广泛使用:Scala、Groovy、Jython 等。我听说它被描述为“卓越”、“出色”和“严重低估”。为什么? 更具体地说,是什么让 JVM 独一无二?随着所有资金投入 .NET,或者 C
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicate: Are there any Java VMs which can save their state to a fi
想象一下 6-7 台服务器的设置都完全相同Java 版本“1.6.0_18”OpenJDK 运行时环境 (IcedTea6 1.8) (fedora-36.b18.fc11-i386)OpenJDK
(如有错误请指正) 我了解到,当您通过发出 java 命令来运行 java 程序时, java MyProg 程序将在新的 JVM 上运行。 什么将程序加载到新的 JVM 中?是生成新线程的 JRE
我们有一个使用 JNI 的桌面应用程序偶尔会导致 JVM 崩溃。幸运的是,JVM 会生成一个 hs_err_pidXXXX.log 文件,这对于调试此类错误非常有用。然而,它似乎总是转到当前工作目录,
我在命令提示符下运行一个程序集 jar 文件并得到下面的异常。并导致终止。 Uncaught error from thread [ccp-akka.persistence.dispatchers.d
一、什么是Java虚拟机 虚拟机:指以软件的方式模拟具有完整硬件系统功能、运行在一个完全隔离环境中的完整计算机系统 ,是物理机的软件实现。常用的虚拟机有VMWare,Visual Box,Java
我是一名优秀的程序员,十分优秀!