gpt4 book ai didi

java - 方法应该返回 boolean,返回 int

转载 作者:搜寻专家 更新时间:2023-11-01 01:42:27 24 4
gpt4 key购买 nike

我必须创建一个类似于 JAR 中的方法。我没有代码,所以我懒得学习了。我用的是JD-GUI,它告诉我的是:

private static boolean checkMe(Date paramDate, String paramString)
throws REUException {
int i = 1;
int j, k;

// unrelated stuff

if (j > k)
i = 0;
return i;
}

你可以在方法签名中看到它应该返回一个boolean,但它实际上返回一个ìnt,这在Java中是不允许的
所以,我认为 JD-GUI 有问题。

我尝试使用 javap 进行反汇编,但我仍然没有得到任何线索:

使用:javap -c -s -verbose -private Class

我得到:

private static boolean checkMe(java.util.Date, java.lang.String)   throws reu.exceptions.REUException;
Signature: (Ljava/util/Date;Ljava/lang/String;)Z
Code:
Stack=4, Locals=7, Args_size=2
0: iconst_1
1: istore_2
2: getstatic #34; //Field iniciado:Z
5: ifne 44
8: ldc_w #35; //class reu/modulos/STDWDATES
11: dup
12: astore_3
13: monitorenter
14: getstatic #34; //Field iniciado:Z
17: ifne 32
20: new #35; //class reu/modulos/STDWDATES
23: dup
24: invokespecial #36; //Method "<init>":()V
27: pop
28: iconst_1
29: putstatic #34; //Field iniciado:Z
32: aload_3
33: monitorexit
34: goto 44
37: astore 4
39: aload_3
40: monitorexit
41: aload 4
43: athrow
44: aconst_null
45: getstatic #37; //Field AlmacenFechaCal:Ljava/util/HashMap;
48: aload_1
49: invokevirtual #38; //Method java/util/HashMap.get:(Ljava/lang/Object;)Ljava/lang/Object;
52: if_acmpne 67
55: new #39; //class reu/exceptions/REUException
58: dup
59: bipush 58
61: bipush 17
63: invokespecial #40; //Method reu/exceptions/REUException."<init>":(II)V
66: athrow
67: getstatic #37; //Field AlmacenFechaCal:Ljava/util/HashMap;
70: aload_1
71: invokevirtual #38; //Method java/util/HashMap.get:(Ljava/lang/Object;)Ljava/lang/Object;
74: checkcast #41; //class reu/modulos/AlmancenFechas
77: astore_3
78: aload_3
79: invokevirtual #42; //Method reu/modulos/AlmancenFechas.getFechaIni:()I
82: istore 4
84: invokestatic #43; //Method java/util/Calendar.getInstance:()Ljava/util/Calendar;
87: astore 5
89: aload 5
91: aload_0
92: invokevirtual #44; //Method java/util/Calendar.setTime:(Ljava/util/Date;)V
95: aload 5
97: iconst_1
98: invokevirtual #45; //Method java/util/Calendar.get:(I)I
101: istore 6
103: iload 4
105: iload 6
107: if_icmple 112
110: iconst_0
111: istore_2
112: iload_2
113: ireturn
Exception table:
from to target type
14 34 37 any
37 41 37 any
Exceptions:
throws reu.exceptions.REUException

我猜线索是 113 中的 ireturn 表达式。根据 oracle documentation for ireturn ,它返回一个整数。

在此Casting conversions to primitive types , 看起来像从 int 到 boolean 的转换,不像 C/C++,是不允许的。

这怎么可能?有隐式类型转换吗?

谢谢。

最佳答案

JVM 使用整数来表示 boolean 值。来自JVM Specification §2.3.4 :

2.3.4. The boolean Type

Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int data type.

你可以自己检查一下:

$ cat Test.java
class Test {
boolean m() {
return true; <------------
}
}
$ javac Test.java
$ javap -c Test
Compiled from "Test.java"
class Test {
[...]

boolean m();
Code:
0: iconst_1
1: ireturn <------------
}

关于java - 方法应该返回 boolean,返回 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28581733/

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