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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM