1. 程式人生 > >java中long(Long)與int(Integer)之間的轉換

java中long(Long)與int(Integer)之間的轉換

示例程式碼:

public static void main(String[] args) {
		// 1、將long型轉化為int型,其中int、long是基礎型別
		long a = 10;
		int b = (int) a;
		System.out.println("1、將long型轉化為int型:" + b);

		// 2、將int型轉化為long型,其中int、long都是基礎型別
		int a1 = 10;
		long b1 = a1;
		System.out.println("2、將int型轉化為long型:" + b1);

		// 3、將Long型轉換為int型的,其中Long型是包裝型別
		Long a2 = 10l;
		int b2 = a2.intValue();
		System.out.println("3、將Long型轉換為int型:" + b2);
		
		//4、將Integer型轉化為long型,其中Integer型是包裝型別,long型是基礎型別
		Integer a3=10;
		long b3=a3.longValue();
		System.out.println("4、將Integer型轉化為long型:"+b3);
		
		//5、將Integer型轉化為Long型,其中Integer、Long型都是包裝型別
		Integer a4=10;
		Long b4=a4.longValue();
		System.out.println("5、將Integer型轉化為Long型:"+b4);
	}

輸出結果:

1、將long型轉化為int型:10 2、將int型轉化為long型:10 3、將Long型轉換為int型:10 4、將Integer型轉化為long型:10 5、將Integer型轉化為Long型:10