黑马程序员技术交流社区

标题: 关于char所占字节. [打印本页]

作者: 张智安    时间: 2013-10-25 11:22
标题: 关于char所占字节.
本帖最后由 仇邓强 于 2013-10-27 15:58 编辑

纳闷了好久 - -!

作者: 张智安    时间: 2013-10-25 13:02
有没有牛一点的回答呀
作者: 1961993790    时间: 2013-10-25 13:06
一定是2个字节.
作者: 张智安    时间: 2013-10-25 13:11
大家要有发散性的思维呀{:soso_e120:}
作者: 1961993790    时间: 2013-10-25 13:14
张智安 发表于 2013-10-25 13:02
有没有牛一点的回答呀

我就是说的java中,咱们是java技术论坛所以当然说的在java中占2个字节了。
作者: hurryup    时间: 2013-10-25 13:46
必须2字节
作者: ☆╰學不会☆╮    时间: 2013-10-25 16:49
java中两个字节 C中占一个字节
char aa;   可以定义世界上任何一个字符
如果是汉字 正好占用俩字节 字母虽然占一个字节 貌似还会空余一个字节。
作者: 枫儿    时间: 2013-10-25 17:08
两个字节空间
作者: 流行语    时间: 2013-10-25 18:23
java中的char占用两个字节,因为java中用unicode编码。可以这样玩一下:
  1. class Test{
  2.     public static void main(String args[]) {
  3.             char c = '中';
  4.             System.out.println(c);
  5.         }
  6. }
复制代码
结果:中
GBK也是占用两个字节
作者: 张智安    时间: 2013-10-25 19:31
流行语 发表于 2013-10-25 18:23
java中的char占用两个字节,因为java中用unicode编码。可以这样玩一下:结果:中
GBK也是占用两个字节 ...

用"  ----双引吧?
作者: 月夜之鬼魅    时间: 2013-10-25 19:41
你无法知道char底层究竟占了多少字节,可能是32位,也可能是64位,128位
java值规定了每个类型的值域,也就是取值范围,char的取值范围用两个字节的补码来表示,最高位不为符号位

实际上,在jvm一层,通常是用int来表示byte,short,char,boolean类型的,而int究竟占多少位,jvm规范是没有规定的,爱怎么实现就可以怎么实现,但int的值域必须是4个字节的补码所表示的范围
作者: 小夕    时间: 2013-10-26 07:30
2个字节16位
作者: 麦子    时间: 2013-10-26 10:23
让java告诉你吧,有代码有真相,呵呵
class Demo
{
        public static void main(String[] args)
        {
               System.out.println("char占用" + Character.SIZE/8+"个字节");
                           System.out.println("int占用" + Integer.SIZE/8+"个字节");
                           System.out.println("short占用" + Short.SIZE/8+"个字节");
                           System.out.println("long占用" + Long.SIZE/8+"个字节");
                           System.out.println("byte占用" + Byte.SIZE/8+"个字节");
                           System.out.println("float占用" + Float.SIZE/8+"个字节");
                           System.out.println("double占用" + Double.SIZE/8+"个字节");
        }
}


---------- java ----------
char占用2个字节
int占用4个字节
short占用2个字节
long占用8个字节
byte占用1个字节
float占用4个字节
double占用8个字节

输出完成 (耗时 0 秒) - 正常终止

作者: 起猿    时间: 2013-10-26 14:13

1:“字节”是byte,“位”是bit ;

  2: 1 byte = 8 bit ;

  char 在java中是2个字节。java采用unicode,2个字节(16位)来表示一个字符。

可以用下面的代码来分析:

  1. public class Test {


  2.         public static void main(String[] args) {
  3.                 String str= "中";
  4.                 char x ='中';
  5.                 byte[] bytes=null;
  6.                 byte[] bytes1=null;
  7.                 try {
  8.                         bytes = str.getBytes("utf-8");
  9.                         bytes1 = charToByte(x);
  10.                 } catch (UnsupportedEncodingException e) {
  11.                         // TODO Auto-generated catch block
  12.                         e.printStackTrace();
  13.                 }
  14.                 System.out.println("bytes 大小:"+bytes.length);
  15.                 System.out.println("bytes1大小:"+bytes1.length);
  16.         }
  17.         public static byte[] charToByte(char c) {
  18.         byte[] b = new byte[2];
  19.         b[0] = (byte) ((c & 0xFF00) >> 8);
  20.         b[1] = (byte) (c & 0xFF);
  21.         return b;
  22.     }
  23. }
复制代码

运行结果:

bytes 大小:3
bytes1大小:2


java是用unicode来表示字符,"中"这个中文字符的unicode就是2个字节。

String.getBytes(encoding)方法是获取指定编码的byte数组表示,

通常gbk/gb2312是2个字节,utf-8是3个字节

如果不指定encoding则取系统默认的encoding。







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2