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
  • 相关阅读:
    fastDFS同步问题讨论
    Android开发(26)--补间动画(Tween)的实现
    android布局
    Linux特殊权限:SUID、SGID、SBIT
    如何使用ssh-keygen生成key
    Linux中环境变量到底写在哪个文件中?解析login shell 和 no-login shell
    使用ssh无密码登录
    github中的ssh配置
    PHP中的一个很好用的文件上传类
    [置顶] js模板方法的思路及实现
  • 原文地址:https://www.cnblogs.com/SevenwindMa/p/3671057.html
Copyright © 2011-2022 走看看