Domanda

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
È stato utile?

Soluzione

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

As the JLS states:

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

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

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.

Also as the JLS states:

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.

If you've done

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

It would print Character Max Value: 65535

Altri suggerimenti

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

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

Implicit casting by adding 0 to Character.MIN_VALUE or Character.MAX_VALUE works well.
You can also explicitly cast to an int, to get the MIN_VALUE and MAX_VALUE With 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+0000 to 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 Character class which is the wrapper class for the char primitive data type, it simply takes the primitive char and wraps it up in the Character class. Allowing us to treat it as an object and giving us access to the fields and methods of the Character wrapper class.
Then using the MIN_VALUE and MAX_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 is 0 to 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 0 has a range of 65535 possible characters that it can represent. Wikipedia has a good article on Unicode.

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top