byte,char,int,Java - 位表示 [英] byte, char, int in Java - bit representation

查看:164
本文介绍了byte,char,int,Java - 位表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java中的byte和char感到困惑。我有以下程序段:

I am confused with byte and char in Java. I have the following program segment :

    byte b = 65;
    char c = 65;
    short s = 65;
    System.out.println("b:"+b+" c:"+c+ " s:"+s);  // outputs b:65 c:A s:65

b,c和s的位表示是相同的,他们都是:: 00000000 01000001。
我的问题是如果位是相同的,那么它们如何不同行为 - b作为字节,c作为char和s作为短?
另一个问题是: char c = 65; 为什么这是一个正确的语句?

The bit representation of b, c and s are same they all are :: 00000000 01000001 . My question is if the bits are same then how they act differently - b as byte, c as char and s as short ? Another question is : char c = 65; why this is a correct statement ? it does not give error though I am assigning int value to a char.

感谢。

推荐答案

1)它们的位表示不一样,它的 01000001 表示字节, 0000000001000001 short和char。

1) their bit representation is not the same, it's 01000001 for byte and 0000000001000001 for short and char.

2)区别在于byte和short是有符号整数,char是一个16位的Unicode字符

2) the difference is that byte and short are signed integers and char is a 16-bit Unicode character

3)65不是int,它是一个常量,如果你尝试分配一个int你会得到一个错误

3) 65 is not int, it's a constant, if you try to assign an int you will get an error

   int x = 65;
   char c = x;   <-- error

这篇关于byte,char,int,Java - 位表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