1. 程式人生 > >JAVA基礎(35)---基本型別的包裝類

JAVA基礎(35)---基本型別的包裝類

基本型別的包裝類

基本資料型別:byte short int  long  char  boolean  float  double

為了對基本基本資料型別操作方便,引入基本資料型別的包裝類

                                                                     byte -> Byte;

                                                                     short -> Short;

                                                                     int –> Integer;

                                                                     long -> Long;

                                                                     float -> Float;

                                                                     double -> double;

                                                                     char -> Character;

                                                                     boolean - > Boolean ;

以int -> Integer為例

Integer 中提供了兩個常量表示int型別的最大值和最小值,分別是:
                                                                 public static final int MAX_VALUE;
                                                                 public static final int MIN_VALUE。

 

將十進位制轉換為其他進位制的字串形式

常用的有將十進位制轉換為二進位制、八進位制和十六進位制的字串。分別用以下方法:

                                                  (1)public static String toBinaryString(int i):轉換為二進位制
                                                  (2)public static String toOctalString(int i):八進位制
                                                  (3)public static String toHexString(int i):十六進位制。

int和String相互轉換

                                                  (1)int轉換成String
                                                                    使用String.valueOf()方法:String str = String.valueOf(100);
                                                                    使用Integer.toString()方法:String str = Integer.toString(100);
                                                  (2)String轉換成int
                                                                     使用Integer.parseInt()方法:int i = Integer.parseInt(“100”);先把String轉成Integer,

                                                                     再用Integer.intValue方法:
                                                                                                  Integer integer = new Integer(“100”);
                                                                                                                    int i = integer.intValue();

自動裝箱和自動拆箱(jdk5的新特性)

自動裝箱:可以將一個基本資料型別直接賦值給他的包裝類
自動拆箱:將基本數型別的包裝類的物件,可以直接賦值給一個基本資料型別

public class IntegerDemo{
	public static  void  main(String[] args){
		
                //獲取int型別表示的資料的最大值和最小值
		int maxValue = Integer.MAX_VALUE;
		System.out.println(maxValue);
		int minValue = Integer.MIN_VALUE;
		System.out.println(minValue);
		
                //建立Integer物件
		Integer  inte1 = new Integer(1);
		System.out.println(inte1);
		Integer inte2 = new Integer("1");
		System.out.println(inte2);
		
                //整數的其他進位制之間的轉換
		String intBin = Integer.toBinaryString(10);
		System.out.println(intBin);
		String intOcx = Integer.toOctalString(10);
		System.out.println(intOcx);
		String intHex = Integer.toHexString(10);
		System.out.println(intHex);
		
                //int和String型別之間的相互轉換
		Integer inte3 = new Integer("10");//將String 轉換為int
		String str = String.valueOf(100);//將int 轉換為String
		System.out.println(str);
		String str1 = Integer.toString(100);
		System.out.println(str1);
		System.out.println("---------------------------");
		
                //自動裝箱和自動拆箱
                Integer inte4 = 100;
		System.out.println(inte4);
		int i = inte4;
		System.out.println(i);

	}
}