包装类

包装类

包装类的引入
1、如何判断一个字符是大写,小写还是数字字符?
c >= ‘a’ && c <= ‘z’
该判断是与字符相关,应该放在字符类里面 Character char
2、如何计算65的二进制,八进制,十六进制呢? – 那么27进制

该计算与整数有关,应该放在整数类里面 Integer Byte Short Long
有了包装类,我们就可以访问和数据类型相关的对象的更多的方法
基本数据类型是没有方法可以调用的

任何的数据类型都有一个class属性,它返回的是类文件
包装类的概念:
八大基本数据类型都对应着一个包装类类型
byte short int long float double char boolean
Byte Short Integer Long Float Double Character Boolean
这里整理的是Integer类和Character类,其他是一样的
Integer是java.lang.Number 包下面的
任何 Number类下的子类都可以转换成其他的数值类型,但是可能会出现精度丢失
static int BYTES
用于表示二进制补码二进制形式的 int值的字节数。
static int MAX_VALUE
一个持有最大值一个 int可以有2^31 -1。
static int MIN_VALUE
的常量保持的最小值的 int可以具有,-2^31。
static int SIZE
用于表示二进制补码二进制形式的 int值的位数。
static 类 TYPE
类原始类型 int的 类实例。
Integer(int value)
构造一个新分配的 Integer对象,该对象表示指定的 int值。
Integer(String s)
构造一个新分配 Integer对象,表示 int由指示值 String参数。
NumberFormatException
异常名称: 数字格式化异常
产生原因: 字符串未满足数字的格式
举例代码如下

public class IntegerDemo01 {
	public static void main(String[] args) {
		System.out.println(Integer.BYTES);
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Integer.MIN_VALUE);
		System.out.println(Integer.SIZE);
		// static 类<Integer> TYPE 
		Class<Integer> c = Integer.TYPE;
		Class<Integer> c2 = int.class;
		System.out.println(c == c2);
		
		Integer ii = new Integer(100);
		System.out.println(ii);
		
//		Integer ii2 = new Integer("100a"); // NaN
//		System.out.println(ii2);
		
		Number number = new Integer(200);
		System.out.println(number);
	}
}

Integer包装类

构造方法
public Integer(int value)
public Integer(String s)
成员方法
public int intValue() 拆箱,将Integer转化为整型
public static int parseInt(String s) 将字符串转换为int类型
public static String toString(int i) 将整型转换为字符串
public static Integer valueOf(int i) 将整型装换为Integer类型
public static Integer valueOf(String s) 将字符串转换为Integer类型
举例代码如下

public class IntegerDemo02 {
	public static void main(String[] args) {
		Integer ii = new Integer(100);
		byte byteValue = ii.byteValue();
		short shortValue = ii.shortValue();
		int intValue = ii.intValue();
		long longValue = ii.longValue();
		float floatValue = ii.floatValue();
		double doubleValue = ii.doubleValue();
		System.out.println(byteValue);
		System.out.println(shortValue);
		System.out.println(intValue);
		System.out.println(longValue);
		System.out.println(floatValue);
		System.out.println(doubleValue);
		
		int i = Integer.parseInt("200");
		System.out.println(i);
		
		String s = Integer.toString(20);
		System.out.println(s);
		
		Integer ii2 = Integer.valueOf(100);
		System.out.println(ii2);
		
		Integer ii3 = Integer.valueOf("500");
		System.out.println(ii3);
	}
}

举例代码2如下所示

public class IntegerDemo06 {
	public static void main(String[] args) {
		Integer i01 = 59; 
		int i02 = 59;
		Integer i03 = Integer.valueOf(59);
		Integer i04 = new Integer(59);
		
		System.out.println("i01 == i02:" + (i01 == i02)); // true i01.intValue == i02
	    System.out.println("i01 == i03:" + (i01 == i03)); // true
	    System.out.println("i03 == i04:" + (i03 == i04)); // false
	    System.out.println("i02 == i04:" + (i02 == i04)); // true i02 == i04.intValue
	    
	    Integer i = 130; // Integer i = Integer.valueOf(130);
	    Integer ii = Integer.valueOf(130);
	    System.out.println(i == ii); // false
	}
}
/*		反编译内容
		Integer i01 = Integer.valueOf(59);
		int i02 = 59;
		Integer i03 = Integer.valueOf(59);
		Integer i04 = new Integer(59);
		System.out.println((new StringBuilder("i01 == i02:")).append(i01.intValue() == i02).toString());
		System.out.println((new StringBuilder("i01 == i03:")).append(i01 == i03).toString());
		System.out.println((new StringBuilder("i03 == i04:")).append(i03 == i04).toString());
		System.out.println((new StringBuilder("i02 == i04:")).append(i02 == i04.intValue()).toString());
		Integer i = Integer.valueOf(130);
		Integer ii = Integer.valueOf(130);
		System.out.println(i == ii);
*/

