4

Consider the simple example:

System.out.println("¬");
System.out.println((int)(('¬')));
System.out.println((char)170);

The output I get is as follows (in order from the sample code):

¬
172
ª

Why is this occurring? I looked into the ASCII chart for character 170, and it says that it is indeed a ¬. Even (on Windows), when one does Alt+170, I get ¬.

Is there something that I'm missing?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 1
    ASCII only defines 128 characters (codes 0-127), so 170 is not a standard ASCII character. The little a seems to be character 170 in [Windows-1252](http://en.wikipedia.org/wiki/Windows-1252). – Jesper Nov 29 '12 at 12:15
  • The link is dead and describes ASCII Extended, not ASCII. – Esailija Nov 29 '12 at 12:25

4 Answers4

2

Java's (internal) default charset is UTF-16 (Unicode).

170 decimal is AA hex, so your character is FEMININE ORDINAL INDICATOR, see here.

The documentation of Primitive Data Types states:

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

jlordo
  • 37,490
  • 6
  • 58
  • 83
0

No. Java is UTF if you don't explicitly specify otherwise. Have a look at http://www.utf8-chartable.de/ and you will see that unicode U+00AC (==172) is the negation sign and unicode U+00AA (==170) is the feminine ordinal character.

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
0

ASCII (Wikipedia) has only 128 characters.

So you are looking up tables in some kind of "enhanced ASCII" table, probably the Latin-1 / ISO-8859-1 character set, while Java uses Unicode, usually the two byte Unicode variant.

List of Unicode characters (Wikipedia) agrees with what Java is doing.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
0

If you really want to print it in a system.out, then use the unicode equivalent as follows;

   System.out.println("\u00AC");

Hope this helps.

dinukadev
  • 2,279
  • 17
  • 22