Java的基本类型包装

由于基本数值类型如int、long等的变量不能直接调用方法。因此,Java另外为基本类型定义了对应的包装类型,通过包装变量直接调用相关的算术方法。

1.数值类型包装

首先,包装类型与基本类型一样,先要声明包装变量,然后对该变量赋值。给包装变量赋值有以下3种方式:

  • 直接通过等号把具体数字赋值给包装变量
    Integer oneInteger = 1;

  • 调用Integer的valueOf方法完成指定数字的赋值
    Integer oneInteger = Integer.valueOf(1);

  • 使用new关键字创建新的包装变量
    Integer oneInteger = new Integer(1);

想一想,既然可以把基本类型的变量直接赋值给包装变量,那可不可以直接把包装变量赋值给基本类型变量呢?固然是不允许的,只能通过指定方法获得对应的基本变量数值,有byteValue()shortValue()longValue()floatValue()doubleValue()

【代码】

		......
		// 数值包装类型下面以Integer举例,其它包装类型的用法同Integer,包括Byte、Short、Long、Float、Double
		// 初始化包装变量的第一种方式:直接用等号赋值
		Integer oneInteger = 1;
		
		// 初始化包装变量的第二种方式:调用包装类型的valueOf方法
		//Integer oneInteger = Integer.valueOf(1);
		
		// 初始化包装变量的第三种方式:使用关键字new创建新变量
		//Integer oneInteger = new Integer(1);
		
		System.out.println("oneInteger = " + oneInteger);
		
		byte oneByte = oneInteger.byteValue();// 把包装变量转换成字节变量
		System.out.println("oneByte = " + oneByte);
		
		short oneShort = oneInteger.shortValue();// 把包装变量转换成短整变量
		System.out.println("oneShort = " + oneShort);
		
		int oneInt = oneInteger.intValue(); // 把包装变量转换成整型变量
		System.out.println("oneInt="+oneInt);
		
		long oneLong = oneInteger.longValue(); // 把包装变量转换成长整变量
		System.out.println("oneLong="+oneLong);
		
		float oneFloat = oneInteger.floatValue(); // 把包装变量转换成浮点变量
		System.out.println("oneFloat="+oneFloat);
		
		double oneDouble = oneInteger.doubleValue(); // 把包装变量转换成双精度变量
		System.out.println("oneDouble="+oneDouble);

2.布尔类型包装

布尔类型的初始化方式与上面数值类型包装一样,只不过它定义的是true和false的布尔值罢了。

要把布尔包装变量转换成基本类型的布尔变量,同样不能通过强制转换,而必须通过包装变量的 booleanValue()

【代码】

		......
		// 把包装变量转换成布尔变量,需要调用包装变量的booleanValue方法
		Boolean boolPack = Boolean.valueOf(true);
		boolean bool = boolPack.booleanValue();
		System.out.println("bool="+bool);

与数值包装类型保持一致的还有equals方法,该方法相当于关系运算符“==”,可用于判断两个布尔包装变量是否相等。

【代码】

		......
		// 包装变量的equals方法相当于关系运算符“==”
		boolean equalResult = boolPack.equals(false); 
		System.out.println("equalResult="+equalResult);

除此之外,布尔类型变量 不仅仅使用逻辑运算符,包装类型 提供了几个逻辑方法,如:

  • logicalAnd方法相当于逻辑“与”运算符“&”;
  • logicalOr方法相当于逻辑“或”运算符“|”;
  • logicalXor方法相当于逻辑“异或”运算符“^”;

