File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Conversion" Watch "Conversion" New topic
Author

Conversion

Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
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

Joined: Jun 21, 2000
Posts: 159
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

Joined: Aug 03, 2000
Posts: 169
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

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

Joined: Aug 03, 2000
Posts: 169
Thanks Tieyi.
chetan nain
Ranch Hand

Joined: Jun 21, 2000
Posts: 159
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

Joined: Jul 19, 2000
Posts: 50
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

Joined: Aug 03, 2000
Posts: 169
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

Joined: Jul 19, 2000
Posts: 50
Thanks Doit.
mita
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conversion