目录
2.3.2构造方法2(字符串作为参数构造包装类),及字符串拼接
2.4.3 parseXXX() 字符串类型--->基本类型(Character除外)
5.2字符串型转换成Date类型和Date类型转换成字符串型
1.枚举
1.1使用非枚举的方式对Student类的sex属性赋值
//使用非枚举的方式实现对Student类中的sex属性赋值
public class Student {
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public static void main(String[] args) {
Student student = new Student();
student.setSex("女");
System.out.println(student.getSex());
student.setSex("asd");
System.out.println(student.getSex());
}
}
1.2使用枚举的方式对Student类中的sex属性赋值
//定义枚举类型,里面的内容为限制的各个赋值
public enum Gender {
男,女
}
//赋值
public class Student {
private Gender sex; // 定义sex属性的类型为枚举类型
public Gender getSex() { // get,set方法
return sex;
}
public void setSex(Gender sex) {
this.sex = sex;
}
public static void main(String[] args) {
Student student = new Student();
student.setSex(Gender.女);
System.out.println(student.getSex()); // 赋值方法改变
// student.setSex("asd"); // 一般赋值的方法会报错
//定义枚举类型,里面的内容为限制的各个赋值
public enum Gender {
男,女
}
关键字:enum 替换 class
2.包装类
2.1定义
每个基本类型在Java.lang包中都有一个对应的包装类
2.2作用
1)提供了一系列的实用方法
2)在集合中不能存放基本数据类型,有时候为了使用基本数据作为对象类型,就需要添加包装类
int,short,等是基本数据类型,不能算对象类型,所以经过包装类,可以在泛型中使用
ArrayList不允许
ArrayList可以,Integer为int的包装类
//成员变量为基本数据类型时,系统会默认赋值为0,但是包装类不会默认赋值,和对象类型,String类型一样,不赋值的情况下都是null
//基本数据类型和包装数据类型的区别:
public class Demo2 {
private int a;
private Integer b;
private Demo2 d;
public static void main(String[] args) {
Demo2 demo2 = new Demo2();
System.out.println(demo2.a); // 0
System.out.println(demo2.b); // null
System.out.println(demo2.d); // null
}
}
2.3构造方法(基本数据类型转换成包装类)
2.3.1基本方法
public class Demo3 {
public static void main(String[] args) {
// 将基础数据类型的变量名作为new包装类的引用
int a = 1;
Integer a_new = new Integer(a);
// 将基础数据类型直接赋值
byte b = 2;
Byte b_new = b;
// new新对象的时候直接赋值
Short c_new = new Short(3);
long d = 4;
Long d_new = new Long(d);
float e = 5;
Float e_new = new Float(e);
double f = 6;
Double f_new = new Double(f);
char g = 'a';
Character g_new = new Character(g);
boolean i = true;
Boolean i_new = i;
}
}
2.3.2构造方法2(字符串作为参数构造包装类),及字符串拼接
可以利用字符串来当作参数构造包装类(除了char类型)(boolean类型在利用字符串作为参数构造的时候,除了ture,不分大小写,之外都是false)
public class Demo4 {
public static void main(String[] args) {
//利用字符串作为参数来构造包装类
String e = "123";
Integer e_new = new Integer(e);
String f = "a";
Character f_new = new Character(f); // 会报错
String g = "trUe";
Boolean g_new = new Boolean(g); // true
//两个字符串拼接
String a = "123";
System.out.println(a + "1"); // 1231
//一个字符串,一个基本数据类型进行 "+",拼接
String b = "123";
System.out.println(b + 1); // 1231
int c = 123;
String d = c + "";
System.out.println(d); // 123
}
}
2.3.3两个构造中的常见特例:
public class Demo5 {
public static void main(String[] args) {
// 会报错,Character不可以用字符串来构造
// Character a = new Character("abcd");
// Boolean 类型的除了(true,不分大小写)以外,都是false
Boolean b = new Boolean("sfa");
Boolean b1 = new Boolean("trUe");
System.out.println(b); // false
System.out.println(b1); // true
}
}
2.4常见方法
2.4.1 intValue() 包装类转换成基本数据类型
//将包装类转换成基本类型
public class Demo6 {
public static void main(String[] args) {
Integer a = new Integer(123);
a.intValue();
Double b = new Double(20.0);
b.doubleValue();
}
}
2.4.2 toString() 基本类型转换成字符串类型
public class Demo7 {
public static void main(String[] args) {
// 第一种:常用转字符串,拼接
int a = 12;
String a_new = a + "";
// 第二种toString()
int b = 12;
String b_new = Integer.toString(b);
double c = 12.2;
String c_new = Double.toString(c);
}
}
2.4.3 parseXXX() 字符串类型--->基本类型(Character除外)
格式:类型 变量名 = 包装类名.parse包装类名(字符串的变量名);
// 字符串转化成int
String a = "123";
int a1 = Integer.parseInt(a);
System.out.println(a1 + 1);
// 字符串转化成double
String b = "123.0";
double b1 = Double.parseDouble(b);
System.out.println(b1 + 1);
// 字符串转化为boolean
String c = "djka";
boolean c1 = Boolean.parseBoolean(c);
System.out.println(c1);
// 字符串转化成char,会报错
// String d = "char";
// char d1 = Character.parseCharacter(d);
2.4.4 valueOf()
valueOf的第一种实现:基本数据类型转换成包装类型
int a = 123;
Integer a1 = Integer.valueOf(a);
double b = 123.0;
Double b1 = Double.valueOf(b);
char c = 's';
Character c1 = Character.valueOf(c);
//没有特例,字符char也可转化
valueOf的第二种实现:字符串类型转换成包装类型
String d = "123";
Integer d1 = Integer.valueOf(d);
String e = "123.0";
Float e1 = Float.valueOf(e);
String f = "sfad";
Boolean f1 = Boolean.valueOf(f);
//字符char会报错
//String g = "a";
//Character g1 = Character.valueOf(g);
2.5装箱和拆箱
//基本数据类型可以自动转换成包装类型(装箱,概念)
Integer a = 1;
//包装类型可以自动转换成基本数据类型(拆箱,概念)
int b = a;
2.6总结包装类
3.String类
3.1使用:
String a = "hello world";
String a = new String("hello world");
3.2==和equals
==比较地址
equals比较内容
注意:基本数据类型只能用==比较
// 字符串相同指向同一地址
String a = "hello";
String b = "hello";
System.out.println(a == b); // true
System.out.println(a.equals(b)); // true
// new对象的地址不同
String a1 = "hello";
String b1 = new String("hello");
System.out.println(a1 == b1); // false
System.out.println(a1.equals(b1)); // true
// 两个new对象地址不同,内容相同
String a3 = new String("Hello");
String b3 = new String("Hello");
System.out.println(a3 == b3); // false
System.out.println(a3.equals(b3)); // true
3.3常用方法
equalsIgnoreCase() 比较内容,不分大小写
toLowerCase() 字符串转换成小写
toUpperCase() 字符串转换成大写
concat() 将两个字符串拼接,+
trim() 去除字符串两边的空格
indexOf() 查询索引
substring() 截取字符串
split() 分割字符串
// 1.equalsIgnoreCase() 比较内容,不分大小写
String a = "tom";
String b = "Tom";
System.out.println(a.equalsIgnoreCase(b));
// 2.toLowerCase() 将字符串转换为小写
String a1 = "tom";
String b1 = "TOM".toLowerCase();
System.out.println(a1.equals(b1));
// 3.toUpperCase() 将字符串转换成大写
String a2 = "tom".toUpperCase();
String b2 = "TOM";
System.out.println(a2.equals(b2));
// 4.concat() 将两个字符串拼接
String a3 = "abc";
String b3 = "def";
System.out.println(a3.concat(b3));
System.out.println(a3 + b3);
// 5.trim() 将两边的空格去掉
String a4 = " hello world ";
System.out.println(a4.trim());
// 6.indexOf() 查询索引
String a6 = "hello yes world yes2 yes yes ";
int index = a6.indexOf("o");
System.out.println(index);
int index1 = a6.indexOf("girl");
System.out.println(index1);
int index2 = a6.indexOf("yes");
System.out.println(index2);
int index3 = a6.lastIndexOf("yes");
System.out.println(index3);
// 7.substring() 截取字符串
String a7 = "hello yes world yes";
System.out.println(a7.substring(5));
System.out.println(a7.substring(5, 8)); //前闭后开,5,6,7三个元素
// 8.split() 分割字符串
String a8 = "长亭外,古道边,芳草碧连天";
String[] a8Arrays = a8.split(",");
for (String s : a8Arrays) {
System.out.println(s);
}
结果:
true
true
true
abcdef
abcdef
hello world
4
-1
6
25
yes world yes
ye
长亭外
古道边
芳草碧连天
4.StringBuffer
4.1产生原因
//String字符串的拼接
String s = "hello";
s = s.concat("world");
System.out.println(s);
字符串的拼接每次需要重新赋值,无法直接修改,效率低
//StringBuff字符串的拼接
StringBuffer sb = new StringBuffer("hello");
sb.append("world");
System.out.println(sb);
StringBuffer字符串可以直接添加,效率高
4.2使用
//StringBuffer转换成String
StringBuffer sb1 = new StringBuffer("hello");
String s1 = sb1.toString();
//String转换成StringBuffer
String s2 = "hello";
StringBuffer sb2 = new StringBuffer(s2);
sb2.append("world");
System.out.println(sb2);
4.3常用方法
length() 长度
insert(index, "值") 在index下标处插入值
StringBuffer s3 = new StringBuffer("hello");
System.out.println(s3.length()); // 5
s3.insert(2, ",");
System.out.println(s3); // he,llo
5.日期类型
5.1获取当前日期
1.实例化Date对象
Date date = new Date();
2.输出系统当前时间
System.out.println(date); // Thu Aug 26 09:12:22 CST 2021
5.2字符串型转换成Date类型和Date类型转换成字符串型
// 字符串型转换成Date型
String s = "2021-08-26 07:23:43";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf.parse(s);
System.out.println(date1); // Thu Aug 26 07:23:43 CST 2021
// Date型转换成字符型型
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = simpleDateFormat.format(date1);
System.out.println(dateStr); // 2021-08-26 07:23:43