简体   繁体   English

Java将int重载为long

[英]Java overloading int to long

Referring to Java overloading - long and float , which mentions rules in JLS #15参考Java 重载 - long 和 float ,其中提到了 JLS #15 中的规则

The following rules define the direct supertype relation among the primitive types:以下规则定义了原始类型之间的直接超类型关系:

double >1 float双 >1 浮点数

float >1 long浮动 >1 长

long >1 int长 >1 整数

int >1 char整数 >1 个字符

int >1 short int >1 短

short >1 byte短 >1 字节

where "S >1 T" means "T is a direct subtype of S", as per JLS #4.10 immediately above this section.其中“S > 1 T”表示“T 是 S 的直接子类型”,根据本节上方的 JLS #4.10。

Why is the following code prints float?为什么下面的代码打印浮动?

int q = 2;
a(q);

void b(long a) {
        System.out.println("long");
}

void a(float a) {
        System.out.println("float");
}

There's only 1 method named a which accept float parameter,只有一种名为a方法接受浮点参数,

Java support relation ( direct supertype relation ) from float to int (using long in the middle): Java 支持从floatint关系(直接超类型关系)(在中间使用long ):

float > long - > int浮动 > 长 - > 整数

If you rename method b to a , you will execute method with long as you expected如果重命名方法ba ,你将与执行方法long如你预期

void a(long a) {
        System.out.println("long");
}

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

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