public static void main(String...args){
        String str = "A1B2";
        char ch = str.charAt(1);
        System.out.println(ch);//1
        System.out.println(ch==1);//false
        System.out.println((int)ch == 1);//false
        System.out.println(String.valueOf(ch).equals("1"));//true
    }为什么前两个都是false?

解决方案 »

  1.   

    ch中存储的并不是整型的1,而是字符‘1’的unicode编码,你这里用判断ch==1自然是false,同理你将ch转换成int后ch的值不是1而是1的unicode码值49
      

  2.   

    '1'的ACSII码是49,所以ch==49是true; (ch-48)==1也是true
      

  3.   

    因为System.out.println(ch)里面的ch被自动转换成字符串打印。。“1”
       System.out.println(ch==1)里的ch是unicode编码对应的数字,不是1,具体是什么数需要查查。。
      

  4.   

    这里和ASCII码有什么关系?java中的字符集是用16位unicode进行存储的,这里的ch实际存储值是'u0031'
    如果是ASCII,char占两个字节要怎么编码?
      

  5.   

    对于数字和英文字符,其数值在ascii范围之内,所以一般说到数字及字母文字,都直接称呼其ascii码。也就是说,就算是u16,0x0031和0x31,对于数值类型来说,没有区别吧