複習筆記2 變量 類型轉換 自動升位

public class Test3
{
	public static void main(String[] args)
	{
		//定義變量,基本類型的取值範圍和位長度
		byte b = 0;
		System.out.println((b = Byte.MAX_VALUE));
		System.out.println((b = Byte.MIN_VALUE));
		System.out.println(Byte.SIZE);
		System.out.println("===================================>");
		
		short s = 0;
		System.out.println((s = Short.MAX_VALUE));
		System.out.println((s = Short.MIN_VALUE));
		System.out.println(Short.SIZE);
		System.out.println("===================================>");
		
		int i = 0;
		System.out.println((i = Integer.MAX_VALUE));
		System.out.println((i = Integer.MIN_VALUE));
		System.out.println(Integer.SIZE);
		System.out.println("===================================>");
		
		long l = 0l;
		System.out.println((l = Long.MAX_VALUE));
		System.out.println((l = Long.MIN_VALUE));
		System.out.println(Long.SIZE);
		System.out.println("===================================>");
		
		float f = 0.0f;
		System.out.println((f = Float.MAX_VALUE));
		System.out.println((f = Float.MIN_VALUE));
		System.out.println(Float.SIZE);
		System.out.println("===================================>");
		
		double d = 0.0;
		System.out.println((d = Double.MAX_VALUE));
		System.out.println((d = Double.MIN_NORMAL));
		System.out.println(Double.SIZE);
		System.out.println("===================================>");
		
		boolean bool = false;
		System.out.println((bool = Boolean.FALSE));
		System.out.println((bool = Boolean.TRUE));
		System.out.println("===================================>");
		
		char c = ' ';
		System.out.println((c = Character.MAX_VALUE));
		System.out.println((c = Character.MIN_VALUE));
		System.out.println(Character.SIZE);
		System.out.println("===================================>");
		
		//低精度向高精度賦值不需要強制轉換
		//java會自動升位
		l = b;
		l = s;
		l = i;
		l = c;
		d = f;
		
		//高精度向低精度賦值會降低精度,so
		//需要進行強制類型轉換,僅取按照左邊
		//類型的長度取右邊類型的低位,高位被拋棄
		b = (byte) s;
		b = (byte) i;
		b = (byte) l;
		b = (byte) c;
		f = (float) d;
		
		//對於boolean,在java中,單個bool變量以
		//int存儲,而bool數組則是以byte數組存儲
		//對於這些數值,非0爲真,0爲假,最典型的例子
		//在DataOutputStream.readBoolean()方法
		bool = b != 0;
		bool = s != 0;
		bool = i != 0;
		bool = l != 0;
		bool = f != 0;
		bool = d != 0;
		bool = c != 0;
		
		//基本類型轉換成字節數組,在IO操作中
		//經常要進行這種轉換
		System.out.print("short");printByteArray(toByteArray(s = 0x0201));
		System.out.print("int  ");printByteArray(toByteArray(i = 0x04030201));
		System.out.print("long ");printByteArray(toByteArray(l = 0x0807060504030201l));
		System.out.print("char ");printByteArray(toByteArray(c = 0x0201));
		System.out.print("true ");printByteArray(toByteArray(bool = true));
		System.out.print("false");printByteArray(toByteArray(!bool));
		System.out.println("===================================>");		
		
		//這裏因爲表達式升位爲int,所以
		//需要強制轉換
		b = 20;
		s = (short)(b * 20);
		System.out.println(s);
		System.out.println("===================================>");
		
		//int的最大值*2之後應該是一個33位的數
		//但並沒有進行升位到long,所以java的
		//默認升位範圍在int,即便表達式超出
		//int範圍,這樣會損失精度
		i = Integer.MAX_VALUE;
		i = i * 2;
		System.out.println(i);
		System.out.println("===================================>");
		
		//這裏並沒有如我們所想的提升到long,而
		//是仍然爲int,最後賦值時才提升到long
		//所以處理這個問題需要強制轉換一個元素
		//爲long,使其他的元素都升位到long
		i = Integer.MAX_VALUE;
		l = i + i;
		System.out.println(l);
		l = (long)i + i;
		System.out.println(l);
		//這裏看到,只需要有一個爲long,不需要
		//規定哪一個
		l = i + (long)i;
		System.out.println(l);
		//這裏證明上邊的理論是錯的,事實證明表達式
		//由左向右開始,而類型升位也是,所以爲了保證
		//最後的精度無損,應該考慮在表達式第一個元素
		//進行最大升位
		l = i + i +(long)i;
		System.out.println(l);
		l = (long)i + i + i;
		System.out.println(l);
		System.out.println("===================================>");
		
		//這兩個看起來沒有變化,因爲在java裏,double的
		//升位級別是最大的,所以沒有問題,換句話說,java
		//默認的浮點數就是double
		b = Byte.MAX_VALUE;
		s = Short.MAX_VALUE;
		i = Integer.MAX_VALUE;
		l = Integer.MAX_VALUE;
		c = Short.MAX_VALUE;
		f = Float.MAX_VALUE;
		d = b + s + c + i + l + f;
		System.out.println(d);
		d = 0.0 + b + s + c + i + l + f;
		System.out.println(d);
		System.out.println("===================================>");
		
		//看起來沒有問題,1100是正確的,但是如果數組是
		//以false開始的呢,高位的0如何判斷?所以這個還
		//需要一個記錄有效位數的變量,也就是數組的長度
		//具體可以參考BitSet類
		boolean[] boola = {true,true,false,false};
		System.out.println(Long.toBinaryString(booleanArrayToNumber(boola)));
		System.out.println("===================================>");
		
		//結果爲1,原因前邊有說到,這裏的問題是,還要
		//強制轉換1爲long麼,有沒有更簡單的辦法?
		l = 1 << 32;
		System.out.println(l);
		//java整數後加l表示爲long類型
		l = 1l << 32;
		System.out.println(l);
		//爲什麼必須加f?因爲默認爲double,需要加f聲明
		//爲float類型,這在表達式中會影響精度
		f = 1.0f;
	}
	