【代码】

			......
			// 布尔的包装变量允许直接使用逻辑运算符“!”“&”“|”“^”
			Boolean boolZhen = true;
			Boolean boolJia = true;
			Boolean not = !boolZhen;
			System.out.println("not="+not);
			Boolean and = boolZhen & boolJia;
			System.out.println("and="+and);
			Boolean or = boolZhen | boolJia;
			System.out.println("or="+or);
			Boolean xor = boolZhen ^ boolJia;
			System.out.println("xor="+xor);
			boolean equalResult = boolPack.equals(false); // 包装变量的equals方法相当于关系运算符“==”
			System.out.println("equalResult="+equalResult);
				
			boolean a = true, b = false;
			// 布尔包装类型的logicalAnd方法相当于逻辑“与”运算符“&”
			boolean andResult = Boolean.logicalAnd(a, b);
			System.out.println("andResult="+andResult);
			// 布尔包装类型的logicalOr方法相当于逻辑“或”运算符“|”
			boolean orResult = Boolean.logicalOr(a, b);
			System.out.println("orResult="+orResult);
			// 布尔包装类型的logicalXor方法相当于逻辑“异或”运算符“^”
			boolean xorResult = Boolean.logicalXor(a, b);
			System.out.println("xorResult="+xorResult);

3.字符包装类型

字符类型的初始化方式与上面数值类型包装一样,直接用等号赋值、调用包装类型的valueOf方法、使用关键字new创建新变量。

倘若要把字符包装变量转换成字符变量,则调用包装变量的charValue()即可。

【代码】

		......
		Character character = 'A'; // 声明一个包装字符变量
		System.out.println("character=" + character);
		char value = character.charValue(); // 把包装字符变量转换成基本字符变量
		System.out.println("value=" + value);
		// Character类型与char类型的变量之间允许直接赋值,靠的是“自动装箱”和“自动拆箱”
		Character plusResult = (char) (character + 1);
		System.out.println("plusResult=" + plusResult);

上面提到的“自动装箱” 和 “自动拆箱”解释:

  • “自动装箱”:编译器会默认调用valueOf方法,将基本类型的变量转换成对应的包装类型变量。
  • “自动拆箱”:编译器会默认调用***Value方法,将包装类型的变量转换成对应基本类型的变量。

除了以上所有介绍的基本方法外,Character类型常用的字符处理方法:

  • isDigit方法判断字符是否为数字

  • isLetter方法判断字符是否为字母

  • isLowerCase方法判断字符是否为小写

  • isUpperCase方法判断字符是否为大写

  • isSpaceChar方法判断字符是否为空格

  • isWhitespace方法判断字符是否为空白(非数字非字母非标点)

  • toLowerCase方法把字符转换为大写

  • toUpperCase方法把字符转换为小写

    以上处理方法均需按照“ Character.方法名称(输入字符) ” 。

【代码】

		......
		Character letter = 'A'; // 声明一个包装字符变量
		// 下面是Character常用的字符处理方法
		boolean isDigit = Character.isDigit(letter); // isDigit方法判断字符是否为数字
		System.out.println("isDigit=" + isDigit);
		boolean isLetter = Character.isLetter(letter); // isLetter方法判断字符是否为字母
		System.out.println("isLetter=" + isLetter);
		boolean isLowerCase = Character.isLowerCase(letter); // isLowerCase方法判断字符是否为小写
		System.out.println("isLowerCase=" + isLowerCase);
		boolean isUpperCase = Character.isUpperCase(letter); // isUpperCase方法判断字符是否为大写
		System.out.println("isUpperCase=" + isUpperCase);
		Character line = '\n'; // 声明一个包装字符变量
		boolean isSpaceChar = Character.isSpaceChar(line); // isSpaceChar方法判断字符是否为空格
		System.out.println("isSpaceChar=" + isSpaceChar);
		// isWhitespace方法判断字符是否为空白(非数字非字母非标点,包括空格、制表、回车、换行等)
		boolean isWhitespace = Character.isWhitespace(line);
		System.out.println("isWhitespace=" + isWhitespace);
		char lowerCase = Character.toLowerCase(letter); // toLowerCase方法把字符转换为大写
		System.out.println("lowerCase=" + lowerCase);
		char upperCase = Character.toUpperCase(letter); // toUpperCase方法把字符转换为小写
		System.out.println("upperCase=" + upperCase);

总之,不管是数值类型包装变量、布尔类型包装变量还是字符包装类型变量,想要转化换成基本类型变量,都不能直接强制转换,必须通过 xxxValue()

猜你喜欢

转载自blog.csdn.net/weixin_46312449/article/details/110499868