Integer类实现进制转换

进制转换的分类:
1.其他进制->十进制 按权展开
2.十进制->其他进制 取余法
3.X进制->Y进制 先将X进制转换成十进制,再将十进制转换成Y进制

快速转换法
4.二进制,八进制,十六进制快速转换法 分组法
5.十进制和二进制的快速转换法 8421码

计算机只会两种方式
1.其他进制->十进制 按权展开
2.十进制->其他进制 取余法
十进制到其他进制
public static String toBinaryString(int i)
public static String toOctalString(int i)
public static String toHexString(int i)
public static String toString(int i,int radix)
其他进制到十进制
public static int parseInt(String s,int radix)

计算机只能够表示到36进制(0-9,a-z,10+26)

举例代码如下

public class IntegerDemo05 {
	public static void main(String[] args) {
		System.out.println(Integer.toBinaryString(100));
		System.out.println(Integer.toOctalString(100));
		System.out.println(Integer.toHexString(100));
		System.out.println(Integer.toString(100));
		
		System.out.println(Integer.toString(100, 36)); // 2s
		
		System.out.println(Integer.parseInt("100101", 2));
		System.out.println(Integer.parseInt("7", 8));
		System.out.println(Integer.parseInt("a", 16));
		System.out.println(Integer.parseInt("z", 36));
		
		System.out.println(getNum(16, 2, "ff"));
	}
	
	// 书写一个方法实现任意进制的转换
	public static int getNum(int xRadix, int yRadix, int num) {
		// 将X进制的num转换成十进制
		int i = Integer.parseInt(String.valueOf(num), xRadix);
		// 将十进制的i转换成对应Y进制
		String s = Integer.toString(i, yRadix);
		return Integer.parseInt(s);
	}
	
	public static String getNum(int xRadix, int yRadix, String num) {
		// 将X进制的num转换成十进制
		int i = Integer.parseInt(num, xRadix);
		// 将十进制的i转换成对应Y进制
		String s = Integer.toString(i, yRadix);
		return s;
	}
}

实现String和int之间的相互转换
parse 解析 将字符串中的数据提取出来存储对应的实体类中
1.日期解析
2.xml解析
3.json解析
4.html解析
format 格式化
将实体类中的数据转换成字符串
举例代码如下

public class IntegerDemo03 {
	public static void main(String[] args) {
		// int -> String
		// 方式一:
		int i = 10;
		String s = i + ""; // new StringBuilder().append(i);
		
		// 方法二:
		String s2 = String.valueOf(i); // 常用
		
		// 方式三:
		String s3 = Integer.valueOf(i).toString();
		
		// String --> int
		// 方式一:
		String str = "100";
		int ii = Integer.parseInt(str); // 最常用
		
		// 方式二:
		int ii2 = new Integer(str).intValue();
		
		// 方式三:
		int ii3 = Integer.valueOf(str).intValue();
		
	}
}

在JDK1.5以后,引入自动拆装箱
自动拆装箱: 保证基本数据类型和包装类类型之间相互运算
自动拆箱:自动将包装类类型转换成基本数据类型,本质依赖的方法 ii.intValue()
自动装箱:自动将基本数据类型转换成包装类类型,本质依赖的方法 Integer.valueOf(100)

Integer i = 25; 

等效于	Integer i = new Integer(25);	(自动装箱)

基本数据类型和整数类型参与运算

Integer i = new Integer(25);

i = i+5;		

等效于 Integer i = new Integer(i.intValue + 5)  (先自动拆箱,在自动装箱 )		

举例代码如下

public class IntegerDemo04 {
	public static void main(String[] args) {
	   // 装箱: 将基本数据类型转换成对应的包装类类型
	   Integer ii = new Integer(100);
	   // 拆箱: 将包装类类型转换成对应的基本数据类型
	   int i = ii.intValue();
	   
	   // 自动装箱
	   Integer ii2 = 100; //Integer ii2 = Integer.valueOf(100)
	   ii2 += 20; // ii = ii + 20;ii2 = Integer.valueOf(ii.intValue() + 20);
	
//	   Integer iii = null;
//	   iii += 100;
//	   System.out.println(iii);
	   
	   int x = ii2 + 5;
	   boolean bool = ii2 > 5;
	}
}

Character类

char对应的包装类 Character
Character类概述:Character类是char的包装类。
构造方法
public Character(char value)
成员方法
public static boolean isUpperCase(char ch) 判断字符是否是大写
public static boolean isLowerCase(char ch) 判断字符是否是小写
public static boolean isDigit(char ch) 判断字符是否是数字
public static char toUpperCase(char ch) 将字符转大写
public static char toLowerCase(char ch) 将字符转小写

举例代码如下

