Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Char

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79535
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79535
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79535
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look up what 9 is in Unicode.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79535
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic