常用类:Object、String、StringBuilder、StringBuffer、System、Date
一、Object类
- Object类是所有Java类的根父类
- 如果在类的声明中未使用extends关键字指明其父类,则默认父类 为java.lang.Object类
- 3.Object类中的功能(属性、方法)就具通用性。
属性:无
方法:equals() / toString() / getClass() /hashCode() / clone() / finalize()
wait() 、 notify()、notifyAll()- 4.Object类只声明了一个空参的构造器
1、equals()方法
1.1、equals()方法的使用
- 1.是一个方法,而非运算符
- 2.只能适用于引用数据类型
- 3.Object类中equals()的定义:
public boolean equals(Object obj) {
return (this == obj);
}
说明:Object类中定义的equals()和==的作用是相同的:比较两个对象的地址值是否相同.即两个引用是否指向同一个对象实体- 4 像String、Date、File、包装类等都重写了Object类中的equals()方法。重写以后,比较的不是两个引用的地址是否相同,而是比较两个对象的"实体内容"是否相同。
- 5.通常情况下,我们自定义的类如果使用equals()的话,也通常是比较两个对象的"实体内容"是否相同。那么,我们就需要对Object类中的equals()进行重写.
重写的原则:比较两个对象的实体内容是否相同.
1.2、 如何重写equals()方法
1.2.1、手动重写举例:
class User{
String name;
int age;
//重写其equals()方法
public boolean equals(Object obj){
if(obj == this){
return true;
}
if(obj instanceof User){
User u = (User)obj;
return this.age == u.age && this.name.equals(u.name);
}
return false;
}
1.2.2、自动生成(不举例)
1.3、==运算符和equals()方法的区别
1、” == “既可以比较基本数据类型也可以比较引用类型。对于基本类型就是比较值,对于引用类型就是比较内存地址。
2、equals如果没有被重写过默认也是“ == ”,重写后比较的是类中的相应属性的内容是否相等。
2、toString() 方法
2.1、toString()方法的使用
- 1.toString()方法在Object类中定义,其返回值是String类型,返回类名和它的引用地址。
- 2.当我们输出一个对象的引用时,实际上就是调用当前对象的toString()
- 3.Object类中toString()的定义:
public String toString() {
return getClass().getName() + “@” + Integer.toHexString(hashCode());
}- 4.像String、Date、File、包装类等都重写了Object类中的toString()方法。 使得在调用对象的toString()时,返回"实体内容"信息
- 5.自定义类也可以重写toString()方法,当调用此方法时,返回对象的"实体内容"
2.2、如何重写toString()方法
举例(自动实现):
//自动实现
@Override
public String toString() {
return "Customer [name=" + name + ", age=" + age + "]";
}
3、hashCode()方法
用于获取对象的散列值。Object 中该方法默认返回的是对象的堆内存地址。
hashCode()
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
int code1 = s1.hashCode();
int code2 = s2.hashCode();
System.out.println(code1);
System.out.println(code2);
}
/*
运行结果:
325040804
1173230247
*/
二、String类
字符串是常量,创建之后不可改变。
字符串的字面值存储在字符串池(方法区)中
Sting的方法
1、查询字符串的方法
①、返回字符串长度:
int length()
String s1 = "hello world";
int length = s1.length();
System.out.println(length);
/*
运行结果:11
*/
②根据下标获取字符:
char charAt(int index)
String s1 = "hello world";
char c = s1.charAt(4);
System.out.println(6);
/*
运行结果:w
*/
③判断当前字符串中是否包含传入的参数str:
boolean contains(String str)
String s1 = "hello world";
String s2 = "world";
boolean isContains = s1.contains(s2);
System.out.println(isContains);
/*
运行结果:true
*/
④判断字符串是否以str开头:
boolean startWith(String str)
boolean startWith(String str, int index)
判断字符串是否以str结尾:
boolean endWith(String str)
String s1 = "hello world";
String s2 = "world";
String s3 = "hello";
boolean isStartWith = s1.startsWith(s3);
boolean b = s1.startsWith(s3,0 );
boolean isEndsWith = s1.endsWith(s2);
System.out.println(isStartWith);
System.out.println(b);
System.out.println(isEndsWith);
/*
运行结果:true
true
true
*/
⑤查找str首次出现的下标,存在则返回该下标,不存在则返回-1:
int indexOf(String str)
int indexOf(String str,int startIndex)
找str最后出现的下标,存在则返回该下标,不存在则返回-1:
int lastIndexOf(String str)
String s1 = "hello world";
String s2 = "world";
String s3 = "hello";
String s4 = "shangguigu";
int i = s1.indexOf(s2);
int j = s1.indexOf(s3, 6);
int i1 = s1.lastIndexOf(s2);
System.out.println(i);
System.out.println(j);
System.out.println(i1);
/*
运行结果:6
-1
6
*/
2、修改字符串的方法
①将小写转换成大写:
String toUpperCase()
将大写转换成小写:
String toLowerCase()
String s1 = "hello world";
String s2 ="STRING";
String toUpperCase = s1.toUpperCase();
String toLowerCase = s2.toLowerCase();
System.out.println(toUpperCase);
System.out.println(toLowerCase);
/*
运行结果:HELLO WORLD
string
*/
②去掉字符串首尾的空格:
String trim()
String s1 = " hello world ";
String s2 =" STR ING";
String trim1 = s1.trim();
String trim2 = s2.trim();
System.out.println(trim1);
System.out.println(trim2);
/*
运行结果:hello world
STR ING
*/
③将就字符(串)替换成新字符(串):
String replace(char oldChar,char newChar)
String s1 = "hello world";
String s2 ="STRING";
String replace = s1.replace('l', 'h');
String replace1 = s1.replace("hello", "world");
System.out.println(replace);
System.out.println(replace1);
/*
运行结果:hehho worhd
world world
*/
④将字符串转换成字符数组
char[] toCharArray()
String s2 ="STRING";
char[] chars = s2.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i] + "\t");
}
/*
运行结果:S T R I N G
*/
⑤分割字符串为字符串数组
String[] split()
String s1 = "hello world";
String s2 = "ll";
String[] split = s1.split(s2);
for (String x:
split) {
System.out.println(x);
}
/*
运行结果:he
o world
*/
⑥截取部分字符串(包前不包后):
String subString(int beginIndex,int endIndex)
String s1 = "hello String world";
String substring = s1.substring(6, 10);
System.out.println(substring);
/*
运行结果:Stri
*/
3、比较字符串的方法
①比较两个字符串内容是否相等:
boolean equals(String str)
String s1 = "hello String world";
String s2 = "String";
boolean isEquals = s1.equals(s2);
boolean isEqualsString = s2.equals("String");
System.out.println(isEquals);
System.out.println(isEqualsString);
/*
运行结果:false
true
*/
②忽略大小写比较两个字符串内容是否相等:
boolean equalsIngoreCase(String str)
String s1 = "helloworld";
String s2 = "HELLOWORLD";
boolean isEqualsIgnoreCase = s1.equalsIgnoreCase(s2);
System.out.println(isEqualsIgnoreCase);
/*
运行结果:true
*/
③比较两个字符串大小(在字符编码中的顺序),从第一个字母开始,只比较一个字母,如果长度不同且包含另一个字符串,则比较字符串长度:
String s1 = "helloworld";
String s2 = "HELLOWORLD";
String s3 = "hello";
int i = s1.compareTo(s2);
int i1 = s1.compareTo(s3);
System.out.println(i);
System.out.println(i1);
/*
运行结果:32
5
*/
三、StringBuilder类/StringBuilder类
- 1.StringBuffer: 可变长字符串,JDK1.0提供,运行效率慢、线程安全;
- 2.StringBuilder: 可变长字符串,JDK1.5提供,运行效率快,线程不安全;
- 3.二者功能完全相同
常用方法
①增加字符串:
StringBuffer append(String str)
StringBuffer hello = new StringBuffer("hello");
StringBuffer world = hello.append(" world");
System.out.println(world);
/*
运行结果:hello world
*/
②删除字符串(包前不包后):
StringBuffer delete(int beginIndex,int endIndex)
StringBuffer hello = new StringBuffer("hello world");
StringBuffer delete = hello.delete(2,6);
System.out.println(delete);
/*
运行结果:heworld
*/
③修改字符串:
void setCharAt(int n ,char ch) / replace(int start, int end, String str)(包前不包后)
StringBuffer hello = new StringBuffer("hello world");
hello.setCharAt(6,'a');
System.out.println(hello);
StringBuffer z = hello.replace(2, 4, "zz");
System.out.println(z);
/*
运行结果:hello aorld
hezzo aorld
*/
④查找字符串:
char charAt(int n )
StringBuffer hello = new StringBuffer("hello world");
char c = hello.charAt(6);
System.out.println(c);
/*
运行结果:w
*/
⑤插入字符串:
StringBuffer insert(int offset,String str)
StringBuffer hello = new StringBuffer("hello world");
StringBuffer hello1 = hello.insert(2, " StringBuffer ");
System.out.println(hello1);
/*
运行结果:he StringBuffer llo world
*/
⑥获取字符串长度:
int length()
StringBuffer hello = new StringBuffer("hello world");
int length = hello.length();
System.out.println(length);
StringBuffer hello1 = hello.insert(2, " StringBuffer ");
int length1 = hello1.length();
System.out.println(length1);
/*
运行结果:11
25
*/
四、System类
- 1.System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包。
- 2.由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便的进行调用。
常用方法
1、数组的复制:
void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
其中,src 表示源数组,srcPos 表示从源数组中复制的起始位置,dest 表示目标数组,destPos 表示要复制到的目标数组的起始位置,length 表示复制的个数。
int[] arr = new int[]{1,2,3,4,5,6,7,8};
int[] arr1 = new int[]{100,99,98,97,96};
System.arraycopy(arr,1,arr1,1,2);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println();
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + "\t");
}
/*
运行结果:1 2 3 4 5 6 7 8
100 2 3 97 96
*/
2、返回当前的计算机时间:
static native long currentTimeMillis()
long start = System.currentTimeMillis();
for(int i = 0;i < 100000000;i++) {
int temp = 0;
}
long end = System.currentTimeMillis();
long time = end - start;
System.out.println("程序执行时间" + time + "毫秒");
/*
运行结果:程序执行时间2毫秒
*/
3、static void exit(int status):
该方法的作用是终止当前正在运行的 Java 虚拟机:其中,status 的值为 0 时表示正常退出,非零时表示异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。
4、static void gc()
该方法的作用是请求系统进行垃圾回收,完成内存中的垃圾清除。
5、static String getProperty(String key):
该方法的作用是获得系统中属性名为 key 的属性对应的值。
String jversion = System.getProperty("java.version");
String oName = System.getProperty("os.name");
String user = System.getProperty("user.name");
System.out.println("Java 运行时环境版本:"+jversion);
System.out.println("当前操作系统是:"+oName);
System.out.println("当前用户是:"+user);
/*
运行结果:Java 运行时环境版本:1.8.0_131
当前操作系统是:Windows 10
当前用户是:Lenovo
*/
五、Date类
- 1.Date 类主要封装了系统的日期和时间的信息
- 2.Date 类表示系统特定的时间戳,可以精确到毫秒。Date 对象表示时间的默认顺序是星期、月、日、小时、分、秒、年。
1、构造方法
Date 类有如下两个构造方法。
1、Date():此种形式表示分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒),使用该构造方法创建的对象可以获取本地的当前时间。
2、Date(long date):此种形式表示从 GMT 时间(格林尼治时间)1970 年 1 月 1 日 0 时 0 分 0 秒开始经过参数 date 指定的毫秒数。
Date date = new Date();
System.out.println(date);
Date date1 = new Date(1000000000000L);
System.out.println(date1);
/*
运行结果:Wed Jun 29 10:46:34 CST 2022
Sun Sep 09 09:46:40 CST 2001
*/
2、常用方法
1、判断此日期是否在指定日期之后
boolean after(Date when)
判断此日期是否在指定日期之前
boolean before(Date when)
Date date = new Date();
Date date1 = new Date(1000000000000L);
System.out.println(date);
System.out.println(date1);
boolean before = date.before(date1);
boolean after = date.after(date1);
System.out.println(before);
System.out.println(after);
/*
运行结果:Wed Jun 29 10:49:22 CST 2022
Sun Sep 09 09:46:40 CST 2001
false
true
*/
2、比较两个日期的顺序
int compareTo(Date anotherDate)
当前时间>传入时间返回1
当前时间<传入时间返回-1
当前时间=传入时间返回0
Date date = new Date();
Date date1 = new Date(1000000000000L);
Date date2 = new Date(1000000000000L);
System.out.println(date);
System.out.println(date1);
int i = date.compareTo(date1);
int i1 = date1.compareTo(date);
int i2 = date1.compareTo(date2);
System.out.println(i);
System.out.println(i1);
System.out.println(i2);
/*
运行结果:Wed Jun 29 10:54:05 CST 2022
Sun Sep 09 09:46:40 CST 2001
1
-1
0
*/
3、比较两个日期的相等性:
boolean equals(Object obj)
Date date = new Date();
Date date1 = new Date(1000000000000L);
Date date2 = new Date(1000000000000L);
System.out.println(date);
System.out.println(date1);
boolean equals = date.equals(date1);
boolean equals1 = date1.equals(date2);
System.out.println(equals);
System.out.println(equals1);
/*
运行结果:Wed Jun 29 10:55:51 CST 2022
Sun Sep 09 09:46:40 CST 2001
false
true
*/
4、返回自 1970 年 1 月 1 日 00:00:00 GMT 以来,此 Date 对象表示的毫秒数:
long getTime()
Date date = new Date();
long time = date.getTime();
System.out.println(date);
System.out.println(time);
/*
运行结果:Wed Jun 29 10:57:21 CST 2022
1656471441851
*/
5、把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy。
其中 dow 是一周中的某一天(Sun、Mon、Tue、Wed、Thu、Fri 及 Sat)
String toString()
Date date = new Date();
System.out.println(date);
System.out.println(date.toString());
/*
运行结果:Wed Jun 29 10:59:33 CST 2022
Wed Jun 29 10:59:33 CST 2022
*/