简体   繁体   English

将 char 传递给带有 int 参数的方法

[英]Passing char into a method with an int parameter

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.以下代码的输出是123因为substring从 beginIndex 到 EndIndex - 1。但是,我很惊讶这里的char被理解为 3 (int),因为substring需要两个 int。 What is the concept behind this?这背后的概念是什么?

String x = "12345";
char a = 3;
x = x.substring(0, a);
System.out.println(x);

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.这一路回到C ,其中char本质上是一个窄整数类型,并在必要时隐式转换为int

In Java, this is technically known as a "widening primitive conversion", and is covered in section 5.1.2 of the JLS .在 Java 中,这在技术上称为“扩展原语转换”, 在 JLS 的第 5.1.2 节中进行了介绍

Others already explainted why it works but note that it is bad practice to use char variables for indices, since they have different associated semantics and thus it is confusing to use a char as an index.其他人已经解释了它为什么起作用,但请注意,将 char 变量用于索引是不好的做法,因为它们具有不同的关联语义,因此将 char 用作索引会令人困惑。

Use chars only to store character data and probably better: try to avoid char altogether, since they are not even wide enough to store every character (see Unicode and code point discussion).仅使用 char 来存储字符数据,可能更好:尽量避免使用 char,因为它们甚至不足以存储每个字符(请参阅 Unicode 和代码点讨论)。 Use int to store character code points instead.使用 int 来存储字符代码点。

This is the concept of implicit and explicit casting.这是隐式和显式转换的概念。 the char is of small data type then the integer so when pass character as a value then it is auto-convert the char value into int value. char 是小数据类型,然后是整数,因此当将字符作为值传递时,它会自动将 char 值转换为 int 值。

public static void age (int  num){
        System.out.println(num);
   }
           char character = '2';
            age(character); output is 50.

The character form '0' to '9' print values 48 to 57字符形式 '0' 到 '9' 打印值 48 到 57

so substring's function declaration looks like substring(int startIndex, int endIndex).所以 substring 的函数声明看起来像 substring(int startIndex, int endIndex)。 Now when you pass char it is automatically promoted to integer (endIndex) and hence treated as int.现在,当您传递 char 时,它会自动提升为整数 (endIndex),因此被视为 int。

Take a look at section 5.1.2 , where it discusses widening conversions.看看第5.1.2节,它讨论了扩大转换。

This is known as implicit casting.这称为隐式转换。 The same occurs if you assign an int value to a double.如果将 int 值分配给 double 也会发生同样的情况。 Quick example快速示例

    double d = 1;

1 is an int but it is implicitly cast to double (1.0). 1 是一个int但它被隐式转换为double (1.0)。

In char a = 3;char a = 3; , you could think of it as storing 0011 , the binary value of 3. The char '3' is not actually being stored: If you tried treating is a char, you would not get 3 . ,您可以将其视为存储0011 ,即 3 的二进制值。实际上并未存储字符“3”:如果您尝试处理的是字符,则不会得到3 But if you did但如果你这样做了

char a = '3';

Now you're storing the char 3, an ascii value of 51, and if you tried using it as in int, you would get 51.现在您要存储 char 3,ascii 值为 51,如果您尝试像在 int 中一样使用它,您将得到 51。

Technically, this is because char is a subtype of int .从技术上讲,这是因为charint的子类型。

To determine whether substring(int,int) is applicable to argument (int,char) , we first try 15.12.2.2 Phase 1: Identify Matching Arity Methods Applicable by Subtyping , we need to test whether char is a subtype of int .为了确定substring(int,int)是否适用于参数(int,char) ,我们首先尝试15.12.2.2 阶段 1:识别适用于 Subtyping 的匹配 Arity 方法,我们需要测试char是否是int的子类型。 Per 4.10.1 Subtyping among Primitive Types , it is. 根据 4.10.1 原始类型之间的子类型,它是。

Then, to assign the char argument to the int parameter, per 15.12.4.5 Create Frame, Synchronize, Transfer Control , we apply 5.3 Method Invocation Conversion , which converts char to int , per 5.1.2 Widening Primitive Conversion然后,为了将char参数分配给int参数,根据15.12.4.5 Create Frame, Synchronize, Transfer Control ,我们应用5.3 Method Invocation Conversion ,将char转换为int ,根据5.1.2 Widening Primitive Conversion

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

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