This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: subtracting char's
 
Similar Threads
Help Or Tips
int c=1; c=c++; // c is then 1 not 2 !
well JAVA may also used? together(with Obj-C) or Java alone
URL rewriting
confusion .....