	/**
	 * 將一個short拆分成字節數組
	 * @param s
	 * @return
	 */
	public static final byte[] toByteArray(short s)
	{
		return new byte[]{(byte)((s & 0xff00) >> 8), (byte)(s & 0x00ff)};
	}
	
	/**
	 * 將一個int拆分成字節數組
	 * @param s
	 * @return
	 */
	public static final byte[] toByteArray(int i)
	{
		return new byte[]{(byte)((i & 0xff000000) >> 24), (byte)((i & 0xff0000) >> 16), (byte)((i & 0xff00) >> 8), (byte)(i & 0x00ff)};
	}
	
	/**
	 * 將一個long拆分成字節數組
	 * @param s
	 * @return
	 */
	public static final byte[] toByteArray(long l)
	{
		return new byte[]{(byte)((l & 0xff00000000000000l) >> 56), (byte)((l & 0xff000000000000l) >> 48), (byte)((l & 0xff0000000000l) >> 40), (byte)((l & 0xff00000000l) >> 32),
				(byte)((l & 0xff000000) >> 24), (byte)((l & 0xff0000) >> 16), (byte)((l & 0xff00) >> 8), (byte)(l & 0x00ff)};
	}
	
	/**
	 * 將一個unicode字符拆分成字節數組
	 * @param c
	 * @return
	 */
	public static final byte[] toByteArray(char c)
	{
		return new byte[]{(byte)((c & 0xff00) >> 8), (byte)(c & 0x00ff)};
	}
	
	/**
	 * 將bool轉換成字節
	 * 或者考慮多個bool按位存在一個整數裏邊
	 * @param bool
	 * @return
	 */
	public static final byte[] toByteArray(boolean bool)
	{
		return new byte[]{bool ? (byte)1 : (byte)0};
	}
	
	/**
	 * 打印一個byte數組
	 * @param b
	 */
	public static final void printByteArray(byte[] b)
	{
		StringBuilder sb = new StringBuilder();
		sb.append('[');
		byte index = 0;
		do
        {
			sb.append(b[index]);
	        if(index != b.length - 1)
	        {
	        	sb.append(',');
	        }
        }
        while (++index != b.length);
		sb.append(']');
		System.out.println(sb.toString());
	}
	/**
	 * 按位存儲布爾數組
	 * @param b
	 * @return
	 */
	public static final long booleanArrayToNumber(boolean[] b)
	{
		long l = 0;
		int bounds = b.length - 1;
		int shift = bounds;
		int index = 0;
		while (index < bounds)
        {
	        l |= (long)(b[index++] ? 1 : 0) << shift--;
        }
        return l;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章