Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion

 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will the output of the following program? Does it compile as
we are assigning an integer value 80 to a character which is a narrowing conversion? Please help me with explanantion.
class Test1{
static char c=10;
public static void main(String [] args){
System.out.println(c);
}
}
 
chetan nain
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heres it:
1. compilation will proceed unhindered. output obtained is 10. this is because the toString() method of the wrapper class Character is called in order to convert char c into its string representation.
HTH ,
chetan
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chetan,
I am not getting output of 10. i am getting blank output.
I think it is doing some sort of conversion here. When i assign 57 , i am getting 9 as output.
Moreover, i do not think wrapper classes are coming into picture here.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there,
I think I know what is going on. When you do the char c= 10, you are assigning a value of 'NL'(New Line character) to the char type variable c. Then when you use System.out.println(c) to print it out, you will get the char variable's real value 'NL' printed out on the screen, which is a new line. When you tested it with value of 57, if you look at the ASCII table, you will find out that 57 in the table represents the character '9', so you will get 9 printed out.
All in one, the point is: System.out.println(char) will print out the character as what it represents in the ASCII table/Unicode table, but not what its value is in int.

Hope this will help.

Tieyi
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tieyi.
 
chetan nain
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops , sorry for the mistake. wrapper classes do not come into picture here. they would if you had declared
char c = '10';
 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will the output of the following program? Does it compile as
we are assigning an integer value 80 to a character which is a narrowing conversion? Please help me with explanantion.
class Test1{
static char c=10;
public static void main(String [] args){
System.out.println(c);
}
}
I thought it would give a compilation error as it,s a narrowing conversion.Please let me know upto what number can we assign to a character.Because I tested the above code assigning c a very big number & got the compilation error..So please tell me if there is something we should know.
Please help.
Thanks in advance
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mita,
Character range is 0 to (2 power 16)-1 .
So you can directly assign from 0 to 65535. But not above that.
 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Doit.
mita
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic