如何打印字符的最大值? [英] How to Print Max Value of Character?

查看:134
本文介绍了如何打印字符的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管这是一个非常初级的问题,但我发现它很复杂。实际上我想知道幕后发生的事情?为什么Character.MAX_VALUE不能显示char的最大值(哪个是65535)而MAX_VALUE-1却可以显示。

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.

    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


推荐答案

因为在第二行中, Character.MAX_VALUE 与字符串连接。

Because in the second line, Character.MAX_VALUE is concatenated with the String.

作为 JLS 状态:


字符串连接运算符+(第15.18.1节),当给定
字符串操作数和整数操作数时,会将字符串
操作数转换为表示其值的字符串以十进制形式,然后
产生一个新创建的String,它是两个
字符串的串联。

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

由于 Character.MAX_VALUE 无法打印,因此您看不到它。

As Character.MAX_VALUE is not printable, you don't see it.

在第三种情况下,您正在执行 int 的减法,因此,整个表达式将转换为 int ,并显示 int 值。

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.

也与 JLS 状态:


二进制+运算符应用于两个数字类型的操作数
时,会执行加法运算,操作数之和。

The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.

[...]

对操作数进行二进制数值提升(§ 5.6.2)。

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. [...]

  1. [...]

使用Wilding基本转换(第5.1.2节)来转换两个操作数中的一个或
,如以下规则所指定:

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

如果其中一个操作数是double类型,另一个是conv

If either operand is of type double, the other is converted to double.

否则,如果其中一个操作数的类型为float,则另一个将
转换为float。

Otherwise, if either operand is of type float, the other is converted to float.

否则,如果其中一个操作数的类型为long,则另一个将
转换为long。

Otherwise, if either operand is of type long, the other is converted to long.

否则,两个操作数都将转换输入int


如果完成

System.out.println("Character Max Value: "+(Character.MAX_VALUE+0));

它将打印字符最大值:65535

这篇关于如何打印字符的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