Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

subtracting char's

 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic