[英]Why Does Java Compiler Treat Long Data Type As Double Instead of Integer By Default?
I'm new to Java and I stumbled upon this while testing some code.我是 Java 的新手,我在测试一些代码时偶然发现了这一点。 Why does Java pass in x (of data type long) into the function that takes in double parameters instead of the one with integer parameters.为什么 Java 将 x(数据类型为 long)传入 function 接受双参数而不是 integer 参数。 I would appreciate it if somebody could kindly explain to me why (Even though it might be an easy question to most of you!) Thank you in advance!如果有人能向我解释原因,我将不胜感激(尽管这对你们大多数人来说可能是一个简单的问题!)提前谢谢你!
public class Hello {
public static void main (String [] args) {
long x=1;
System.out.println("Before calling the method, x is "+x);
increase(x);
System.out.println("After calling the method, x is "+x);
System.out.println();
double y=1;
System.out.println("Before calling the method, y is "+y);
increase(y);
System.out.println("After calling the method, y is "+y);
}
public static void increase(int p) {
p+=1;
System.out.println(" Inside the method is "+p);
}
public static void increase(double p) {
p+=2;
System.out.println(" Inside the method is "+p);
} }
The conversions allowed when calling a method are defined by JLS Chapter 5. Implicit primitive conversions must be widening , that is, not result in a loss of magnitude (though in the case of long to double, may result in a loss of precision).调用方法时允许的转换由 JLS 第 5 章定义。隐式原始转换必须是加宽的,也就是说,不会导致幅度损失(尽管在 long 到 double 的情况下,可能会导致精度损失)。
There are six kinds of conversion contexts in which poly expressions may be influenced by context or implicit conversions may occur.有六种转换上下文,其中多边形表达式可能受上下文影响或可能发生隐式转换。 Each kind of context has different rules for poly expression typing and allows conversions in some of the categories above but not others.每种上下文对多表达式类型都有不同的规则,并允许在上述某些类别中进行转换,但不允许在其他类别中进行转换。 The contexts are:上下文是:
... ...
Strict invocation contexts (§5.3, §15.9, §15.12), in which an argument is bound to a formal parameter of a constructor or method.严格的调用上下文(§5.3、§15.9、§15.12),其中参数绑定到构造函数或方法的形式参数。 Widening primitive, widening reference, and unchecked conversions may occur.可能会发生扩大原语、扩大参考和未经检查的转换。
Loose invocation contexts (§5.3, §15.9, §15.12), in which, like strict invocation contexts, an argument is bound to a formal parameter.松散的调用上下文(第 5.3 节、第 15.9 节、第 15.12 节),其中,与严格调用上下文一样,参数绑定到形式参数。 Method or constructor invocations may provide this context if no applicable declaration can be found using only strict invocation contexts.如果仅使用严格的调用上下文无法找到适用的声明,则方法或构造函数调用可以提供此上下文。 In addition to widening and unchecked conversions, this context allows boxing and unboxing conversions to occur.除了扩大和未经检查的转换外,此上下文还允许进行装箱和拆箱转换。
Casting from long to int is a narrowing primitive conversion, as it may result in a loss of magnitude information.从 long 转换为 int 是一种缩小的原始转换,因为它可能会导致幅度信息的丢失。 So it is not called unless you first explicitly cast to (int)
.因此,除非您首先明确转换为(int)
,否则不会调用它。
You have 2 overloaded methods for increase() with int and double as input parameters.您有 2 个用于 increase() 的重载方法,其中 int 和 double 作为输入参数。 Also you are passing input parameter as long type.您还将输入参数作为长类型传递。
In Java the UpCasting happens in the below format.在 Java 中,UpCasting 以以下格式发生。
byte -> short -> int -> long -> float -> double
So when you pass the long type value as input parameter, compiler first looks for exact match in the input parameter.因此,当您将 long 类型值作为输入参数传递时,编译器首先会在输入参数中查找完全匹配。 If it not found then it will upcast to next value.如果没有找到,那么它将向上转换到下一个值。
Hence long value can be accepted by the method having double value as input parameter.因此,长值可以被具有双值作为输入参数的方法接受。
Please go through the below url's.请通过以下网址 go。
Type Conversion In JavaJava中的类型转换
Type Conversion - Oracle Documentation类型转换 - Oracle 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.