java byte byte_java byte 和Byte

java的基本数据类型中有byte这种,byte存储整型数据,占据1个字节(8 bits),能够存储的数据范围是-128~+127。

Byte是java.lang中的一个类,目的是为基本数据类型byte进行封装。封装有几种好处,比如:1. Byte可以将对象的引用传递,使得多个function共同操作一个byte类型的数据,而byte基本数据类型是赋值之后要在stack(栈区域)进行存储的;2. 定义了和String之间互相转化的方法。Byte的大小是8个字节。因为Byte是需要通过关键字new来申请对象,而此部分申请出来的对象放在内存的heap(堆区域)。

package BasicKnowledge.Testlang;

public class ByteTest {

public static void main(String args[])

{

Byte by1 = new Byte("123");

Byte by2 = new Byte("123");

int length = by1.SIZE;

int max = by2.MAX_VALUE;

int min = by2.MIN_VALUE;

if(by1 == by2)

{

System.out.println("Operation '=' compares the reference of Byte objects and equal");

}else System.out.println("Operation '=' compares the objects of Byte objects and not equal");

if(by1.equals(by2))

{

System.out.println("Function 'equals()' compares the value of Byte objects and equal");

}else System.out.println("Function 'equals()' compares the value of Byte objects and not equal");

Byte by3 = by1;

if(by3 == by1)

{

System.out.println("Operation '=' compares the reference of Byte objects and equal");

}else System.out.println("Operation '=' compares the reference of Byte objects and not equal");

System.out.println(by1);

System.out.println(by2);

System.out.println("The length is "+length);

System.out.println("MAX:"+max+" MIN"+min);

byte temp = (byte)241; // 241的二进制表示为11110001(补码),其中第一位为符号位,那么剩余的计算结果为15,最终结果为-15

System.out.println(temp);

}

}

输出结果:

Operation '=' compares the objects of Byte objects and not equal

Function 'equals()' compares the value of Byte objects and equal

Operation '=' compares the reference of Byte objects and equal

123

123

The length is 8

MAX:127 MIN-128

-15

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值