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 参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 char 传递给带有 int 参数的方法


- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 获取数字的最后一位 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 转换 ldap 日期 2022-01-01