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
  • 相关阅读:
    【题解】【bzoj1819】【JSOI】Word Query电子字典
    【笔记】好背的KMP
    【题解】【bzoj 1503】【NOI2004】郁闷的出纳员
    【题解】【bzoj 2809】【Apio2012】dispatching
    CSP2019游记
    Spring boot starter pom的依赖关系说明
    Mybatis的分页插件PageHelp:Page对象中的pageSize等属性无法序列化,无法转换为json字符串
    Java Util
    实现Quartz的动态增删改查
    1. Spring boot 之热部署
  • 原文地址:https://www.cnblogs.com/Robotke1/p/3099305.html
Copyright © 2011-2022 走看看