今日内容
一、API
二、Object
三、String&StringBuffer&StringBuilder
四、包装类
五、常用类
- math,random
一、API(应用程序编程接口)
(年前课里的day14里有API文档)
二、Object
Object是所有类的父类
重点学习Object的方法:
hashCode
hashCode方法返回的是对象的地址值
equals
toString
2.1 equals
这样用 equals没有意义,地址值不一样,返回的布尔值一直都是false
判断地址值是无意义的,一般是通过判断对象属性是否一致来决定是否相等,所以,基本上每个类都会重写equals方法
equals不重写则判断地址值,重写则判断对象属性是否相等
2.2 toString
输出语句调用对象时,默认调用toString方法打印地址值
这里打印地址值无意义。一般经常查看对象内的属性值
所以:子类都会重写toString,打印对象内容信息
三、String
java.lang包下的不用导包
3.1 构造方法
- String代表字符串的
- String字符串是常量,不可以改变
创建空字符串:(这两种写法是一样的)
作用一:
- 将字节数组转换为字符串
- 将字符串转换成字节数组
3.2 String类常用方法
String
类包括的方法可用于检查序列的单个字符、比较字符串、搜索字符串、提取子字符串、创建字符串副本并将所有字符全部转换为大写或小写。
3.2.1 关于查字符串的方法
根据下标找字符charAt(索引下标):
根据字符找下标indexOf(),如果找不到就返回-1
3.2.2 关于字符串的判断
- 判断字符串是否包含某些字符 字符串.contains(字符串)
- 判断字符串是否为空 字符串.isEmpty(字符串)
- 判断字符串是否以指定字符开头 字符串.startsWith(字符串)
- 判断字符串是否以指定字符结尾 字符串.endsWith(字符串)
- 判断字符串是否相等 字符串.equals(字符串)
3.2.3 关于字符串转换的方法:
- 字符串转成字节数组 字符串.getBytes()
- 字符串转成字符数组 字符串.toCharArray()
- 字符串转大写,小写 字符串.toLowerCase() 字符串.toUperCase()
**【重点】**
字符串.valueOf 可以将任意数据类型转换为String类型返回
3.2.4 拆分,截取,替换
- 拆分 字符串.split(“”) 拆完存成数组
- 截取字符串(子字符串) 字符串.substring【开头下标,结尾下标)
- 替换字符串 字符串.replace(char oldChar, char newChar)
四、StringBuffer和StringBuilder
因为String是常量,无法改变字符串,如果真要拼接改变字符串是非常耗内存和时间,所以JDK提供了一个StringBuffer和StringBuilder的来专用于可变字符串
4.1 StringBuffer
【重点】append() 追加
- insert()
- delete()
- replace()
- setCharAt(int index, char c)
- 转归转,还是要把StringBuffer转换成String类型,
以下三种方式:(任选其一)
4.2 StringBuilder
StringBuilder也是可变字符串,只不过不保证线程安全,与StringBuffer方法基本一样,但因StringBuffer是线程安全的,效率比较慢,建议再单线程情况下或者不要求线程安全时优先采用StringBuilder类拼接字符串
4.3 总结
String | StringBuffer | Stringbuild | |
---|---|---|---|
是字符串? | 是 | 是 | 是 |
能改变? | 常量 | 能 | 能 |
线程安全? | 安全 | 安全 | 不安全 |
拼接效率 | 最慢 | 快 | 最快 |
出现版本 | JDK1.0 | JDK1.0 | JDLK1.5 |
五、包装类
下去调正idea内存大小
包装类是一些列类的统称,将基本类型包装成类
- 基本类型: byte short int long float double boolean char
- 包装类: Byte Short Integer Long Float Double Boolean Character
5.1 为什么需要包装类?
- 包装成类,里面就可以定义属性和方法,可以创建对象,调用属性和方法完成一些功能,比之前基本类型功能丰富!!!
5.2 包装类的这些方法最主要的是用来干什么的?
5.3 自动装箱拆箱
将基本类型包装成类就是装箱,相反就是拆箱
Integer i = 1; //自动装箱 int a = i; //自动拆箱
5.4 总结图
5.5 Character(了解)
public static void main(String[] args) {
// 判断是否是数字
System.out.println(Character.isDigit('a'));
System.out.println(Character.isDigit('1'));
// 判断是否是字母
System.out.println(Character.isLetter('a') );
System.out.println(Character.isLetter('1') );
// 判断是否是大写/小写
System.out.println(Character.isUpperCase('a') );
System.out.println(Character.isLowerCase('a') );
// 转换大写小写
char a = Character.toUpperCase('a');
System.out.println(a );
char a1 = Character.toLowerCase('A');
System.out.println(a1 );
}
六、Math,Random
6.1 Math数学类
Math类是操作数学运算,属性和方法都是静态的,直接类名.方法/属性调用
方法:
6.2 Random随机数
七、作业
7.1 题 键盘输入身份证号,
判断此人是出生年月日,
此人男女(最后一位是奇数男,偶数女),
是否成年
- 键盘录入
- 根据身份证号特性,截取其中年月日
- 根据下标或者截取获得最后一位
- 首先年份是否够,如果今年18岁,那再考虑月份,如果当月生日,最后考虑天数
public class Demo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入您的身份证号:");
String idCard = sc.next();
// System.out.println(s.substring(6, 14));
//截取出生年月日
String birth = idCard.substring(6, 14);
StringBuilder sb = new StringBuilder(birth);
sb.insert(4, "-");
sb.insert(7, "-");
// System.out.println(sb);
System.out.println("此人的出生年月日是:"+sb);
// System.out.println(idCard.substring(17));
int i = Integer.parseInt(idCard.substring(17));
//判断最后一位是偶数还是奇数,是奇数男,是偶数女
if(i % 2 == 0){
System.out.println("此人是女生");
}else{
System.out.println("此人是男生");
}
String endBirth = String.valueOf(sb);
String[] split = endBirth.split("-"); //年月日
// System.out.println(Arrays.toString(split));
int[] arr = new int[3];
for (int j = 0; j < split.length; j++) {
arr[j] = Integer.parseInt(split[j]);
}
int yearBirth = arr[0];
int monthBirth = arr[1];
int dayBirth = arr[2];
//当前年月日
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("--->"+year);
System.out.println("--->"+month);
System.out.println("--->"+day);
//判断当前年份于生日月份的差是否大于18,大于18则成年
int resultYear;
int resultMonth;
int resultDay;
if((resultYear = (year - yearBirth))>= 18){
//若差值正好是18,则判断月份
if(resultYear == 18){
if((resultMonth = (month - monthBirth)) > 0 ){
System.out.println("此人成年");
}else if(resultMonth == 0){
if((resultDay = (day - dayBirth)) > 0){
System.out.println("此人成年");
}else{
System.out.println("此人未成年");
}
}
}else{
System.out.println("此人成年,目前"+resultYear+"岁");
}
}else{
System.out.println("此人未成年,目前"+resultYear+"岁");
}
}
}
7.2 题 键盘输入一段文字,将文字内容中文保留,大写变小写,小写变大写,数字剔除后输出
- 键盘录入
- 将数据拆分成字符数组
- 准备一个StringBuilder的空字符串
- 遍历字符数组,判断大写(转小写),小写(转大写),是数字(不要),剩下的不动,原样拼接
public class Demo3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一段文字:");
String s = sc.next();
// System.out.println(s);
//将输入的字符串编程字符数组
char[] charArray = s.toCharArray();
// for (int i = 0; i < split.length; i++) {
// System.out.println(split[i]);
// }
StringBuilder sb = new StringBuilder();
for (int i = 0; i < charArray.length; i++) {
//如果是大写,转小写
if(Character.isUpperCase(charArray[i])){
sb.append(Character.toLowerCase(charArray[i]));
}//如果是小写,转大写
else if(Character.isLowerCase(charArray[i])){
sb.append(Character.toUpperCase(charArray[i]));
}else if(Character.isDigit(charArray[i])){
}else {
sb.append(charArray[i]);
}
}
System.out.println(sb);
}
}
7.3 题 定义学生类,认为名字一样且年龄相差2岁以内即认为相同
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
Student s = (Student) o;
if(this.name.equals(s.name)&& Math.abs(this.age - s.age) <= 2){
return true;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
class TestStudent{
public static void main(String[] args) {
Student stu1 = new Student("张三", 2);
Student stu2 = new Student("张三", 4);
System.out.println(stu1.equals(stu2));
}
}
quals(s.name)&& Math.abs(this.age - s.age) <= 2){
return true;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
class TestStudent{
public static void main(String[] args) {
Student stu1 = new Student(“张三”, 2);
Student stu2 = new Student(“张三”, 4);
System.out.println(stu1.equals(stu2));
}
}