Java常用类

1.枚举

枚举指由一组固定的常量组成的类型
枚举中默认都是全局静态常量的取值,直接写值,多个值使用逗号分隔

/*-----Student.java-----*/
public class Student {
	public Sex sex;
	public String name;
	
	public static void main(String[] args) {
		Student stu1 = new Student();
		stu1.name = "赵四";
		stu1.sex = Sex.MALE;
		
		Student stu2 = new Student();
		stu2.name = "大脚";
		stu2.sex = Sex.FEMALE;	
	}
}
/*-----Sex.java-----*/
public enum Sex {
	MALE,FEMALE
}

2.包装类

2.1包装类

包装类把基本数据类型转换为对象
目的:
提供了一系列对象可以使用的方法
集合不允许存放基本数据类型数据,存放数字时,需要用包装类
| 基本数据类型 | 包装类 |
| ------- | --------- |
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
| char | Character |
除了char包装类之外,其他的包装类都可将一个字符串作为参数构造它们的实例
char包装类只支持传入char类型
Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示为true,否则,表示为false
当Number包装类构造方法参数为String类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则运行不通过,运行会抛出NumberFormatException异常

public static void main(String[] args) {
	byte b = 12;
	Byte b1 = new Byte(b);
	Byte b2 = new Byte("120");
	System.out.println(b1);
	System.out.println(b2);

	Short s1 = new Short((short)555);
	Short s2 = new Short("2356");

	Integer i1 = new Integer(2356);
	Integer i2 = new Integer("5623");

	Long l1 = new Long(5623568989745121L);
	Long l2 = new Long("5689");
	
	Float f1 = new Float(2.2);
	Float f2 = new Float(2.2F);
	Float f3 = new Float("2.2");

	Double d1 = new Double(20.3);
	Double d2 = new Double("20.3");

	Boolean bl1 = new Boolean("fAlse");
	System.out.println(bl1);
	Boolean bl2 = new Boolean("TrUe");
	System.out.println(bl2);
	Boolean bl3 = new Boolean("abcdefg");
	System.out.println(bl3);
	Boolean bl4 = new Boolean(true);
	
	Character ch1 = new Character('a');
	System.out.println(ch1);

	// A 65  a 97 
	Character ch2 = new Character((char)66);
	System.out.println(ch2);
}

2.2 xxxValue()

xxxValue()将包装类转换为基本数据类型

public static void main(String[] args) {
	Byte b2 = new Byte("120");
	byte b3 = b2.byteValue();
		
	Short s1 = new Short((short)555);
	short s2 = s1.shortValue();
		
	Integer i1 = new Integer(2356);
	int i2 = i1.intValue();
		
	Long l1 = new Long(5623568989745121L);
	long longValue = l1.longValue();
			
	Float f1 = new Float(2.2);
	float floatValue = f1.floatValue();
		
	Double d1 = new Double(20.3);
	double doubleValue = d1.doubleValue();
		
	Boolean bl1 = new Boolean("fAlse");
	boolean booleanValue = bl1.booleanValue();
		
	Character ch1 = new Character('a');
	char charValue = ch1.charValue();

2.3 valueOf()

valueOf()将基本数据类型转换为包装类
除了Character类以外,其他的包装类还支持传入一个String对象,转换类型

public static void main(String[] args) {
	int a = 10;
	Integer i1 = Integer.valueOf(a);
	Integer i2 = Integer.valueOf("230");
		
	Byte b2 = Byte.valueOf((byte)12);
	Byte b3 = Byte.valueOf("62");
		
	Short s1 = Short.valueOf((short)231);
	Short s2 = Short.valueOf("5645");
		
	Long l1 = Long.valueOf(2356);
	Long l2 = Long.valueOf("8956");
		
	Float f1 = Float.valueOf(5623);
		
	Double d1 = Double.valueOf(230);
		
	Boolean bl1 = Boolean.valueOf(true);
		
	Character ch1 = Character.valueOf('a');	
	}

2.4 toString()

以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)

 String sex = Character.toString('男')
  String id = Integer.toString(25);

2.5 parseXXX()

把字符串转换为相应的基本数据类型(Character除外)(字符串->基本类型)

 int num = Integer.parseInt("36");
  boolean bool = Boolean.parseBoolean("false");

3.装箱和拆箱

装箱:基本类型转换为包装类的对象
拆箱:包装类对象转换为基本类型的值

public static void main(String[] args) {
	Byte b1 = new Byte((byte)20);
	Byte b2 = new Byte((byte)30);
	System.out.println(b1.toString());
	System.out.println(b2);
	System.out.println(b1 + b2);
	Integer i = 20; // 自动装箱
	int a = i; // 自动拆箱
		
	System.out.println(i + a);
}

4.Math类

提供有常用的数学计算方法和两个静态常量E PI

public static void main(String[] args) {
	System.out.println(Math.E);
	System.out.println(Math.PI);
		
	// 以下方法面试题
	System.out.println(Math.abs(-300));   // 绝对值 300
	System.out.println(Math.ceil(-3.3));  // 向上取整 -3
	System.out.println(Math.floor(3.9));  // 向下取整 3
	System.out.println(Math.round(3.6));  // 四舍五入 4
	System.out.println((int)(Math.random() * 13));  // 0-13之间的随机数
}

5.Random类

获取更多类型的随机数据

public static void main(String[] args) {
	Random ran1 = new Random();
	System.out.println(ran1.nextBoolean());  // 随机生成一个boolean类型
	System.out.println(ran1.nextDouble());   // 随机生成一个double类型的数
	System.out.println(ran1.nextFloat());    // 随机生成要给float类型的数
	System.out.println(ran1.nextInt());      // 随机生成一个int型数
	System.out.println(ran1.nextInt(100));   // 0-100之间随机生成一个数
	System.out.println(ran1.nextLong());     // 随机生成一个long类型的数
		
	// 不同的Random对象使用相同的种子 生成的随机数是相同
	Random ran2 = new Random(111);
	Random ran3 = new Random(111);
	System.out.println(ran2.nextInt());
	System.out.println(ran3.nextInt());    //这两个生成的随机数每次都相同
}
posted @ 2021-07-24 14:47  码丁XIA  阅读(37)  评论(0)    收藏  举报