zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Netty ByteBuf(图解之 2)| 秒懂
    【转】 RGB各种格式
    缺少动态库报错
    bug:进程可调用函数而子线程调用报错
    【转】 pthread设置线程的调度策略和优先级
    【转】 C++析构函数的作用和用法
    【转】 g++编译时对'xxxx'未定义的引用问题(undefined reference to)
    【转】 C语言文件操作详解
    【转】 H.264编码原理以及I帧B帧P帧
    【转】 strrchr()函数---C语言
  • 原文地址:https://www.cnblogs.com/SevenwindMa/p/3671057.html
Copyright © 2011-2022 走看看