1.枚举
1.1 枚举产生的原因
场景:定义属性的同时如果出现不合逻辑的类型,则需要用枚举类型替代(通俗来说就是规定了某些参数就是在某个固定的数据内,不允许这些参数是其它值)
1.2 创建枚举
枚举指由一组固定的常量组成的类型
枚举的关键字:enum
public enum Genders {
Male,Female;
}
1.3 使用枚举
public class Student2 {
private Genders sex;
public Genders getSex() {
return sex;
}
public void setSex(Genders sex) {
this.sex = sex;
}
public static void main(String[] args) {
Student2 student2 = new Student2();
student2.setSex(Genders.MALE);
System.out.println(student2.getSex()); }
}
1.4 使用枚举——2
定义学生类,属性为成绩,利用枚举类型实现优秀,良好,及格,不及格与A、B、C、D的映射
public enum Achievement {
excellent("优秀"),good("良好"),pass("及格"),fail("不及格");
private String name;
Achievement() {
}
Achievement(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Student {
private Achievement achievement;
public Achievement getAchievement() {
return achievement;
}
public void setAchievement(Achievement achievement) {
this.achievement = achievement;
}
public static void main(String[] args) {
Student student = new Student();
student.setAchievement(Achievement.excellent);
System.out.println(student.getAchievement().getName());
}
}
1.5 使用枚举 ——3
public class Student1 {
private String score;
public Student1() {
}
public Student1(String score) {
this.score = score;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public static void main(String[] args) {
Student1 student = new Student1();
student.setScore(Achievement.excellent.getName());
System.out.println(student.getScore());
}
}
2.包装
2.1 定义
每个基本数据类型在java.lang包都各自有自己的包装类型,如int类型的包装类型是Integer,char类型的包装类型是Character等等
2.2 包装类型作用
- 包装类型提供了一系列方法,例如可以实现基本数据类型和包装类型之间的转换等功能
- 集合中只能存放包装类型的数据,不能存放基本类型的数据
2.3 包装类型的构造方法实现
(1)基本数据类型转换成包装类型:
public static void main(String[] args) {
int a = 12;
//将基本数据类型int类型的a转换成了包装类型Integer
Integer integer = new Integer(a);
byte b = 1;
Byte by = new Byte(b);
short s = 12;
Short sh = new Short(s);
long l = 12;
Long lo = new Long(l);
}
(2)字符串类型转换成包装类型
除了Character类型以外都可以使用构造器将字符串类型转换为包装类型
public static void main(String[] args) {
String a = "123";
//字符串类型转换成Integer类型
Integer integer = new Integer(a);
System.out.println(integer);
Double d = new Double(a);
}
备注:特例Boolean
2.4 常用方法
利用包装类型的xxvalue()方法 实现 包装类型转换为基本数据类型
Boolean b = new Boolean("True");
boolean b1 = b.booleanValue();
System.out.println(b);
基本数据类型转换成字符串类型
int a = 3;
//利用包装类型中的toString() 实现 将基本数据类型转换成字符串类型
String b = Integer.toString(a);
将字符串类型转换成基本数据类型(利用包装类型的parseXX() 除Character以外)
String a = "133";
int aa = Integer.parseInt(a);
基本数据类型转换成包装类型(利用包装类型中的valueOf())
int aa = 12;
Integer integer1 = new Integer(aa);
Integer integer3 = Integer.valueOf(aa);
字符串类型转换成包装类型(利用包装类型中的valueOf() 除Character以外)
String a = "1223";
Integer integer = new Integer(a);
Integer integer2 = Integer.valueOf(a);
2.5 装箱和拆箱
装箱:基本类型转换成包装类型的对象(自动转换!)
拆箱:包装类对象转换为基本类型的值(自动转换!)
//基本数据类型
int a = 3;
//基本数据类型自动转换成包装类型--》装箱
Integer b = a;
//包装数据类型
Integer aa = 3;
//包装数据类型自动转换成基本类型--》装箱
int bb = aa;
备注:int和Integer属性在初始化的时候系统默认赋值不同
int类型的属性默认赋值0
Integer类型属性默认赋值null
3.String
3.1 String基本语法
- 定义:存储字符串
- 语法:String a = “”; String b = new String(); String c = new String(“”);
- 包路径:java.lang包
- 备注:String a = “”;//字符串变量是存储在方法区中(常量池中)
String c = new String(“”);字符串变量存放在堆中
3.2 常用方法
3.2.1 length()
length():返回此字符串的长度
String a = "helloworld";
System.out.println(a.length());
备注:数组长度,array.length 属性
集合长度:list.size() 方法
3.2.2 equals()
equals():比较的是字符串的内容是否相同
==:比较的是内存地址是否相同
int a = 1;
int b = 1;
System.out.println(a==b); //这里的结果是true
System.out.println(a.equals(b)); //这里编译报错,equals不是是基本类型的方法
System.out.println("乔尼".equals("jojo"));//这里的结果是false
对于基本类型的比较要使用==而不能使用equals
这里要扩展说说==与equals的区别:
String a = "11";
String b = "11";
String c = new String("11");
System.out.println(a==b);//结果true
System.out.println(a.equals(b));//结果true
System.out.println(a==c);//结果false
System.out.println(a.equals(c));//结果true
上述例子在我们看来a,b,c应该都是一样的,为什么还能有false,
这里我们要先从变量的存储状态说起,基本类型的变量都是存放在栈里的,而引用类型则是存放在堆里的,但是对映射在栈中一个地址。
==比较的是变量在栈中的内容是否相同,对于引用类型来说就是比较其内存地址是否相同,上述的a和b都未开辟新空间,在栈中的地址都是同一个,而c开辟了新空间就相当于在堆中新开辟了一个空间,在栈中的地址就自然与b,c不同了
equals比较的是堆中的内容是否相同,所以上面都是true
3.2.3 equalsIgnoreCase()
定义:将此String与另一个String做比较,不考虑大小写
String a = "hello";
String b = "Hello";
System.out.println(a.equalsIgnoreCase(b));//结果为true
3.2.4 toLowerCase()
定义:String中所以字符都转换成小写【大写转小写,小写不变】
String st = "Jojo".toLowerCase();//st值为jojo
3.2.5 toUpperCase()
定义:String中所以字符都转换成大写【小写转大写,大写不变】
String st = "Jojo".toLowerCase();//st值为JOJO
3.2.6 concat()
定义:将两个字符串拼接,与+功能相同
String st1 = "hello"+"world";
String st2 = "hello".concat("world");
3.2.7 trim()
定义:返回字符串的副本,忽略头部空白和尾部空白
String st = " hello world ".trim();//st值为hello world
3.2.8 indexOf()
定义:返回指定字符串在此字符串中第一次出现处的索引,如果未出现则返回-1
String s = "hello world";
System.out.println(s.indexOf("w"));//结果为6,索引从0开始算起,空格也算一位
3.2.9 substring()
substring(int beginIndex):
返回一个子字符串,该子字符串从指定索引处的字符开始,直到此字符串末尾
String s = "hello world";
System.out.println(s.substring(7));//结果为orld,索引从0开始算起,空格也算一位
substring(int beginIndex,int endIndex)
返回一个子字符串,该字符串从指定的beginIndex处开始,直到索引endIndex - 1处的字符。因此,该子字符串的长度为endIndex - beginIndex【能取到的值是左闭右开区间】
String s = "hello world";
System.out.println(s.substring(7,8));//结果为o,索引从0开始算起,空格也算一位
3.2.10 split()
定义:分割字符串
String s = "hello world";
String []st = s.split(" ");//以空格分割字符串s,返回一个字符串数组
for(String s1 : st){
System.out.println(s1);//结果为依次输出hello和world
}
4.StringBuffer
4.1 StringBuffer产生的原因
场景:String是不可修改的字符串,拼接效率低,所以需要使用StringBuffer替代
String的+或者concat方法都是返回一个新的String字符串,所以说其效率低,而StringBuffer则是修改自身。
4.2 StringBuffer声明
StringBuffer buffer = new StringBuffer(“hello”);
StringBuffer buffer = new StringBuffer();
4.3 常用方法
4.3.1 append()
将新的字符串拼接到原有的字符串末尾
StringBuffer buffer = new StringBuffer("jojo");
buffer.append("hello");
System.out.println(buffer);//输出jojohello
4.3.2 insert()
在原有的字符串的某个索引位置上插入某个字符串,该索引之后的字符依次往后移
buffer.insert(2, "123");
System.out.println(buffer);//输出jo123jo
5.日期
5.1 获取当前日期
Date date = new Date();
System.out.println(date);//输出当前时间
5.2 字符串类型转成Date类型
使用SimpleDateFormat的parse()方法
String s = "2023-12-1 17:12:1";
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd hh:mm:dd");
Date date1 = format.parse(s);
System.out.println(date1);
5.3 Date类型转成字符串类型
使用SimpleDateFormat的format()方法
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd hh:mm:dd");
String datest = format.format(date);
System.out.println(datest);