简体   繁体   English

Null在反编译的java类文件中生成

[英]Null is casted in decompiled java class file

I decompiled one of my java .class files and saw this line of code 我反编译了一个java .class文件并看到了这行代码

new ResponseModel("Reset Complete", false, (LinkedHashMap)null)

The line corresponds to 该行对应于

new ResponseModel("Reset Complete", false, null);

Why was the null parameter casted? 为什么输入null参数? Is it just my decompiler hinting the parameter type? 它只是我的反编译器暗示参数类型?

Immagine you've overloaded a method: 想象你已经重载了一个方法:

public class Foo {

    public void something (String s) { ... }
    public void something (List l) { ... }
}

Invoking something with a null argument is now ambiguous . 现在使用null参数调用something不明确的 To bind the invocation to either method, you need to cast the null value , giving it a type: 要将调用绑定到任一方法,您需要转换null值 ,为其指定一个类型:

new Foo().something((String)null);
new Foo().something((List)null);

As this class may be different at runtime than on compile time (on compile time, the method may not be overloaded, but on runtime the class is a newer version which has an overloaded method), the compiler makes it explicit in the bytecode to prevent ambiguousness later. 由于此类在运行时可能与编译时不同 (在编译时,方法可能不会重载,但在运行时该类是一个具有重载方法的较新版本),编译器在字节码中使其显式化以防止后来模棱两可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM