Java 加减字符为什么会这样?(爪哇)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21208308/
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
adding and subtraction chars why does this work? (java)
提问by thrax
I'm learning Java through "introduction to Java programming 9th edition" by Daniel liang at chapter 9 "strings" I've encountered this piece of code :
我正在通过 Daniel liang 在第 9 章“字符串”的“Java 编程简介第 9 版”学习 Java,我遇到了这段代码:
public static int hexCharToDecimal(char ch){
if (ch >= 'A' && ch <= 'F')
return 10 + ch - 'A';
else
return ch - '0';
}
oki so can someone explain what just happened in here? how is possible to add,sub chars from integers and what's the meaning behind it and thanks.
oki 所以有人可以解释一下这里发生了什么吗?如何从整数中添加子字符以及它背后的含义是什么,谢谢。
采纳答案by Michael Yaworski
From the Docs
来自文档
The char data type is a single 16-bit Unicode character.
char 数据类型是单个 16 位 Unicode 字符。
A char
is represented by its code point value:
Achar
由其代码点值表示:
- min
'\u0000'
(or 0) - max:
'\uffff'
(or 65,535)
- 最小
'\u0000'
(或 0) - 最大值:(
'\uffff'
或 65,535)
You can see all of the English alphabetic code points on an ASCII table.
您可以在ASCII 表上看到所有英文字母代码点。
Note that 0 == \u0000
and 65,535 == \uffff
, as well as everything in between. They are corresponding values.
请注意0 == \u0000
和65,535 == \uffff
,以及介于两者之间的所有内容。它们是对应的值。
A char
is actually just stored as a number (its code point value). We have syntax to represent characters like char c = 'A';
, but it's equivalent to char c = 65;
and 'A' == 65
is true.
Achar
实际上只是存储为一个数字(它的代码点值)。我们有语法来表示像 那样的字符char c = 'A';
,但它等价于char c = 65;
并且'A' == 65
为真。
So in your code, the chars are being represented by their decimal values to do arithmetic (whole numbers from 0 to 65,535).
因此,在您的代码中,字符由它们的十进制值表示以进行算术运算(从 0 到 65,535 的整数)。
For example, the char 'A'
is represented by its code point 65
(decimal value in ASCII table):
例如,char'A'
由其代码点65
(ASCII 表中的十进制值)表示:
System.out.print('A'); // prints A
System.out.print((int)('A')); // prints 65 because you casted it to an int
As a note, a short
is a 16-bit signedinteger, so even though a char
is also 16-bits, the maximum integer value of a char
(65,535) exceeds the maximum integer value of a short
(32,767). Therefore, a cast to (short)
from a char
cannot always work. And the minimum integer value of a char
is 0, whereas the minimum integer value of a short
is -32,768.
注意,ashort
是一个 16 位有符号整数,所以即使 achar
也是 16 位,a 的最大整数值char
(65,535) 超过了 a short
(32,767)的最大整数值。因此,(short)
从 a的强制转换char
并不总是有效。并且a的最小整数值为char
0,而a的最小整数值为short
-32,768。
For your code, let's say that the char
was 'D'
. Note that 'D' == 68
since its code point is 68
.
对于您的代码,假设char
was 'D'
。请注意,'D' == 68
由于它的代码点是68
.
return 10 + ch - 'A';
This returns 10 + 68 - 65
, so it will return 13
.
这将返回10 + 68 - 65
,因此它将返回13
。
Now let's say the char was 'Q' == 81
.
现在让我们说 char 是'Q' == 81
.
if (ch >= 'A' && ch <= 'F')
This is false since 'Q' > 'F'
(81 > 70
), so it would go into the else
block and execute:
这是错误的,因为'Q' > 'F'
( 81 > 70
),所以它会进入else
块并执行:
return ch - '0';
This returns 81 - 48
so it will return 33
.
这将返回,81 - 48
因此它将返回33
。
Your function returns an int
type, but if it were to instead return a char
or have the int
casted to a char
afterward, then the value 33
returned would represent the '!'
character, since 33
is its code point value. Look up the character in ASCII table or Unicode table to verify that '!' == 33
(compare decimal values).
您的函数返回一个int
类型,但如果它改为返回 achar
或在之后将其int
强制转换为 a char
,则33
返回的值将代表该'!'
字符,因为33
它是其代码点值。在 ASCII 表或 Unicode 表中查找字符以验证'!' == 33
(比较十进制值)。
回答by praveen_mohan
Chars are in turn stored as integers(ASCII value) so that you can perform add and sub on integers which will return ASCII value of a char
字符依次存储为整数(ASCII 值),以便您可以对整数执行加法和减法,这将返回字符的 ASCII 值
回答by ejbs
This is because char is a primitive type which can be used as a numerical value. Every character in a string is encoded as a specific number (not entirely true in all cases, but good enough for a basic understanding of the matter) and Java allows you to use chars in such a way.
这是因为 char 是一种原始类型,可以用作数值。字符串中的每个字符都被编码为一个特定的数字(并非在所有情况下都完全正确,但足以对问题有基本的了解)并且 Java 允许您以这种方式使用字符。
It probably allows this mostly for historical reasons, this is how it worked in C and they probably motivated it with "performance" or something like that.
它可能主要出于历史原因允许这样做,这就是它在 C 中的工作方式,并且他们可能以“性能”或类似的东西来激励它。
If you think it's weird then don't worry, I think so too
如果你觉得这很奇怪那么别担心,我也这么认为
The other answer is incorrect actually. ASCII is a specific encoding (an encoding is some specification that says "1 = A, 2 = B, ... , 255 = Space") and that is not the one used in Java. A Java char is two bytes wide and is interpreted through the unicode character encoding.
另一个答案实际上是不正确的。ASCII 是一种特定的编码(一种编码是一些规范,上面写着“1 = A, 2 = B, ... , 255 = Space”),而不是 Java 中使用的编码。Java char 是两个字节宽,通过 unicode 字符编码进行解释。
回答by nhgrif
Regardless of how Java actually stores the char
datatype, what's certain is this, the character 'A'
subtracted from the character 'A'
would be represented as the null
character, \0
. In memory, this means every bit is 0
. The size in memory a char
takes up in memory may vary from language to language, but as far as I know, the null
character is the same in all the languages, every bit is equal to 0
.
不管 Java 实际如何存储char
数据类型,可以肯定的是,从字符中'A'
减去的字符'A'
将表示为null
字符\0
. 在内存中,这意味着每一位都是0
. 内存中 a 在内存中char
所占的大小可能因语言而异,但据我所知,null
所有语言中的字符都是相同的,每一位都等于0
.
As an int
value, a piece of memory with every bit equal to 0
represents the integer value of 0.
作为一个int
值,一块每一位都等于的内存0
代表0的整数值。
And as it turns out, when you do "character math", subtracting any alphabetical character from any other alphabetical character (of the same case) results in bits being flipped in such a way that, if you were to interpret them as an int
, would represent the distance between these characters. Additionally, subtracting the char '0'
from any other numeric char will result in int value of the char you subtracted from, for basically the same reason.
事实证明,当您进行“字符数学”时,从任何其他字母字符(相同情况下)中减去任何字母字符会导致位以这样的方式翻转,如果您将它们解释为int
,代表这些字符之间的距离。此外,'0'
从任何其他数字 char 中减去char 将导致您从中减去的 char 的 int 值,原因基本相同。
'A' - 'A' = '##代码##'
'a' - 'a' = '##代码##'
'0' - '0' = '##代码##'