GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes subtracting char's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "subtracting char Watch "subtracting char New topic
Author

subtracting char's

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
i didn't think this would work but it does, i guess it is because it works out what 16 bit codes the letters are and subtracts them from one another.
...
class Hmm {
public static void main(String[] args) {
char c = 'c'-'a';
System.out.println(c);
}
}
... it results in a smiley face


giddee up
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
So, you've discovered that char is an unsigned integer data type. 'c' is the 99th character and 'a' is the 97th character. 99 - 97 is 2. According to ASCIITable.com, the 2nd character is the start of text character. When displayed to my dos console, it sure looks like a face (though I cannot confirm his disposition).


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
yeah, he seems pretty happy on the display of this little toshiba satellite.
i wonder if that sort of thing will be examined in the cert exam (not that this is the cert forum).
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I would guess that the SCJP exam assumes a basic understanding of the primitive data types in Java, including the understanding that char is an integer data type.
 
GeeCON Prague 2014
 
subject: subtracting char's