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

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

How to Print Max Value of Character?

javacharacter

提问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_VALUEis 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_VALUEis 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 intand it prints an intvalue.

在第三种情况下,您使用 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:

  1. [...]

  2. 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 节)。

当运算符将二进制数字提升应用于一对操作数时,每个操作数必须表示一个可转换为数字类型的值,以下规则依次适用:

  1. [...]

  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

http://www.fileformat.info/info/unicode/char/ffff/index.htm

http://www.fileformat.info/info/unicode/char/ffff/index.htm

回答by Dwayne

Implicit casting by adding 0to Character.MIN_VALUEor Character.MAX_VALUEworks well.
You can also explicitly cast to an int, to get the MIN_VALUEand MAX_VALUEWith two println()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 from U+0000to U+FFFF, the FFFF(65535) is the numeric upper range of the char primitive data type and 0000(0) is the numeric lower range of the char data type. In the two println()methods above, is the Characterclass which is the wrapper class for the charprimitive data type, it simply takes the primitive charand wraps it up in the Characterclass. Allowing us to treat it as an object and giving us access to the fields and methods of the Characterwrapper class.
Then using the MIN_VALUEand MAX_VALUEfields with the dot operator we can cast these Unicode values to int's and the output is the range expressed as decimal numbers is 0to 65535
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. Plane 0has a range of 65535possible characters that it can represent. Wikipedia has a good article on Unicode.

通过添加0Character.MIN_VALUECharacter.MAX_VALUE效果很好的隐式转换。
您还可以显式转换为 int,以获取MIN_VALUEMAX_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+0000U+FFFFFFFF( 65535) 是 char 原始数据类型的数字上限,0000( 0) 是 char 数据类型的数字下限。在println()上面的两个方法中, 是Character作为char原始数据类型的包装类的类,它只需要原始数据类型char并将其包装在Character课堂上。允许我们将其视为一个对象,并允许我们访问Character包装类的字段和方法。
然后使用带有点运算符的MIN_VALUEandMAX_VALUE字段,我们可以将这些 Unicode 值转换为 int 并且输出是表示为十进制数的范围0to65535
当然可以理解,如果某些东西可以隐式转换,它也可以显式转换。
我们在这里也只考虑基本多语言平面(平面 0)。通常一个 Unicode 代码点是通过写来引用的"U+"后跟它的四位十六进制数。所以Plane 0中的这些代码点用四位十六进制数表示,没什么难的。Unicode 代码点只不过是四位十六进制数字。平面可以表示0一系列65535可能的字符。维基百科有一篇关于 Unicode 的好文章。

http://en.wikipedia.org/wiki/Unicode

http://en.wikipedia.org/wiki/Unicode