枚举:枚举指由一组固定的常量组成的类型
public enum Sexs {
//枚举
男,女
}
public class Student {
public String name;
public Sexs sex;
}
public class Test {
public static void main(String[] args) {
Student stu=new Student();
stu.name="张三";
stu.sex=Sexs.男;
System.out.println(stu.name+"-"+stu.sex);
}
}
包装类
包装类把基本类型数据转换为对象
每个基本类型在java.lang包中都有一个相应的包装类
包装类的作用
提供了一系列实用的方法
集合不允许存放基本数据类型数据,存放数字时,要用包装类型
public class Demo02 {
public static void main(String[] args) {
// 所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
// 除Character类外,其他包装类可将一个字符串作为参数构造它们的实例
//对于Number类型的对象,字符串需要是“数字形式的字符串”,也不能传递一个null值
byte byt = 10;
Byte byte1 = new Byte(byt);
Byte byte2 = new Byte("123");
System.out.println(Byte.MAX_VALUE);
System.out.println(Byte.MIN_VALUE);
System.out.println(byte2);
System.out.println("-----------------");
short sho = 100;
Short short1 = new Short(sho);
Short short2 = new Short("12342");
System.out.println(Short.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
System.out.println(short2);
int num = 1000;
Integer integer = new Integer(num);
Integer integer2 = new Integer("1232");
System.out.println();
long lon = 10000l;
Long long1 = new Long(lon);
Long long2 = new Long("2426");
float flo = 100.5f;
Float float1 = new Float(flo);
Float float2 = new Float("3.5");
double dou = 99.88;
Double double1 = new Double(dou);
Double double2 = new Double("153.45");
boolean bool = true;
Boolean boolean1 = new Boolean(bool);
//字符串里的内容为true(不区分大小写)的时候,对象里存储的是true,其它所有情况都为false
Boolean boolean2 = new Boolean("True");
System.out.println(boolean2);
}
}
包装类的方法
public class Demo03 {
public static void main(String[] args) {
// 包装类常用方法
// XXXValue():包装类转换成基本类型
Long lon1 = new Long(125L);
long num1 = lon1.longValue();
// toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)
String sex = Character.toString('男');
String str = Boolean.toString(true);
Integer inte = new Integer(100);
String str2 = inte.toString();
// parseXXX():把字符串转换为相应的基本数据类型数据(Character除外,并且字符串里面的内容要能够转换为数字,否则会报异常)(字符串->基本类型)
byte num2 = Byte.parseByte("123");
System.out.println(num2);
boolean result = Boolean.parseBoolean("TruE");
System.out.println(result);
// valueOf():所有包装类都有如下方法(基本类型->包装类) public static Type valueOf(type value)
byte byte2 = 10;
Byte byte3 = Byte.valueOf(byte2);
Character cha = Character.valueOf('女');
// 除Character类外,其他包装类都有如下方法(字符串->包装类)public static Type valueOf(String s)
Byte byte4 = Byte.valueOf("123");
// Character.valueOf("a");
}
}
装箱和拆箱
装箱:直接将基本数据类型赋值给了包装类对象
拆箱:将包装类对象直接赋值给基本数据类型的变量
public class Demo04 {
public static void main(String[] args) {
// 装箱和拆箱
byte byte1 = 10;
Byte byte2 = new Byte(byte1);
Byte byte3 = Byte.valueOf(byte1);
// 装箱:直接将基本数据类型赋值给了包装类对象
Byte byte4 = byte1;
System.out.println(byte4);
Integer int1 = new Integer(20);
int int2 = int1.intValue();
// 拆箱:将包装类对象直接赋值给基本数据类型的变量
int int3 = int1;
System.out.println(int3);
// JDK1.5后,允许基本数据类型和包装类型进行混合数学运算
Integer num1 = new Integer(100);
int num2 = 1000;
int sum = num1 + num2;
System.out.println(sum);
Integer sum2 = num1 + num2;
System.out.println(sum2);
}
}
Math类
public class MathDemo {
public static void main(String[] args) {
double num =Math.random();
System.out.println(num);
//随机获取一个[num1,num2)之间的整数
// int num =(int)(Math.random()*(num2-num1)+num1);
double pi=Math.PI;
System.out.println(pi);
System.out.println(Math.abs(-35));
//ceil(double a):返回一个比a大的离a最近的整数
System.out.println(Math.ceil(-2.4));
System.out.println(Math.ceil(4.4));
//floor(double a):返回一个比a小的离a最近的整数
System.out.println(Math.floor(3.3));
System.out.println(Math.floor(-2.4));
//round():根据四色五入的原则返回数据
System.out.println(Math.round(3.5));
System.out.println(Math.round(3.4));
System.out.println(Math.max(12, 25));
System.out.println(Math.min(56, 23));
//pow(double a,double b):返回a的b次方结果
System.out.println(Math.pow(3, 4));
}
}
Random类
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
//创建Random类对象
Random random1 = new Random();
System.out.println(random1);
boolean result1 =random1.nextBoolean();
System.out.println(result1);
int result2=random1.nextInt();
System.out.println(result2);
int result3=random1.nextInt(10);
System.out.println(result3);
Random random2 = new Random(100L);
System.out.println(random2.nextInt());
Random random3 = new Random(100L);
System.out.println(random3.nextInt());
}
}