Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I assign an int to a char variable without an explicit cast?

Tags:

java

casting

I wanted to know why this snippet works.

char ch1;
ch1 = 'a' + 1;
System.out.println(ch1);

In line 2, isn't the right hand side promoted to an int and then for assigning int to char, won't we need an explicit cast?

Similarly, I understand what happens when you do ch1 = 65. But since Java does not allow automatic down type conversion, don't we need explicit cast from int to char?

like image 796
maverick Avatar asked Sep 03 '11 23:09

maverick


People also ask

Is it legal to assign an int to a char variable?

The character is only a representation of an integer value. For example, '0' can be written as 0x30 or 48 , 'a' is an alternative for 0x61 or 97 , etc. So the assignment is perfectly valid. Save this answer.

Can we assign number to char variable?

We can assign an ASCII value (from 0 to 127) to the char variable rather than the character itself.

Can you assign an int to a char C++?

Convert int to a char in C++A better, safer option to cast an integer to a char is using static_cast , as shown below. C++ also offers three other casting operators – dynamic_cast , reinterpret_cast , and const_cast (Read more here). That's all about converting an int to a char in C++.

Can int be assigned char in Java?

An integer can be converted into a character in Java using various methods. Some of these methods for converting int to char in Java are: using typecasting, using toString() method, using forDigit() method, and by adding '0'.


2 Answers

Because the Java Language Specification says:

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.

So you're right in that the expression is promoted to int, but because it's a constant expression, the cast is not necessary. If it involved a variable (or its value did not fit into a char), it would be different.

For this kind of question it's best to look at the language specification right away, as it's the authoritative source and quite readable for a specification.

like image 154
Michael Borgwardt Avatar answered Sep 27 '22 16:09

Michael Borgwardt


I'm using Eclipse at the moment. There are two things to note:

  • The compiler checks for bounds during initialization.
  • The compiler calculates the values of constant expressions, such as 3 * (2 + 1).

This works:

byte b = 127; // works, range of a byte is [-128, 127]

But this does not:

byte b = 128; // does not work, outside of range

This works:

byte b = 100 + -228; // works, 100 + -228 = -128

But this does not:

byte b = 1;
byte c = b + 1; // does not work, why not?

And this does not, either:

byte b = 1;
byte c = b + (byte) 1; // does not work, why not?

Note that b is a variable expression. If a variable expression is involved, the result of the operator + is at least as large as an int. Therefore, you can't assign it to c. Unlike constant expressions, the compiler does not calculate variable expressions.

The compiler would similarly complain for short and char - try it out yourself.

And finally, using final on a variable effectively turns it into a constant expression, so this would work:

final byte b = 1;
byte c = b + 1; // works
like image 34
Timbits Avatar answered Sep 27 '22 15:09

Timbits