Java Unicode Confusion

前端 未结 5 1056
无人及你
无人及你 2020-12-18 05:03

HEy all, I have only just started attempting to learn Java and have run into something that is really confusing!

I was typing out an example from the book I am using

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 06:01

    0xAB is 1/2 in good old Codepage 437, which is what Windows terminals will use by default, no matter what codepage you actually set.

    So, in fact, the char value represents the "«" character to a Java program, and if you render that char in a GUI or run it on a sane operating system, you will get that character. If you want to see proper output in Windows as well, switch your Font settings in CMD away from "Raster Fonts" (click top-left icon, Properties, Font tab). For example, with Lucida Console, I can do this:

    C:\Users\Documents>java CharDemo
    131
    a + b is AB
    y is K and half is ½    
    
    C:\Users\Documents>chcp 1252
    Active code page: 1252
    
    C:\Users\Documents>java CharDemo
    131
    a + b is AB
    y is K and half is «
    
    C:\Users\Documents>chcp 437
    Active code page: 437
    

提交回复
热议问题