Java 如何打印字符的最大值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23413497/
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
How to Print Max Value of Character?
提问by Hello World
Although it is a very Initial level question but I find it complex one. Actually I want to know what is happening behind the scene? Why Character.MAX_VALUE does not print the Max Value of char(Which is 65535) and MAX_VALUE-1 does.
虽然这是一个非常初级的问题,但我觉得它很复杂。其实我想知道幕后发生了什么?为什么 Character.MAX_VALUE 不打印字符的最大值(即 65535)而 MAX_VALUE-1 可以。
System.out.println("Byte Max Value: "+Byte.MAX_VALUE);//print 127 Ok!
System.out.println("Character Max Value: "+Character.MAX_VALUE);//print ?(Question Mark)
System.out.println(Character.MAX_VALUE-1);//print 65534
采纳答案by Alexis C.
Because in the second line, Character.MAX_VALUE
is concatenated with the String.
因为在第二行中,Character.MAX_VALUE
是与字符串连接的。
As the JLSstates:
正如JLS所说:
The string concatenation operator + (§15.18.1), which, when given a String operand and an integral operand, will convert the integral operand to a String representing its value in decimal form, and then produce a newly created String that is the concatenation of the two strings
字符串连接运算符 +(第 15.18.1 节),当给定一个字符串操作数和一个整数操作数时,会将整数操作数转换为一个以十进制形式表示其值的字符串,然后生成一个新创建的字符串作为连接两个字符串的
As Character.MAX_VALUE
is not printable, you don't see it.
由于Character.MAX_VALUE
不可打印,您看不到它。
In the third case, your doing a substraction with an int
, thus the whole expression is casted to int
and it prints an int
value.
在第三种情况下,您使用 a 进行减法int
,因此整个表达式被强制转换为int
并打印一个int
值。
Also as the JLSstates:
同样正如JLS所说:
The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.
[...]
Binary numeric promotion is performed on the operands (§5.6.2).
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:
[...]
Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
二元 + 运算符在应用于两个数值类型的操作数时执行加法,产生操作数的总和。
[...]
对操作数执行二进制数字提升(第 5.6.2 节)。
当运算符将二进制数字提升应用于一对操作数时,每个操作数必须表示一个可转换为数字类型的值,以下规则依次适用:
[...]
扩展原语转换(第 5.1.2 节)用于转换一个或两个操作数,如以下规则所指定:
如果任一操作数的类型为 double,则另一个将转换为 double。
否则,如果任一操作数的类型为 float,则另一个将转换为 float。
否则,如果任一操作数的类型为 long,则另一个将转换为 long。
否则,两个操作数都被转换为 int 类型。
If you've done
如果你已经完成
System.out.println("Character Max Value: "+(Character.MAX_VALUE+0));
It would print Character Max Value: 65535
它会打印 Character Max Value: 65535
回答by Peter Lawrey
Character.MAX_VALUE is \uFFFF. This is not a printable character by definition. When you perform an operation like -1 or +1 you are changing the type to an int
Character.MAX_VALUE 是 \uFFFF。根据定义,这不是可打印的字符。当您执行 -1 或 +1 之类的操作时,您将类型更改为int
回答by Dwayne
Implicit casting by adding
0
toCharacter.MIN_VALUE
orCharacter.MAX_VALUE
works well.
You can also explicitly cast to an int, to get theMIN_VALUE
andMAX_VALUE
With twoprintln()
methods....System.out.println("char min. value: " + (int)Character.MIN_VALUE);
System.out.println("char max. value: " + (int)Character.MAX_VALUE);
In your third line you have...System.out.println(Character.MAX_VALUE-1);//print 65534
You are essentially implicitly casting the Unicode maximum value to an int and then subtracting 1 from that maximum value. In Unicode format the range is fromU+0000
toU+FFFF
, theFFFF
(65535
) is the numeric upper range of the char primitive data type and0000
(0
) is the numeric lower range of the char data type. In the twoprintln()
methods above, is theCharacter
class which is the wrapper class for thechar
primitive data type, it simply takes the primitivechar
and wraps it up in theCharacter
class. Allowing us to treat it as an object and giving us access to the fields and methods of theCharacter
wrapper class.
Then using theMIN_VALUE
andMAX_VALUE
fields with the dot operator we can cast these Unicode values to int's and the output is the range expressed as decimal numbers is0
to65535
Of course it is understood that if something can be implicitly cast it can also be explicitly cast.
We are also only considering here the Basic Multilingual Plane (Plane 0). Normally a Unicode code point is referred to by writing"U+"
followed by its four digit hexadecimal number. So these code points in Plane 0 are expressed by a four digit hexadecimal number, nothing to difficult. The Unicode code points are nothing more than four digit hexadecimal numbers. Plane0
has a range of65535
possible characters that it can represent. Wikipedia has a good article on Unicode.
通过添加
0
到Character.MIN_VALUE
或Character.MAX_VALUE
效果很好的隐式转换。
您还可以显式转换为 int,以获取MIN_VALUE
和MAX_VALUE
使用两种println()
方法...。System.out.println("char min. value: " + (int)Character.MIN_VALUE);
System.out.println("char max. value: " + (int)Character.MAX_VALUE);
在您的第三行中,您有...System.out.println(Character.MAX_VALUE-1);//print 65534
本质上是将 Unicode 最大值隐式转换为 int,然后从该最大值中减去 1。在 Unicode 格式中,范围是从U+0000
到U+FFFF
,FFFF
(65535
) 是 char 原始数据类型的数字上限,0000
(0
) 是 char 数据类型的数字下限。在println()
上面的两个方法中, 是Character
作为char
原始数据类型的包装类的类,它只需要原始数据类型char
并将其包装在Character
课堂上。允许我们将其视为一个对象,并允许我们访问Character
包装类的字段和方法。
然后使用带有点运算符的MIN_VALUE
andMAX_VALUE
字段,我们可以将这些 Unicode 值转换为 int 并且输出是表示为十进制数的范围0
to65535
当然可以理解,如果某些东西可以隐式转换,它也可以显式转换。
我们在这里也只考虑基本多语言平面(平面 0)。通常一个 Unicode 代码点是通过写来引用的"U+"
后跟它的四位十六进制数。所以Plane 0中的这些代码点用四位十六进制数表示,没什么难的。Unicode 代码点只不过是四位十六进制数字。平面可以表示0
一系列65535
可能的字符。维基百科有一篇关于 Unicode 的好文章。