Win a copy of Design for the Mind this week in the Design forum!

# assignment for char

Anonymous
Ranch Hand
Posts: 18944
Hi ...
I recently came across this site and have been avidly watching the discussions...i have a question which might sound ridiculous to some of u...
i came across this expression....
char x =3; and i want to know if this is legal if so how come ???

Khurram Fakhar
Ranch Hand
Posts: 65
hi there,
Actually character types r defined 16 bt unsigned unicode values,Strictly speaking , a character is a numeric value basically, equavalent to a short ( i.e 16 bit) integer value.
characters can be declared as
char abc = 'a';
As characters r numeric values ,so it is possible to assign a numeric literal to a character typed variable and a character literal to an integer typed variable
here is the example
char abc = 48;
integer def = 'a';
i hope it will help ya
Regards
Khurram fakhar

Anonymous
Ranch Hand
Posts: 18944
hi khurram.......
Thanks a lot .....that really cleared up things.
Thanks again.

Anonymous
Ranch Hand
Posts: 18944

Khurram....(one more ques)
Hey i have one more question based on ur explanation.....if i can assign char values to int variable..doesnt'that come under narrowing conversion.
Which, if i understand correctly cannot occur without an explicit cast.
Unless its from int to char, short or byte...( the value being within range of course.

Anonymous
Ranch Hand
Posts: 18944
hi
refer the JLS for Conversions and Promotions here http://java.sun.com/docs/books/jls/html/5.doc.html#27529
basically char is unique in the sense that
1. conversions from byte, short to char are not Widening converisons rather are narrowing conversions.
2. even conversions from char to byte, short are narrowing conversions
to clarify point 1 byte and short take negetive numbers and char does not so explicit cast is needed to convert both positive and negetive byte and short to char.
to clarify point 2 char has a range of around 0-65535 . this huge a number does not fall into the range of byte or short. as such again a cast is required.
for Assignment conversions refer JLS 5.2 which states the follwoing
for example byte b=5;
a narrowing primitive conversion may be used if all of the following conditions are satisfied:
The expression is a constant expression of type 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.

coming to int to char and char to int conversions.
u can convert from char to int without casting example
char c='a';
int i=c;
however u cannot convert from int to char. as the range of int is more than that of char a definate cast is required.
int i=60;
char c=(char)i;
hope this helps.
rahul.

[This message has been edited by rahul_mkar (edited June 07, 2000).]

Anonymous
Ranch Hand
Posts: 18944

HI Rahul....

Thanks ....ur explanation really cleared up things....but i have one more doubt based on ur answer.
consider this
char c='A';
int i =c;
suppose i want to calculate the valeue
result m= i+2;
with what vaue of i will i do that....( i guess i want to know how where do i find the comparable int values....god..that sounds a bit jumbled up. I hope u understand my question???

Anonymous
Ranch Hand
Posts: 18944
try to remeber this:
boolean is on bit with a default of FALSE
byte is 8 bits (signed) with a default of 0
short is 16 bits (signed) witha default of 0
char is 16 bits (unsigned) with a default of \0000 (unicode 0)
int is 32 bits (signed) with a default of 0
long is 64 bits (signed) witha default of 0
float is 32 bits (signed) with a default of 0
double is 64 bits (signed) with a default of 0
obj references have a default of null
String being a quazi object ha a default of null
* referencing null objects will create a NullPointerException
order of convertion:
bite > short > int > long > float > double
.......char >
hope this helps
monty

[This message has been edited by monty6 (edited June 08, 2000).]

Anonymous
Ranch Hand
Posts: 18944
hi gp,
i am sorry i am a little late.
u r doubt was bascily IMO
char c='a';
int i=c;// statement 1
int m=i+2;
IMO u mean what does i contain after statement 1.
i does contain an integer not a character and will not give an error.
note that char i='a' does not mean a is stored in memory rather the ascii or unicode integer associated with 'a' is stored.
Hope this clears things up.
regds
Rahul.