将char传递给带有int参数的方法 [英] Passing char into a method with an int parameter
本文介绍了将char传递给带有int参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码的输出是 123
,因为 substring
从beginIndex到EndIndex - 1.但是,我我很惊讶 char
这里被理解为3(int),因为 substring
取两个整数。这背后的概念是什么?
The output from the following code is 123
because substring
takes from beginIndex to EndIndex - 1. However, I am surprised how char
here is understood as 3 (int) because substring
take two ints. What is the concept behind this?
String x = "12345";
char a = 3;
x = x.substring(0, a);
System.out.println(x);
推荐答案
这一直回到 C ,其中 char
实质上是一个窄整数类型,并在必要时隐式转换为 int
。
This goes all the way back to C, where char
is in essence a narrow integer type and gets implicitly converted to int
whenever necessary.
在Java中,这在技术上称为扩大原始转换,并在 JLS第5.1.2节。
In Java, this is technically known as a "widening primitive conversion", and is covered in section 5.1.2 of the JLS.
这篇关于将char传递给带有int参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文