喵星之旅-成长的雏鹰-java高级特性-2-java实用类

枚举

枚举指由一组固定的常量组成的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public enum Week {
MON,TUE,WED,THU,FRI,SAT,SUN
}


/**
* 枚举常量的定义和使用
* */
public class WeekDemo2 {
/**
* 做什么事情
* */
public void doWhat(Week day){
//使用枚举
switch(day){
case MON:
case TUE:
case WED:
case THU:
case FRI:
System.out.println("工作日,努力写代码!");
break;
case SAT:
System.out.println("星期六,休息!看电影!");
break;
case SUN:
System.out.println("星期日,休息!看电影!");
break;
default:
System.out.println("地球上的一个星期就7天");
}
}

public static void main(String[] args){
WeekDemo2 wd=new WeekDemo2();
wd.doWhat(Week.FRI);
}
}

包装类

包装类把基本类型数据转换为对象,每个基本类型在java.lang包中都有一个相应的包装类。

包装类的作用:
1、提供了一系列实用的方法
2、集合不允许存放基本数据类型数据,存放数字时,要用包装类型

所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例。
除Character类外,其他包装类可将一个字符串作为参数构造它们的实例。

注意事项
1、Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false。
2、当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常。

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

JDK1.5后,允许基本数据类型和包装类型进行混合数学运算。
包装类并不是用来取代基本数据类型的,
在基本数据类型需要用对象表示时使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Demo {
public static void main(String[] args) {
//所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
Integer i=new Integer(34);
Double d=new Double(98.7);
Boolean b=new Boolean(true);
Character c=new Character('a');
System.out.println(i+"\t"+d+"\t"+b+"\t"+c);

//除Character类外,其他包装类可以一个字符串为参数构造它们的实例
//编译错误
//Character c2=new Character("a");
Integer i2=new Integer("34");
Double d2=new Double("98.7");
Boolean b2=new Boolean("true");
System.out.println(i2+"\t"+d2+"\t"+b2);

//Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false
Boolean b3=new Boolean("TRue");
Boolean b4=new Boolean("false");
Boolean b5=new Boolean("love");
System.out.println(b3+"\t"+b4+"\t"+b5);

//当包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译通过,运行时NumberFormatException异常
Integer i3=new Integer(null);
Double d4=new Double("包装类");
System.out.println(i3+"\t"+d4);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* 包装类常用方法
* */
public class Demo {
public static void main(String[] args){
//XXXValue():包装类转换成基本类型
Integer integerId=new Integer(25);
int intId=integerId.intValue();
System.out.println(intId);

Boolean booleanVal=new Boolean(true);
boolean bool2=booleanVal.booleanValue();
System.out.println(bool2);
System.out.println("*************************");

//toString():以字符串形式返回包装对象表示的基本类型数据
String sex=Character.toString('男');
String id=Integer.toString(89);
System.out.println(sex);
System.out.println(id);
String sex2='男'+"";
String id2=89+"";
System.out.println(sex2);
System.out.println(id2);
System.out.println("*************************");

//所有包装类valueOf(type value)
Integer intValue=Integer.valueOf(21);
System.out.println(intValue);
Boolean bool=Boolean.valueOf(false);
System.out.println(bool);
System.out.println("*************************");

//除Character类外,其他包装类valueOf(String s)
intValue=Integer.valueOf("32");
//bool=Boolean.valueOf("true");
bool=Boolean.valueOf("love");
//编译错误
//Character c=Character.valueOf("a");
System.out.println(intValue);
System.out.println(bool);
System.out.println("*************************");


//parseXXX():把字符串转换为相应的基本数据类型数据(Character除外)
int i=Integer.parseInt("89");
System.out.println(i);
//boolean flag=Boolean.parseBoolean("true");
//boolean flag=Boolean.parseBoolean("TRue");
//boolean flag=Boolean.parseBoolean("love");
boolean flag=Boolean.parseBoolean("false");
System.out.println(flag);
System.out.println("*************************");

//基本类型和包装类的自动转换:装箱和拆箱
//装箱
Integer intObject=5;
//拆箱
int intValue2=intObject;
System.out.println(intObject+"\t"+intValue2);



}
}

Math

java.lang.Math类提供了常用的数学运算方法和两个静态常量E(自然对数的底数) 和PI(圆周率)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner;

/*
* 幸运抽奖:会员号的百位数与系统随机数相同,即为中奖
* */
public class GoodLuck {

public static void main(String[] args) {
//随机产生一个0-9之间的任意整数
int random=(int)(Math.random()*10);
// System.out.println(random);
//从控制台接收一个任意的四位数
System.out.print("请输入4位会员号:");
Scanner input=new Scanner(System.in);
int custNo=input.nextInt();
//获得会员号的百位数
int baiwei=custNo/100%10;
if(baiwei==random){
System.out.println(custNo+"是幸运客户,获得精美MP3一个。");
}else{
System.out.println(custNo+" 谢谢您的支持!");
}

}
}

Random

生成随机数的方式

1
2
3
4
Random rand=new Random(); //创建一个Random对象
for(int i=0;i<20;i++){//随机生成20个随机整数,并显示
int num=rand.nextInt(10);//返回下一个伪随机数,整型的 System.out.println("第"+(i+1)+"个随机数是:"+num);
}

用同一个种子值来初始化两个Random 对象,然后用每个对象调用相同的方法,得到的随机数也是相同的。

String

String类位于java.lang包中,具有丰富的方法:
计算字符串的长度、比较字符串、连接字符串、提取字符串。

String类提供了length()方法,确定字符串的长度。

String类提供了equals( )方法,比较存储在两个字符串对象的内容是否一致。

字符串比较的其他方法:
1、使用equalsIgnoreCase()方法
2、使用toLowerCase()方法
3、使用toUpperCase()方法

字符串连接:
方法1:使用“+”;
方法2:使用String类的concat()方法。

字符串常用提取方法:
public int indexOf(int ch) 搜索第一个出现的字符ch,如果没有找到,返回-1;
public int indexOf(String value) 搜索第一个出现的字符串value,如果没有找到,返回-1;
public int lastIndexOf(int ch) 搜索最后一个出现的字符ch,如果没有找到,返回-1;
public int lastIndexOf(String value) 搜索最后一个出现的字符串value,如果没有找到,返回-1;
public String substring(int index) 提取从位置索引开始的字符串部分;
public String substring(int beginindex, int endindex) 提取beginindex和endindex之间的字符串部分;
public String trim() 返回一个前后不含任何空格的调用字符串的副本。

文章目录
  1. 枚举
  2. 包装类
  3. Math
  4. Random
  5. String
|