Hello all: The following statement does not work : *************** int i = 100; char c = i; *************** But this one works : *************** char c = 100; *************** Why? Thanks a lot in advance .. -skd
In general, any integral literal encountered in a Java application is considered an int and has 32 bits. However, when you assign directly from a literal, Java is a little more forgiving. The compiler will determine whether or not the literal you are trying to assign is within the range of the type you are assigning to and, if it is, it will allow the assignment (and perform an implicit narrowing conversion). However, assigning an int to a char, which is a narrowing conversion is never allowed as you might lose data. The compiler isn't "smart" enough to look back and see what's really in your variable because that might change. If you were to change your code to this:
It will work again as this is equivalent to assigning from a literal. Here's a bit from the JLS, §5.2 Assignment Conversion: --------------------------------------------------------------------------- Assignment conversion occurs when the value of an expression is assigned (�15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of an identity conversion (�5.1.1), a widening primitive conversion (�5.1.2), or a widening reference conversion (�5.1.4). In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied: - The expression is a constant expression of type byte, short, char or int. - The type of the variable is byte, short, or char. - The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.
--------------------------------------------------------------------------- I hope that helps, Corey [ March 31, 2004: Message edited by: Corey McGlone ]
Hi i still don't undrestand why final is OK, and no Final isn't? normally it has no relation with that, it should have only relation with the scope! Could you please explain more Corey?
Joined: Dec 20, 2001
Originally posted by fethi makhlouf: Hi i still don't undrestand why final is OK, and no Final isn't? normally it has no relation with that, it should have only relation with the scope! Could you please explain more Corey? When you declare a variable as being final, you're basically stating that the variable in question is a constant. The compiler knows that the value can't change. Therefore, if you have this:
The compiler will complain at you because, between those two statements, the value of i might have changed from 50 to something else. Therefore, the compiler gives you an error. If you change to this:
The compiler is fine with this because it knows, when it reaches the assignment to b that the variable i will have the value 50. It has to have that value because we declared i to be final - once its value has been set, it can't be changed. Therefore, the compiler treats this siuation to be the same as "byte b = 50;" because it knows that i must be equal to 50. I hope that helps, Corey
Joined: Mar 06, 2004
Tryinh to add more to what Corey said ... The size of an int is 32 bits. The size of a char is 16. When you try to assign an int value to char, the compiler tries to see if the value being assigned can be fit within the bit-size of the value that it is being assigned to. If yes, it will go ahead and do it. If not, it will give a loss od pression compile time error. So when you declare an int literal value to be final, the compile will know that this value is not going to change anymore. The number of bits required for holding that value is constant. Hence, this program is also not going to work.
Reason: By the time, the compiler tries to do assignment in the second statement it knows that the number of bits required for this assignment is more than 16. It is interesting to note that the following think doesnot work either.
I am wondering why only char, byte and short can be assigned values of different primitives (for example char) and why not int. Thanks, -skd
Joined: Dec 20, 2001
Shravan, If you look back at my previous post, one of the requirements for an automatic narrowing conversion is:
The expression is a constant expression of type byte, short, char or int.
Note that it doesn't say long. Therefore, you can't do an automatic narrowing conversion on a long. Why not? Honestly, I'm not really sure, but that's what the JLS says. I imagine the reasoning was based on some design decision long, long ago. Perhaps somsone else knows why (or perhaps I'll find some extra time later on to hunt it down). Corey