java中的 char 數據類型使用 Unicode 編碼,占用兩個字節內存。
因為Unicode 采用無符號編碼,一共可以存儲 0x0000 ~ 0xffff 共65536 個字符,
而 int 是有符號4個字節,剛好一半是2個字節,所以在 java 將 char 看作整數(0-65535),於是我做了一個測試:
//unicode 無符號編碼 0x0000 ~ 0xffff (16進制)總共 可以表示 0-65535 for(int i =0 ;i<=65535;i++){ if(i%10==0)System.out.println(""); char s = (char) i; System.out.print(" "+i+"|"+s); Thread.sleep(300); }
備注,其他基本數據類型:byte \ short \ int \ long \ float \ double 分別占用內存 124848 個字節。