2020.7.30实用类介绍(一)

                实用类(一)

枚举:由一组固定的常量组成的类型
public enum Genders{
Male, Female }
public class Student{
public Genders sex;}
Student stu=new Student();
stu.sex=Gender.Male;
stu.sex=“你好”//此时就会报错
优点:类型安全,易于输入,代码清晰
包装类及其构造方法
1.包装类把基本类型数据转换为对象
2.每个基本类型在java.lang包中都有一个相应的包装类
包装类的作用:
(1).提供了一系列实用的方法
(2).如果集合无法存放基本数据类型,基本数据类型转换为包装类放进去。
基本数据类型的包装类:

char–>Character
int–>Integer
其它首字母大写
3.包装类的构造方法:
(1)以每个包装类类对应的基本数据类型作为参数
int iNum=9;
Integer i =new Integer(iNum);
double dNum=9.8;
Double d=new Double(dNum);
char cNum=‘c’;
Character c=new Character(cNum);
float fNum=9.8f;
Float f=new Float(f);
boolean bNum=true;
Boolean v=new Boolean(bNum);
(2)除Character以外,以字符串作为参数,Character没有带字符串作为参数的构造方法
Integer i2=new Integer(“123”)
System.out.println( i2+1);
结果:124,不是1231;
注意:
Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写) ,则该Boolean对象表示true ,否则表示false
如:Boolean b1=new Boolean(“TrUe”);
输出b1,结果为true;
Boolean b1=new Boolean(“love”);
输出结果为false
◆当Number包装类构造方法参数为String类型时,字符串不能为null ,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常
4.将包装类转换成基本类型:
xxx.value
例如:
Integer i =new Integer(100);
int iNum=i.intValue();
Boolean b=new Boolean(true);
boolean Inum=b.boolea();
5.把基本数据类型转换为字符串
(1)toString
int num =100;
String sNum=Interger.toString(num);
boolean b3=true;
String sB3=Boolean.toString(b3);
(2)+""
int num =100;
String sNum=100+"";
boolean b3=true;
String sB3=true+"";
6.把字符串转换为相应的基本数据类型(Character)
parseXXX()
String age=“18”;
int ageNum=Integer.paraseInt(age);
String score=“98,3”;
double score2=Double.parseDouble(score);
7.j将基本类型/字符串转换为包装类;
valuOf();
int j=90;
Integer j1=Integer.valueOf(j);
char sex =‘男’;
Character cSex=Character.valueOf(sex);
String m=“123”
Integer n=Integer.valueOf(m)
注意
(1)Character无法使用,当包装类为Number时,参数字符必须为数字兼容的字符串,否则会NumberFormatException
(2)当Boolean调用valueOf(字符串)时,除了参数内同为true(不区分大小写),其它都转化为false
装箱和拆箱
装箱:基本类型转换为包装类的对象
例如:
Integer i =new Integer(100);
int j=i;
拆箱:包装类对象转换为基本类型的值
Math类和Random类
java.langMatb类提供了常用的数学运算方法和两个静态常量E(自然对数的底数)和PI(圆周率)
生成随机数
int random=(int)(Math.random())//随机生成0~1的数字
Random rand=new Random();//创建一个Random对象
for(int i=0;i<20;i++){//随机生成20个随机整数,并显示
int num=rand.nextInt(10);
System.out.println(num) }
System.out.println(random.nextInt())//生成int范围内的随机整数
System.out.println(random.nextInt(10))//生成0~10范围内的随机数
随机数生成器种子不同,每次生成的随机数不同,反之则相同
Random rand1=new Random()
Random rand2=new System.out.println(rand1.nextInt(10))
System.out.println(rand2.nextInt(10))
两数的结果则不同


补充
1.枚举:可以认为是类的变种,里面的直接写的值都是该类的公共静态常量,可以用类型 .值的方式调用,可以和普通类一样定义方法和属性
2.valueOf方法常用来将该类的枚举类型与字符串类型比较
3.包装类:对应每个基本数据类型,都有一个包装类型与之对应
目的:可以把基本数据类型转换成对象,也可以把字符串转成相应的基本数据类型
注意: 泛型不可以放基本数据类型
4.装箱和拆箱:把基本数据类型赋值给包装类,就是装箱,反之就是拆箱
注意实例:
1.当自动装箱时,Integer i=1
Interger j=1;
System.out.print(ij)结果是true,原因是未新建地址
2.Integer i=128
Integer j=128;
System.out.print(i
j)结果是false,原因是超过了int缓冲区,新建地址
integer k=new Integer(1)
System.out.print(i==k)结果是false,新建了地址
5.Character没有String的构造方法
以Integer为例:几种赋值方法
new Integer(整型)
new Integer(字符串)
Ineteger。parseInt(字符串)
基本类型–>封装后
byte Byte
short: Short
int: Integer
long : Long
float Float
double: Double
char: Character
boolean: Boolean
6.Math的方法:
Math.cell:向上取整
Math.floor向下取整
Math.abs:返回绝对值
Math.sqrt()开根号
注意实例:
Math.round四舍五入Math.round(1.499999999999999999999999)结果是2,因为系统默认它是1.5

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值