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
posted @ 2014-04-17 16:25  马七风  阅读(59610)  评论(2编辑  收藏  举报