zoukankan      html  css  js  c++  java
  • (转载)char与byte的区别

    (转载)http://www.2cto.com/kf/201303/193644.html

    很多初学者肯会对char和byte这两种数据类型有所疑惑,相互混淆,今天特地查了好多资料,对byte和char两种数据类型进行了总结和比较,先将结果与大家分享:

            byte 是字节数据类型 ,是有符号型的,占1 个字节;大小范围为-128—127 。char 是字符数据类型 ,是无符号型的,占2字节(Unicode码 );大小范围 是0—65535 ;char是一个16位二进制的Unicode字符,JAVA用char来表示一个字符 。
            下面用实例来比较一下二者的区别:
    1、Char是无符号型的,可以表示一个整数,不能表示负数;而byte是有符号型的,可以表示-128—127 的数;如:
    [html]  
    char c = (char) -3; // char不能识别负数,必须强制转换否则报错,即使强制转换之后,也无法识别  
    System.out.println(c);  
    byte d1 = 1;  
    byte d2 = -1;  
    byte d3 = 127; // 如果是byte d3 = 128;会报错  
    byte d4 = -128; // 如果是byte d4 = -129;会报错  
    System.out.println(d1);  
    System.out.println(d2);  
    System.out.println(d3);  
    System.out.println(d4);  
    结果为:
    ?
    1
    -1
    127
    -128
    2、char可以表中文字符,byte不可以,如:
    [html]   
    char e1 = '中', e2 = '国';  
    byte f= (byte) '中'; //必须强制转换否则报错  
    System.out.println(e1);  
    System.out.println(e2);  
    System.out.println(f);  
    结果为:
    45
    3、char、byte、int对于英文字符,可以相互转化,如:
    [html]  www.2cto.com
    byte g = 'b'; //b对应ASCII是98  
            char h = (char) g;  
    char i = 85;    //U对应ASCII是85  
    int j = 'h';    //h对应ASCII是104  
    System.out.println(g);  
    System.out.println(h);  
    System.out.println(i);  
    System.out.println(j); 
    结果为:
    98
    b
    U
    104
  • 相关阅读:
    51nod 1428 活动安排问题
    COGS 1. 加法问题 (水体日常)
    COGS 1406. 邻居年龄排序[Age Sort,UVa 11462](水题日常)
    51nod 1133 不重叠的线段
    51nod 1031 骨牌覆盖
    51nod 1050 循环数组最大子段和
    51nod 1094 和为k的连续区间
    51nod 1433 0和5
    51nod 1092 回文字符串
    洛谷 P1507 NASA的食物计划
  • 原文地址:https://www.cnblogs.com/Robotke1/p/3099305.html
Copyright © 2011-2022 走看看