Java中的bit、byte、字母、漢字之間的關系:
-
- package com.jadyer.demo;
-
- /**
- * Java中的位,字節,字母,漢字之間的關系
- * @see ====================================================
- * @see 1 bit = 1 二進制數據
- * @see 1 byte = 8 bit (范圍:-128到+127)
- * @see 1 字母 = 1 byte = 8 bit
- * @see 1 漢字 = 2 byte = 16 bit
- * @see ====================================================
- * @author 玄玉<http://blog.csdn/net/jadyer>
- * @create Aug 20, 2012 10:28:20 AM
- */
- public class ByteDemo {
- /**
- * @see 控制台打印結果,如下
- * @see 4
- * @see 4
- * @see ----------------------------
- * @see str=ABab
- * @see 65
- * @see 66
- * @see 97
- * @see 98
- * @see ----------------------------
- * @see name=玄玉
- * @see -48
- * @see -2
- * @see -45
- * @see -15
- * @see ----------------------------
- */
- @SuppressWarnings("unused")
- public static void main(String[] args) {
- //byte bb11 = 128; //cannot convert from int to byte
- //byte bb22 = -129; //cannot convert from int to byte
- byte bb11 = 127;
- byte bb22 = -128;
- byte bb33 = 56;
-
- //byte bb44 = '玄'; //錯誤,因為: 1漢字==2字節
- //byte bb55 = 'bb'; //錯誤,因為: 'bb'==2字節
- byte bb44 = 'b';
- byte bb55 = 'B';
- byte bb66 = 'Z';
-
- //int i11 = "玄玉"; //1 int = 32 bit, but int is numeral
- //short ss = '玄玉'; //1 short = 2 byte = 16 bit = 1 漢字
- short ss11 = '玄';
-
- //char cc11 = '玄玉'; //這樣是不可以的
- //char cc22 = 'cc'; //這樣是不可以的
- char cc11 = '玄';
- char cc22 = 'c';
-
- String str = "ABab";
- byte[] data = str.getBytes();
- System.out.println(str.length());
- System.out.println(data.length);
- System.out.println("----------------------------");
-
- System.out.println("str=" + new String(data));
- for(int i=0; i<data.length; i++){
- System.out.println(data[i]);
- }
- System.out.println("----------------------------");
-
- byte[] name = "玄玉".getBytes();
- System.out.println("name=" + new String(name));
- for(int i=0; i<name.length; i++){
- System.out.println(name[i]);
- }
- System.out.println("----------------------------");
- }
- }