Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - why does char get implicitly cast to byte (and short) primitive, when it shouldn't?

Certain functionality of the compiler puzzles me (Oracle JDK 1.7 using Eclipse).

So I've got this book that says char primitive needs to be explicitly cast to short and byte and this all makes sense due the data types' allowed ranges don't overlap.

In other words below code works (but wouldn't work without the explicit type casts):

char c = '&';  
byte b = (byte)c;
short s = (short)c;

Printing b or s correctly displays the number 38, which is the numeric equivalent of (&) in Unicode.

Which brings me to my actual question. Why does the following work as well?

byte bc = '&';
short sc = '&';
System.out.println(bc); // Correctly displays number 38 on the console
System.out.println(sc); // Correctly displays number 38 on the console

Now I would certainly understand the following (which works too):

byte bt = (byte)'&';
System.out.println(bt); // Correctly displays number 38 on the console

But this no-compiler-warning char to byte (and short) "sneak conversion" doesn't seem right to me.

Can some one explain, why this is allowed?

Could the reason be in the interpretation of the '<char>' itself, so that it doesn't actually ever get to a char primitive state but is handled as a numeric (octal or hexadecimal etc) value?

like image 573
straville Avatar asked Jun 12 '13 10:06

straville


People also ask

Why char uses 2 bytes in Java and what is u0000?

The 'char' data type in Java originally used for representing 16-bit Unicode. Therefore the size of the char data type in Java is 2 byte, and same for the C language is 1 byte. Hence Java uses Unicode standard. What are features of Java language?

Can we type cast byte to char?

If you want to encode/decode characters from bytes, use Charset , CharsetEncoder , CharsetDecoder or one of the convenience methods such as new String(byte[] bytes, Charset charset) or String#toBytes(Charset charset) . You can get the character set (such as UTF-8 or Windows-1252) from StandardCharsets .

What is the effect of converting an integer to byte?

When an integer value is converted into a byte, Java cuts-off the left-most 24 bits. We will be using bitwise AND to mask all of the extraneous sign bits. Here is an illustration of a Java Program that converts an integer to byte.

What is the difference between char and byte in Java?

The main difference between a byte and char data type is that byte is used to store raw binary data while other is used to store characters or text data. You can store character literals into a char variable e.g. char a = 'a'; A character literal is enclosed in single quotes.


1 Answers

Basically, the specification of assignment conversion specifies that

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:

A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

Your '&' is precisely "a constant expression of type byte, short, char, or int".

like image 135
Denys Séguret Avatar answered Oct 04 '22 23:10

Denys Séguret