V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
amiwrong123
V2EX  ›  Java

Java 的 char 类型就是使用二字节长度的 Unicode 码作为底层存储,这句话对吗

  •  
  •   amiwrong123 · Feb 10, 2020 · 3119 views
    This topic created in 2270 days ago, the information mentioned may be changed or developed.

    因为 Unicode 的范围是 0x0000 至 0x10FFFF,但 char 只有两个字节,所以 java 的 char 类型只使用的 Unicode 的 0x0000 至 0xFFFF 范围吗?

    6 replies    2020-02-11 09:17:57 +08:00
    est
        1
    est  
       Feb 10, 2020
    surrogate pair 了解一下。 -XX:+UseCompressedStrings 了解一下
    amiwrong123
        2
    amiwrong123  
    OP
       Feb 10, 2020
    @est
    这个 surrogate pair 是 utf-16 里的东西,所以也有可能两个 char 代表一个字符的呗。

    UseCompressedStrings 百度了下,它就是让 jvm 认为每个字符都是 ascii,也都是单字节的呗。
    zhuge415
        3
    zhuge415  
       Feb 10, 2020
    超过范围的,编译就会报错
    kx5d62Jn1J9MjoXP
        4
    kx5d62Jn1J9MjoXP  
       Feb 10, 2020   ❤️ 1

    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).

    超出范围的无法用 char 表示, 只能用 String
    palmers
        5
    palmers  
       Feb 10, 2020   ❤️ 1
    spadger
        6
    spadger  
       Feb 11, 2020
    看起来还是用 utf8 比较好。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5703 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 06:15 · PVG 14:15 · LAX 23:15 · JFK 02:15
    ♥ Do have faith in what you're doing.