'0' 在 Java 中有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34409950/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:52:07  来源:igfitidea点击:

What does '0' do in Java?

java

提问by Thev

I'm trying to find the product of all digits in a number, which I have stored as a string (due to int and long length limits).

我试图找到一个数字中所有数字的乘积,我将其存储为字符串(由于 int 和 long 长度限制)。

So the number looks like this:

所以数字看起来像这样:

final String number = "1234567898765432123......etc"

If I use this code, it works:

如果我使用此代码,它会起作用:

product *= number.charAt(i + j) - '0';

It doesn't if I remove the '0'.

如果我删除“0”,则不会。

I got this code from another online resource. Can someone explain what the '0' does?

我从另一个在线资源中获得了此代码。有人可以解释'0'的作用吗?

回答by e2-e4

Ascii characters are actually numbers. And 0 .. 9digits are numbers starting from decimal 48 (0x30 hexadecimal).

Ascii 字符实际上是数字。而0 .. 9数字是十进制48(十六进制的0x30)开始的数字。

'0' is 48
'1' is 49
...
'9' is 57

So to get the value of any character digit, you can just remove the '0', ie 48.

因此,要获取任何字符数字的值,您只需删除“0”,即 48。

'1' - '0' => 1
 49 -  48 => 1

If you don't remove '0' (48) the total sum will be over by 48 * numberOfDigits.

如果您不删除 '0' (48),则总和将超过48 * numberOfDigits.

See an ascii tableto locate digits in it.

查看ascii 表以定位其中的数字。

Note that '0'is the character 0, not the string "0"containing the character '0'.

请注意,这'0'是字符0,而不是"0"包含该字符的字符串'0'

回答by yelliver

Let's check the ASCII chart enter image description here

让我们检查一下 ASCII 图表 在此处输入图片说明

You can see the Dec value of '0' -> '9'

您可以看到 '0' -> '9' 的 Dec 值

The charAt() method just gets the character, is automatically converted to int value when calculating.

charAt() 方法只是获取字符,计算时自动转换为int 值。

'0' -> 48
'1' -> 49
...
'9' -> 57

after minus '0':

减“0”后:

'0' - '0' -> 48 - 48 = 0
'1' - '0' -> 49 - 48 = 1
...
'9' - '0' -> 57 - 48 = 9

After all, 'n' - '0' = nwith n is a digit character

毕竟,'n' - '0' = n其中 n 是一个数字字符

回答by NightWatcher

Removing 0from number.charAt(i + j)will give you same character in form of digit (or integer).

删除0fromnumber.charAt(i + j)将以数字(或integer)的形式为您提供相同的字符。

When you are using character in any expression that character is replaced by its ASCIIvalues.

当您在任何表达式中使用字符时,该字符将被其ASCII值替换。

ASCII value for '0'(character 0) is 48 ASCII value for '1'(character 0) is 49

为ASCII值'0'(字符0)为48个ASCII值'1'(字符0)是49

Now, if number.charAt(i + j)is '1'then in your expression '1'is replaced by 49 and '0'is replaced by 48 which will give you 1(integer) as a result.

现在,如果number.charAt(i + j)'1'then 在你的表达式'1'中被 49'0'替换并被 48 替换,这将给你1( integer) 作为结果。

回答by Mike

'0' is the char value of zero.

'0' 是零的字符值。

When you write a string, you're writing an array of 'char' datatypes which the compiler translates into ASCII values (which have a corresponding decimal number value).

当您编写字符串时,您正在编写一个“char”数据类型数组,编译器会将其转换为 ASCII 值(具有相应的十进制数值)。

When you call

你打电话的时候

number.charAt(i + j);

You are actually generating an ASCII value.

您实际上是在生成一个 ASCII 值。

http://www.asciitable.com/

http://www.asciitable.com/

The ASCII value of '0' is:

'0' 的 ASCII 值是:

48

48

'1':

'1':

49

49

. . .

. . .

'9':

'9':

57

57

So when you call

所以当你打电话

number.charAt(i + j) - '0';

You're actually running the operation of an ASCII value minus 48 which is it's actual value as an integer.

您实际上正在运行 ASCII 值减去 48 的操作,这是它作为整数的实际值。

e.g.

例如

('9' - '0') = (57 - 48) = (9 - 0) = 9

('9' - '0') = (57 - 48) = (9 - 0) = 9

回答by Arc676

Characters and integers are practically the same thing because every character has a code (ASCII, but also Unicode in some contexts).

字符和整数实际上是一回事,因为每个字符都有一个代码(ASCII,但在某些情况下还有 Unicode)。

By subtracting '0'from whatever number is in the string, you get the actual value of that integer.

通过'0'从字符串中的任何数字中减去,您将获得该整数的实际值。

'0' > 48
48 - 48 = 0 (integer value)

The same applies to all the other integers. You can do similar things with other characters, such as letter - 'A'to assign a number to every letter based on its position in the alphabet.

这同样适用于所有其他整数。您可以对其他字符执行类似的操作,例如根据letter - 'A'每个字母在字母表中的位置为每个字母分配一个数字。

回答by J?rn Buitink

Characters are stored as numbers. For example, '0' equals 48 in ASCII. So if you want to convert a character into an integer, you have to remove '0' (or remove 48 would work the same way):

字符存储为数字。例如,“0”在 ASCII 中等于 48。因此,如果要将字符转换为整数,则必须删除 '0'(或删除 48 的工作方式相同):

product *= number.charAt(i + j) - 48;

More information: http://www.asciitable.com

更多信息:http: //www.asciitable.com