Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Char

 
Aruna Balasuriya
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up what 9 is in Unicode.
 
Rok Ć telcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic