1

According to the specification

The following conversion combines both widening and narrowing primitive conversions:

byte to char

First, the byte is converted to an int via widening primitive conversion (§5.1.2), and then the resulting int is converted to a char by narrowing primitive conversion (§5.1.3).

Widening conversion of a byte literal to an int does not change the literal. It compulsorily extends the bits but doesn't change the value or the literal. So what's the point in here? JVM could have directly converted a byte literal to a char literal (of course by providing the casting instruction) instead of converting a byte to int and then converting it to char which also requires a casting instruction?

Community
  • 1
  • 1
user12208242
  • 315
  • 1
  • 11
  • The point is `byte` and `char` are ***integral*** types. Also, one is signed and eight bits and the other is unsigned and sixteen. Thus the designers of Java, chose to widen to an `int` and then narrow to a `char` for this particular conversion. – Elliott Frisch Dec 15 '19 at 18:15
  • 1
    Yeah but converting a byte to an int doesn't change anything...it only extends the bits but doesn't change the constant. So why bother to convert to int? – user12208242 Dec 15 '19 at 18:19
  • I'm not certain; I could speculate though. It's either faster, or was easier to implement, the way they chose. Regardless, they documented the decision that was made. You might consider that Java runs on mainframes. [*Interesting*](https://en.wikipedia.org/wiki/UTF-EBCDIC) things are done on mainframes. – Elliott Frisch Dec 15 '19 at 18:22

0 Answers0