wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "char" Watch "char" New topic
Author

char

Shravan Durvasula
Greenhorn

Joined: Mar 06, 2004
Posts: 19
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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 ]

SCJP Tipline, etc.
Shravan Durvasula
Greenhorn

Joined: Mar 06, 2004
Posts: 19
Thanks Corey. That really helped.
fethi makhlouf
Ranch Hand

Joined: Feb 16, 2004
Posts: 50
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?


SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
Shravan Durvasula
Greenhorn

Joined: Mar 06, 2004
Posts: 19
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: char
 
Similar Threads
Why Integer only 16bit??
assignments and + operator
Casting Problem
char array
long's L