gpt4 book ai didi

java - ifne 和 ifnonnull 有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:40 24 4
gpt4 key购买 nike

我刚刚通读了 Java bytecode instruction listings并想知道:

Java字节码中的ifneifnonnull有什么区别?

我知道在高级语言(例如 Java)中,值 0 是一个具有某些运算(如加法、减法、乘法)并且可以与其他数值进行比较的数值,而null 是代表“无值”的值。

我想我遇到的一个问题是我不知道如何在字节码/程序集中表示 null

最佳答案

JVM 规范在这方面可能更清楚。

因此 ifne (and ifeq etc)限制是:

An ifeq instruction is type safe iff one can validly pop a type matching int off the incoming operand stack yielding the outgoing type state NextStackFrame, and the operand of the instruction, Target, is a valid branch target assuming an incoming type state of NextStackFrame.

鉴于 ifnonnull限制是:

An ifnonnull instruction is type safe iff one can validly pop a type matching reference off the incoming operand stack yielding the outgoing type state NextStackFrame, and the operand of the instruction, Target, is a valid branch target assuming an incoming type state of NextStackFrame.

基本上 ifeqifne 用于整数值,而 ifnonnull 用于引用值。

要比较两个引用是否相等,您可以使用 if_acmpeq反而。 ifnonnull 实际上是只有 一个 操作数的特殊情况,因为它总是只与 null 进行比较。

关于null 引用如何在data 中表示,JVM 规范explicitly allows this to be implementation-specific :

A reference value may also be the special null reference, a reference to no object, which will be denoted here by null. The null reference initially has no run-time type, but may be cast to any type. The default value of a reference type is null.

The Java Virtual Machine specification does not mandate a concrete value encoding null.

关于java - ifne 和 ifnonnull 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22731293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com