File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assignment for char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assignment for char" Watch "assignment for char" New topic

assignment for char

Ranch Hand

Joined: Nov 22, 2008
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

Joined: May 29, 2000
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
Khurram fakhar
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi khurram.......
Thanks a lot .....that really cleared up things.
Thanks again.
Ranch Hand

Joined: Nov 22, 2008
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.
Thanks in advance.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
refer the JLS for Conversions and Promotions here
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.

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

Joined: Nov 22, 2008
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???
Ranch Hand

Joined: Nov 22, 2008
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

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

Joined: Nov 22, 2008
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.

wood burning stoves
subject: assignment for char