File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about conversion

 
Jason Wang
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 289
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic