gpt4 book ai didi

java - 字节类型转换

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

这是一个基本问题,但我找不到正确的答案。我认为这是因为原始类型自动类型转换

为什么下面的语句会调用 print(int x) 方法而不是 print(char x) 方法。

public class Overloading {

public static void main(String args[])
{
byte b='x';
print(b);
}

public static void print(int x)
{
System.out.println("Inside int Print "+x);
}


public static void print(char x)
{
System.out.println("Inside char Print "+x);
}

public static void print(float x)
{
System.out.println("Inside float Print "+x);
}


}

最佳答案

可用于此的转换 method invocation conversionwidening primitive conversion :

byte to short, int, long, float, or double

short to int, long, float, or double

char to int, long, float, or double

int to long, float, or double

long to float or double

float to double

您看到没有 bytechar 路径。这不是优先事项:如果您删除以 int 作为参数的函数,您的代码将无法编译。

关于java - 字节类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15808474/

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