JAVA基础(35)---基本类型的包装类

版权声明:如需转载请标明出处 https://blog.csdn.net/yj201711/article/details/83901068

基本类型的包装类

基本数据类型: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);

	}
}

猜你喜欢

转载自blog.csdn.net/yj201711/article/details/83901068