File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Char" Watch "Char" New topic
Author

Char

Aruna Balasuriya
Ranch Hand

Joined: Nov 14, 2009
Posts: 44
public class Test3 {


public Test3() {
}

public static void main(String[] args) {
char x = 'a';
char y = 'b';

System.out.println(x);//1
System.out.println(y);//2
System.out.println(x+y);//3
}

}

line 1 prints a
line 2 prints b
but line 3 prints 195

but it should be ab.Please explain
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
So many people have this delightful misconception that a char is a character. Well it isn't. It's a number. So you can do arithmetic with it. Exactly what you are doing there.
Aruna Balasuriya
Ranch Hand

Joined: Nov 14, 2009
Posts: 44
Campbell Ritchie wrote:So many people have this delightful misconception that a char is a character. Well it isn't. It's a number. So you can do arithmetic with it. Exactly what you are doing there.


So why in line 1 it print a not 67(unicode value)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Aruna Balasuriya wrote:
So why in line it print a not 67(unicode value)


When you add two chars, the result is an int. So, when you tried to print the result, it is printing an int. String concat only works for strings -- not chars.

As for the earlier prints, they are printing chars and not ints -- and the print system knows what to do with them.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Aruna Balasuriya wrote:So why in line 1 it print a not 67(unicode value)
Maybe because its Unicode value is 0x61??
Aruna Balasuriya
Ranch Hand

Joined: Nov 14, 2009
Posts: 44
One More Question

class Test2{
public void method() {}

public static void main(String[] args) {

char s1 = 9;
char s2 = '9';


System.out.println(s1);//1
System.out.println(s2);//2
}

}


Here line 1 prints nothing. Line 2 prints 9. Please Explain
Rob Poulos
Ranch Hand

Joined: Aug 21, 2009
Posts: 49
If you wanted it to print out "ab" instead of the sum of the unicode values you could preceed the addition with "" (empty quotes) (line 7)

for example:


anything after the empty quotes is treated as a string


SCJA, ITIL V3 Foudation, Studying for SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Look up what 9 is in Unicode.
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

Aruna Balasuriya wrote:
Here line 1 prints nothing. Line 2 prints 9. Please Explain

The reason for this is that dec 9 represents a code for horizontal tab ... that's why you see an "empty" line.

Try this ... just to get the feeling:
Perhaps you should start with the ASCII table first:
http://www.asciitable.com/


Regards,
Rok


SCJP, SCWCD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Horizontal tab? I didn't know that; I hadn't looked in the Unicode page, but knew it was a control character of some sort.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

The result of any addition is never smaller than an int. Therefore, char + char == int. This means that x + y is not a char anymore but an int, and is therefore printed as an int. You will need to cast the result back to char first to see it as a char.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Char