Java:从 char 中减去 '0' 得到一个 int ......为什么这行得通?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4318263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Subtract '0' from char to get an int... why does this work?
提问by ledneb
This works fine:
这工作正常:
int foo = bar.charAt(1) - '0';
Yet this doesn't - because bar.charAt(x) returns a char:
然而这并没有 - 因为 bar.charAt(x) 返回一个字符:
int foo = bar.charAt(1);
It seems that subtracting '0' from the char is casting it to an integer.
似乎从字符中减去 '0' 是将其转换为整数。
Why, or how, does subtracting the string '0' (or is it a char?) convert another char in to an integer?
为什么或如何减去字符串 '0'(或者它是一个字符?)将另一个字符转换为一个整数?
采纳答案by Lukas Eder
That's a clever trick. char's are actually of the same type / length as shorts. Now when you have a char that represents a ASCII/unicode digit (like '1'), and you subtract the smallest possible ASCII/unicode digit from it (e.g. '0'), then you'll be left with the digit's corresponding value (hence, 1)
这是一个聪明的伎俩。字符实际上与短裤的类型/长度相同。现在,当您有一个表示 ASCII/unicode 数字的字符(如“1”),并从中减去可能的最小 ASCII/unicode 数字(例如“0”)时,您将得到该数字的相应值(因此,1)
Because char is the same as short (although, an unsigned short), you can safely cast it to an int. And the casting is always done automatically if arithmetics are involved
因为 char 与 short 相同(尽管是 unsigned short),所以您可以安全地将其转换为 int。如果涉及算术,则转换总是自动完成的
回答by Codemwnci
Because in Java if you do not specify a type indicator with a number then it assumes Integer. What I mean by that, if you want to set a Long value, it would need to be 0L.
因为在 Java 中,如果您不指定带有数字的类型指示符,则它假定为 Integer。我的意思是,如果你想设置一个 Long 值,它需要是 0L。
Performing a numerical operation on two different numerics, the result takes the larger. Hence, a char (which is a numerical value) minus an integer, results in an integer.
对两个不同的数值进行数值运算,结果取较大者。因此,一个字符(它是一个数值)减去一个整数,结果是一个整数。
You will find that this works also
你会发现这也有效
long val = bar.charAt(1) - 0L;
回答by khachik
char
s are converted to int
implicitly:
char
s 被int
隐式转换为:
public static void main(String [] args) throws Exception {
String bar = "abc";
int foo = bar.charAt(1) - '0';
int foob = bar.charAt(1);
System.err.println("foo = " + foo + " foob = " + foob);
}
output: foo = 50 foob = 98
.
输出:foo = 50 foob = 98
。
Maybe you put two int foo ...
and this is because it didn't work?
也许你放了两个int foo ...
,这是因为它不起作用?
回答by OscarRyz
'0'
is a char too. It turns out, the characters in Java have a unicode (UTF-16) value. When you use the -
operator with characters Java performs the operation with the integer values.
'0'
也是一个字符。事实证明,Java 中的字符具有 unicode (UTF-16) 值。当您使用-
带有字符的运算符时,Java 会使用整数值执行操作。
For instance, int x = '0' - 'A';// x = 16
例如, int x = '0' - 'A';// x = 16
回答by Dmitri
Your second snipped should work fine though:
不过,您的第二个剪辑应该可以正常工作:
int foo = bar.charAt(1);
A char
, just like a short
or byte
, will always be silently cast to int
if needed.
A char
,就像 ashort
或 一样byte
,int
如果需要,将始终默默地转换为。
This will compile just fine: int i = 'c';
这将编译得很好: int i = 'c';
回答by ColinD
The following code works perfectly fine!
下面的代码工作得很好!
int foo = bar.charAt(1);
Similar to reference types, any Java primitive can be assigned without casting to another primitive of a type it is considered a subtype of. The subtyping rules for primitives are given by JLS section 4.10.1. char
is considered a subtype of int
, so any char
may be assigned to an int
.
与引用类型类似,任何 Java 原语都可以在不强制转换为它被视为其子类型的类型的另一个原语的情况下进行分配。JLS 第 4.10.1 节给出了原语的子类型规则。char
被视为 的子类型int
,因此 anychar
可以分配给int
。
回答by DwB
This is an old ASCII trick which will work for any encoding that lines the digits '0' through '9' sequentially starting at '0'. In Ascii '0' is a character with value 0x30 and '9' is 0x39. Basically, i f you have a character that is a digit, subtracting '0' "converts" it to it's digit value.
这是一个古老的 ASCII 技巧,适用于任何从“0”开始按顺序排列数字“0”到“9”的编码。在 Ascii 中,'0' 是一个值为 0x30 的字符,'9' 是 0x39。基本上,如果您有一个数字字符,则减去 '0' 会将其“转换”为它的数字值。
I have to disagree with @Lukas Eder and suggest that it is a terrible trick; because the intent of this action aproaches to 0% obvious from code. If you are using Java and have String
that contains digits and you want to convert that String
to an int
I suggest that you use Integer.parseInt(yourString);
.
我不得不不同意@Lukas Eder 并认为这是一个可怕的技巧;因为此操作的意图从代码中接近 0%。如果您使用的是 Java 并且String
包含数字,并且您想将其转换String
为一个,int
我建议您使用Integer.parseInt(yourString);
.
This technique has the benifit of being obvious to the future maintenance programmer.
这种技术的好处是对未来的维护程序员显而易见。
回答by aksarben
Your code may compile without error & run without throwing an exception, but converting between char's & int's is bad practice. First, it makes the code confusing, leading to maintenance headaches down the road. Second, clever "tricks" can prevent compilers from optimizing the byte code. One of the best ways to get fastcode is to write dumbcode (i.e., not clevercode).
您的代码可能会编译而不会出错并运行而不会引发异常,但是在 char 和 int 之间进行转换是不好的做法。首先,它使代码混乱,导致后续的维护问题。其次,巧妙的“技巧”可以防止编译器优化字节码。获得快速代码的最好方法之一是编写愚蠢的代码(即,不是聪明的代码)。