public class CharacterDemo {
	public static void main(String[] args) {
		Character ch = new Character('a');
//		Character character = 'a';
		
		System.out.println(Character.BYTES);
		System.out.println((int)Character.MAX_VALUE);
		System.out.println(Character.MAX_RADIX);
		System.out.println(Character.MIN_VALUE);
		System.out.println(Character.SIZE);
		
		System.out.println(Character.isUpperCase('A')); 
		System.out.println(Character.isLowerCase('a'));
		System.out.println(Character.isDigit('0'));
		System.out.println(Character.toUpperCase('a'));
		System.out.println(Character.toLowerCase('A'));
		
		System.out.println(Character.isJavaIdentifierPart('1'));
		
	}
}

其他包装类
byte short int long float double 的包装类都是Number类的子类
举例代码如下

public class OtherPackageClassDemo {
	public static void main(String[] args) {
		byte by = 30;
		Byte byt = new Byte(by);
		System.out.println(byt);
		
		Byte byte1 = Byte.valueOf((byte)30);
		byte byteValue = byt.byteValue();
		
		byte parseByte = Byte.parseByte("30");
		
		byt.shortValue();
		byt.intValue();
		byt.longValue();
		
		Long long1 = new Long(100L);
		long1.intValue();
		long1.floatValue();
		
		long parseLong = Long.parseLong("231311");
		Long.valueOf(1000L);
		
		Boolean boolean1 = new Boolean(false);
		boolean1.booleanValue();
		
		Boolean valueOf = Boolean.valueOf(false);
		
	}
}

BigIngeter类

对于超出int范围内的数据进行运算
构造方法
public BigInteger(String val)
成员方法
public BigInteger add(BigInteger val)
public BigInteger subtract(BigInteger val)
public BigInteger multiply(BigInteger val)
public BigInteger divide(BigInteger val)
public BigInteger[] divideAndRemainder(BigInteger val)
举例代码如下

public class BigIntegerDemo01 {
	public static void main(String[] args) {
//		int i = Integer.MAX_VALUE;
//		System.out.println(i + 1);
		
		System.out.println(new BigInteger("2147483647").add(new BigInteger("1")));
		System.out.println(new BigInteger("2147483647").subtract(new BigInteger("1")));
		System.out.println(new BigInteger("2147483647").multiply(new BigInteger("10")));
		System.out.println(new BigInteger("2147483647").divide(new BigInteger("10")));
		
		BigInteger remainder = new BigInteger("4").remainder(new BigInteger("2"));
		System.out.println(remainder);
		System.out.println(Arrays.toString(new BigInteger("4").divideAndRemainder(new BigInteger("2"))));
	}
}

BigDecimal类

在一些金融项目当中,在进行小数运算的时,float、double很容易丢失精度为了能精确的表示、计算浮点数,
Java设计了BigDecimal。
以后在对数据库操作的时候,数据库查询出来的数据没有整数和小数之分,
统称为 Number类型【数据库类型 Oracle】
从数据库中转换到Java对应的数据类型就是 BigDecimal类型
BigDecimal类概述:不可变的、任意精度的有符号十进制数
构造方法
public BigDecimal(String val)
成员变量
public BigDecimal add(BigDecimal augend)
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal multiply(BigDecimal multiplicand)
public BigDecimal divide(BigDecimal divisor)
举例代码如下

public class BigDeccimalDemo {
	public static void main(String[] args) {
//		System.out.println(0.01 + 0.09);
//		System.out.println(1.0 - 0.33);
//		System.out.println(1.015 * 100);
//		System.out.println(1.301 / 100);
		
		System.out.println(new BigDecimal("0.01").add(new BigDecimal("0.09")));
		System.out.println(new BigDecimal("1.0").subtract(new BigDecimal("0.33")));
		System.out.println(new BigDecimal("1.015").multiply(new BigDecimal("100")));
		System.out.println(new BigDecimal("1.301").divide(new BigDecimal("100")));
		
	}
}

Math类

Math类概述:Math 类涵盖了很多执行基本数学运算的方法, 是一个工具类。
成员方法
public static int abs(int a) 求绝对值
public static double sqrt(double a) 开根号
public static double ceil(double a) 向上取整
public static double floor(double a) 向下取整
public static int max(int a,int b) 求最大值
public static int min(int a,int b) 求最小值
public static double pow(double a,double b) 求a的b次幂
public static double random() 生成随机数
public static int round(float a) 四舍五入

Random类

Random类概述:生成随机数
构造方法
public Random()
public Random(long seed)
成员方法
public int nextInt()
public int nextInt(int n)

System类

System类概述:System 类包含一些有用的类字段和方法。它不能被实例化。

成员方法
public static void gc() 运行垃圾回收器
public static void exit(int status) 终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止
public static long currentTimeMillis() 返回以毫秒为单位的当前时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值