gpt4 book ai didi

java - Java 中的转换和短参数问题

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:41 25 4
gpt4 key购买 nike

我试图理解以下代码:

public class A {
private void doTask() {
doInt(99);
}

public short doInt(short s) {
return 100;
}
}

该代码在“doInt(99)”行给出编译器错误。

The method doInt(short) in the type A is not applicable for the arguments (int)

谁能解释为什么会给我这个错误。

最佳答案

在java中,整数的默认类型是int。 Java 会自动向上转换(例如从 shortint),但会发出向下转换的警告(例如从 intshort) 因为可能丢失数据。

您的代码的修复是这样的:

doInt((short)99);

这个显式向下转换将 99(它是一个 int)更改为 short,因此不会(进一步)丢失数据.

关于java - Java 中的转换和短参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11064020/

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