一,枚举[enumeration]
1,是指一组有限的常量构成的集合类型
2、通过关键字enum定义枚举
public enum Gender {
Male,Female;//男,女
@Override
public String toString() {
String ret=null;
switch (Gender.this){
case Male:
ret= "男";
break;
case Female:
ret= "女";
break;
}
return ret;
}
}
3、为了获取更有意义的数据通常会重写toString()方法
private String name;
private Gender gender;//性别
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", gender=" + gender +
'}';
}
public static void main(String[] args) {
Student student=new Student();
student.setName("李四");
student.setGender(Gender.Female);
System.out.println(student);
}
4、通过Java API[Application Programming Interface应用程序编程接口]手册可以学习更多的有关对象的常用方法
二,包装类
1、包装类的作用:是把基本数据类型转换为引用数据类型(即对象类型)
2、八种基本数据类型对应的包装类型:
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
3、包装类的继承关系:Object <--Boolean,Character,Number
Number<--Byte,Short,Integer,Long,Float,Double
//输出int数据类型的最大值(32个字节,2的31次方,32-31=代表最大最小的正负数)
System.out.println(Integer.MAX_VALUE);
// 输出int数据类型的最小值(最大值+1变成最小值,最小值+1变成最大值)
System.out.println(Integer.MIN_VALUE);
// 输出int数据类型占用的字节数量[1Byte=8bit]
System.out.println(Integer.SIZE);
//输出类型表示
System.out.println(Integer.TYPE);
1.包装类的构造方法
1、创建Integer对象时可把int数据或String数据作为构造方法的参数
【注意】String数据必须是数字字符[0-9,+,-]构成
//创建包装类对象
Integer i1=new Integer(123);//int类型
Integer i2=new Integer("456");//String类型
2、创建Boolean对象时可把boolean数据或String数据作为构造方法的参数
【注意】String数据(除了不区分大小写的)"true"之外全解释为false
Boolean b1=new Boolean(true);
Boolean b2=new Boolean(false);
//只要不是true,都是false
Boolean b3=new Boolean("true");
Boolean b4=new Boolean("True");
Boolean b5=new Boolean("TRUE");
Boolean b6=new Boolean("ABC");
3、创建Character对象时只能把char数据作为构造方法的参数
Character c1=new Character('A');
Character c2=new Character('中');
System.out.println(c1.toString());
System.out.println(c2.toString());
4、创建Byte,Short,Long,Double,Float与创建Integer相同
2.包装类常用的方法
1、在所有包装类型中都有XxxValue()方法可把对象类型转换为对应的基本数据类型
//把Interger转换成int
int i3=i2.intValue();
System.out.println(i3);
//把Bollean转换成bollean
Boolean b7=b6.booleanValue();
System.out.println(b7);
//把character转换成char
char c3=c1.charValue();
System.out.println(c3);
2、在所有包装类型中都有toString()方法可把对应的基本类型数据转换为String
String s1=Integer.toString(123);
String s2=Boolean.toString(true);
String s3=Character.toString('a');
String s4=Double.toString(1.6);
String s5=Long.toString(123L);
3、所有基本类型数据通过+""或""+可转换为字符串
System.out.println('a'+""+'b');//ab中间要+双引号
System.out.println(""+2+3);//23
System.out.println('a'+'b');//195
4、包装类提供了把字符串转换为基本类型数据的方法parseXxx(),注意Character除外。
int i=Integer.parseInt("123");
boolean b=Boolean.parseBoolean("abc");
boolean c=Boolean.parseBoolean("True");
5、包装类提供了把字符串或基本类型数据转换为包装类型数据的方法valueOf(),注意Character不能使用字符串做参数
Integer i1=Integer.valueOf(123);
Integer i2=Integer.valueOf("456");
Boolean b1=Boolean.valueOf("false");
Boolean b2=Boolean.valueOf("a");
6、在字符串转换为某种包装类型时如果遇到java.lang.NumberFormatException异常则表示存在非法字符
三,包装类及装箱、拆箱概念
1、装箱是指把基本类型数据转换为包装类型数据
2、拆箱是指把包装类型数据转换为基本类型数据
int i1=100;
Integer i2=i1;//自动装箱
i1=i2;//自动拆箱
Boolean b1=true;
boolean b2=b1;
b1=b2;
char c1='a';
Character c2=c1;
c1=c2;
int s1=1+2;
Integer i1=1;//自动装箱
Integer i2=2;//自动装箱
Integer s2=i1+i2;//先自动拆箱求和,在自动装箱
System.out.println(s2);
//10(10)=1010(2)=A(16)
// 1000(10)=1111101000(2)=3E8(16)
int i=1000;
String s2=Integer.toBinaryString(i);
String s16=Integer.toHexString(i);
System.out.println(s2);
System.out.println(s16)
四,Math类的数学运算
1、java.lang.Math提供多种关于数学运算的静态方法,至少要求掌握以下常用方法
(1)求PI---Math.PI(3.14)
(2)求E----Math.E(2.718)
//两个重要无理数
System.out.println(Math.PI);
System.out.println(Math.E);
(3)求绝对值---abs()
//求绝对值
System.out.println(Math.abs(9));
System.out.println(Math.abs(-9));
System.out.println(Math.abs(0));
(4)求大于等于参数的最小整数---ceil()
//求大于等于参数的最小整数
System.out.println(Math.ceil(3));//3.0
System.out.println(Math.ceil(3.4));//4.0
System.out.println(Math.ceil(3.8));//4.0
System.out.println(Math.ceil(-3);//-3.0
System.out.println(Math.ceil(-3.4));//-3.0
System.out.println(Math.ceil(-3.8));//-3.0
(5)求小于等于参数的最大整数---floor()
//求小于等于参数的最大整数
System.out.println(Math.floor(3));//3.0
System.out.println(Math.floor(3.4));//3.0
System.out.println(Math.floor(3.8));//3.0
System.out.println(Math.floor(-3));//-3.0
System.out.println(Math.floor(-3.4));//-4.0
System.out.println(Math.floor(-3.8));//-4.0
(6)求两个数中的较大值---max()
//求两个数或三个数中的较大值
System.out.println(Math.max(1,2));
System.out.println(Math.max(Math.max(1,2),3));
(7)求两个数中的较小值---min()
//求两个数或三个数中的较小值
System.out.println(Math.min(1,2));
System.out.println(Math.min(Math.min(1,2),3));//超过两个三个用循环
(8)求四舍五入值 Math.floor(x+0.5)---round()
System.out.println(Math.round(3));//3
System.out.println(Math.round(3.4));//3
System.out.println(Math.round(3.8));//4
System.out.println(Math.round(-3));//-3
System.out.println(Math.round(-3.4));//-3
System.out.println(Math.round(-3.8));//-4
System.out.println(Math.round(-3.5));//-3
System.out.println(Math.round(3.5));//4
(9)求随机小数 [0,1)---random()(包括0,不包括1)
求随机整数: [m,n)= (int)((较大值-较小值)*随机数+较小值)(较小值包括,较大值不 包括)
double d;
int n;
for (int i=0;i<10;i++) {
d = Math.random();
d *= 10;
n = (int) d;
System.out.println(n);
}
//求随机整数 [m,n),[25,55) = (int)((较大值-较小值)*随机数+较小值)
int d;
for (int i = 0; i < 10; i++) {
d = (int) ((55 - 25) * Math.random() + 25);
System.out.println(d);
}
double d;
long count = 0;
while(true){
d = Math.random();
count++;
if(d<=0.0125) break;
}
System.out.println(count);
;