my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question about conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question about conversion" Watch "a question about conversion" New topic
Author

a question about conversion

Jason Wang
Greenhorn

Joined: Apr 13, 2001
Posts: 26
Is this conversion without explicit casting legal?
char c = 10;
and what about this:
int i = 10;
char c =i;
Any help are welcome
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Jason Wang:
Is this conversion without explicit casting legal?
char c = 10;
and what about this:
int i = 10;
char c =i;
Any help are welcome


There would be a compilation error saying " Invalid type conversion. Explicit cast is required for converting char to int"
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Hie,
Only widening conversion are legal.Narrowing conversions are illegal and you have to do an explicit cast to convice the compiler that it is indeed your wish to narrow your type and that you are solely responsible for any loss of accuracy, etc. A widening conversion is one that converts a type of smaller size to a type of bigger type (in terms of premitives).so in your case char c = 10 is illegal because all numerical literals are considered to be of type int, so you are trying to convert an int to a char...ILLEGAL,because a char is smaller than an int in terms of size.Recall that char uses 2 bytes and int uses 4 bytes.
Regards,
Herbert
Anand Iyer
Greenhorn

Joined: Jun 13, 2000
Posts: 26
I tried..
char c=10;
System.out.println(c);
well.. the program compiled without error..
But when it printed out it was blank..
Why?Could anyone help me with this?
Thanks,
Anand
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Anand,
I had explained a related topic for Wai Iu and Umesh here. Please take a look at it and if you have any more doubts please reply back.
regds
maha anna
An interesting discussion on conversion topic here and here
regds
maha anna
[This message has been edited by maha anna (edited June 13, 2000).]
Jason Wang
Greenhorn

Joined: Apr 13, 2001
Posts: 26
Thanks for help, maha anna.
Now I got the picture....
if RH operand is int constant and in the range of the LH operand type, no need to explicit cast, otherwise it is required.
Rose
Greenhorn

Joined: Sep 08, 2002
Posts: 6
Originally posted by Anand Iyer:
I tried..
char c=10;
System.out.println(c);
well.. the program compiled without error..
But when it printed out it was blank..
Why?Could anyone help me with this?
Thanks,
Anand

Hi Anand,
As you may know
char a=65;
System.out.println("Result = " + a);
gives you
Result = A
Means the charactor value corresponding to 65 is 'A' is printed.
If you code like this :
char c=10;
System.out.println(c);

The result will be the charactor value corresponding to 10 which "the new line character".
The new line charactor is the one which when tried to print works as '\n' in
System.out.println("This is for testing. \n Hello");

So your result will be "The current line is skipped ".

To make more clear : I tried this Code
public static void main(String args[]){
char a=65;
System.out.println("Result = " + a);
System.out.println("\nThis is for testing. \n Hello\n");
char ch=10;
System.out.println("This is for testing. "+ch+"Hello"+
" Java is cool"+ch+ " Yep You Got it");
}
The result was :
Result = A
This is for testing.
Hello

This is for testing.
Hello Java is cool
Yep You Got it

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Jason.Yes the statement char c=10 will certainly compile and execute.You can assign integer literals to primitive data types byte,short or char AS LONG AS THEY ARE WITHIN THE VALID RANGE FOR THAT PRIMITIVE TYPE.So char c=10 is valid for char.In this case I think it will correspond to a newline character.This is a special case as char is treated as a subset of int.You can even use all the operators on char u normally use on an int.
But the statement c=i will not compile as an int's size is 32 bites and char's is 16.So u have to use a explicit cast such as
c=(Char)i; to make the statement work .
I hope I have been clear enough.
Anand Iyer
Greenhorn

Joined: Jun 13, 2000
Posts: 26
Thank You all very much.. Kind of really stupid of me not to analyse the result further. I did see a blank line.. corresponding to 'new line' for char value 10
Thanks, once again.
 
jQuery in Action, 2nd edition
 
subject: a question about conversion
 
Similar Threads
"conversion" of primitive types
ascii to binary conversion
alleged lost of precision
Will it be a narrow Conversion
Conversion between char and short ?