字符串操作类StringBuilder、StringBuffer
1.String类中的equals(Object anObject)
1.equals方法并不是String类的实例方法,它原本是Object类的实例方法,只是String继承Object类,将这个方法重写了。
2.所有的java类都默认继承了Object,包括我们自己定义的Java类。只是我们在自定义java类的时候没有使用extends关键字去继承Object类。【一切皆对象】
equals方法的含义是用来做比较的,比较两个对象是否相同。
面试题:”= =” 与 equals方法的区别?
“= =”常用来比较基本数据类型,8种基本数据类型有byte、short、long、double、char、int、float、boolean,因为变量直接存储的就是他们的值,所以用"= ="去比较,比较的就是他们的值。但是复合数据类型用“==”比较的是他的堆内存地址【引用地址】。
“equals”对于复合数据类型比较的也是它的堆内存地址(不能作用于基本数据类型的变量)。
由于String类重写了equals方法所以String类“equals”比较的是存储对象的内容是否相等
“==” | “equals” | |
比较运算符 | Object类的实例方法 | |
基本数据类型 | 可以,保存在变量中的数据值 | 不能被比较 |
复合数据类型 | 堆内存地址【引用地址】 | 堆内存地址【引用地址】 |
如果被重写比较的是存储对象的内容 |
例如:
package com.wangxing.test1;
public class TestMain1 {
public static void main(String[] args) {
//"=="
//1.可以比较基本数据类型【比较的是保存在变量中的数据值】
/*
int num1=100;
int num2=100;
if(num1==num2){
System.out.println("int型的num1==int型的num2");
}else{
System.out.println("int型的num1 不相等 int型的num2");
}
*/
//2.可以比较复合数据类型【比较的是他的堆内存地址【引用地址】】
/*
Student stu1=new Student();
System.out.println("stu1=="+stu1.hashCode());
Student stu2=new Student();
System.out.println("stu2=="+stu2.hashCode());
//stu1=stu2;
if(stu1==stu2){
System.out.println("stu1==stu2");
}else{
System.out.println("stu1!=stu2");
}
*/
//equals()方法
//1.equals()方法是Object类的实例方法,我们创建的java类继承了Object类中的equals()方法。
//2.equals()方法不能比较基本数据类型
//3.equals()方法继承但没有重写是,比较的是复合数据类型的堆内存地址【引用地址】
Student stu1=new Student();
System.out.println("stu1=="+stu1.hashCode());
Student stu2=new Student();
System.out.println("stu2=="+stu2.hashCode());
//stu1=stu2;
if(stu1.equals(stu2)){
System.out.println("stu1==stu2");
}else{
System.out.println("stu1!=stu2");
}
//4.equals()方法被继承重写之后,比较的是复合数据类型的存储对象的内容
//String类继承重写了equals()方法
String str1=new String("hello");
System.out.println("str1=="+str1.hashCode());
String str2=new String("hello");
System.out.println("str2=="+str2.hashCode());
if(str1.equals(str2)){
System.out.println("str1==str2");
}else{
System.out.println("str1!=str2");
}
}
}
2.StringBuilder类如何创建对象,有哪些常用方法?
public final class String 一个不可变的字符序列【重新开辟存储空间】
String str1=”hello”;
String str2=”world”;
String str3=str1+str2;
StringBuilder定义:
public final class StringBuilder 一个可变的字符序列【开辟存储空间为同一个】
StringBuilder b1=new StringBuilder(“hello”);
b1.append(“world”);
此类提供与StringBuffer相同的API,但不保证线程安全
构造方法:
StringBuilder() 构造一个初始容量为16个字符的空StringBuilder对象。 |
---|
StringBuilder(CharSequence seq) 通过其他的StringBuilder对象创建一个新的StringBuilder对象 |
-StringBuilder(int capacity) 构造一个由 capacity参数指定的初始容量得空StringBuilder对象。 |
StringBuilder(String str) 构造一个初始化为指定字符串内容的StringBuilder对象。 |
package com.wangxing.test1;
public class TestStringBuilder1 {
public static void main(String[] args) {
//StringBuilder() 构造一个初始容量为16个字符的空StringBuilder对象。
StringBuilder stringBuilder1=new StringBuilder();
//StringBuilder(CharSequence seq) 通过其他的StringBuilder对象创建一个新的StringBuilder对象
StringBuilder stringBuilder2=new StringBuilder(stringBuilder1);
//StringBuilder(int capacity) 构造一个由 capacity参数指定的初始容量得空StringBuilder对象。
StringBuilder stringBuilder3=new StringBuilder(30);
//StringBuilder(String str) 构造一个初始化为指定字符串内容的StringBuilder对象。
StringBuilder stringBuilder4=new StringBuilder("hello");
}
}
实例方法
StringBuilder | append(Object o) 将参数的字符串表示追加到序列中。 |
---|---|
int | capacity() 返回当前容量。 |
char | charAt(int index) 返回 char在指定索引在这个字符值。 |
StringBuilder | delete(int start, int end) 删除此序列的子字符串中的字符。 |
StringBuilder | deleteCharAt(int index) 删除 char在这个序列中的指定位置。 |
int | indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引。 |
int | lastIndexOf(String str) 返回指定子字符串最右边出现的字符串内的索引。 |
StringBuilder | insert(int offset, Object o) 将参数的字符串表示插入到此序列中指定的位置。 |
int | length() 返回长度(字符数)。 |
StringBuilder | reverse() 导致该字符序列被序列的相反代替。 |
StringBuilder | replace(int start, int end, String str) 用指定的String中的字符替换此序列的子字符串中的 String 。 |
String | substring(int start) 返回一个新的 String ,其中包含此字符序列中当前包含的字符的子序列。 |
String | substring(int start, int end) 返回一个新的 String ,其中包含此序列中当前包含的字符的子序列。 |
String | toString() 返回表示此顺序中的数据的字符串。 |
package com.wangxing.test1;
public class TestStringBuilder2 {
public static void main(String[] args) {
StringBuilder str1=new StringBuilder();
System.out.println("没有append之前的初始容量=="+str1.capacity());
str1.append("hello");
str1.append(1234);
str1.append(true);
str1.append(12.5);
str1.append("world");
System.out.println("str1=="+str1);
System.out.println("append之后的容量=="+str1.capacity());
System.out.println("StringBuilder容量扩展规则==当前容量*2+2");
System.out.println("charAt(3)=="+str1.charAt(3));
System.out.println("delete(13,17)=="+str1.delete(13,17));
System.out.println("deleteCharAt(3)=="+str1.deleteCharAt(3));
System.out.println("indexOf('l')=="+str1.indexOf("l"));
System.out.println("lastIndexOf('l')=="+str1.lastIndexOf("l"));
System.out.println("insert(12,12.5)=="+str1.insert(12,12.5));
System.out.println("length=="+str1.length());
System.out.println("replace(8,12,'false')=="+str1.replace(8,12,"false"));
//System.out.println("reverse()=="+str1.reverse());
System.out.println("substring(4, 8) =="+str1.substring(4, 8));
}
}
3.String类与StringBuilder类之间的相互转换
String转换成StringBuilder—》StringBuilder(String str)
StringBuilder转换成String–》StringBuilder的toString()
String的构造方法String(StringBuilder builder)
4.StringBuffer类如何创建对象,有哪些常用方法?
StringBuffer定义:
public final class StringBuffer一个可变的字符序列[线程安全]
构造方法
StringBuffer() 构造一个初始容量为16个字符的空StringBuffer对象。 |
---|
StringBuffer(CharSequence seq) 通过其他的StringBuffer对象创建一个新的StringBuffer对象 |
StringBuffer(int capacity) 构造一个由 capacity参数指定的初始容量得空StringBuffer对象。 |
StringBuffer(String str) 构造一个初始化为指定字符串内容的StringBuffer对象。 |
实例方法
StringBuffer | append(Object o) 将参数的字符串表示追加到序列中。 |
---|---|
int | capacity() 返回当前容量。 |
char | charAt(int index) 返回 char在指定索引在这个字符值。 |
StringBuffer | delete(int start, int end) 删除此序列的子字符串中的字符。 |
StringBuffer | deleteCharAt(int index) 删除 char在这个序列中的指定位置。 |
int | indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引。 |
int | lastIndexOf(String str) 返回指定子字符串最右边出现的字符串内的索引。 |
StringBuffer | insert(int offset, Object o) 将参数的字符串表示插入到此序列中指定的位置。 |
int | length() 返回长度(字符数)。 |
StringBuffer | reverse() 导致该字符序列被序列的相反代替。 |
StringBuffer | replace(int start, int end, String str) 用指定的String中的字符替换此序列的子字符串中的 String 。 |
String | substring(int start) 返回一个新的 String ,其中包含此字符序列中当前包含的字符的子序列。 |
String | substring(int start, int end) 返回一个新的 String ,其中包含此序列中当前包含的字符的子序列。 |
String | toString() 返回表示此顺序中的数据的字符串。 |
5.String类与StringBuffer类之间的相互转换
String转换成StringBuffer—》StringBuffer(String str)
StringBuffer转换成String–》StringBuffer的toString()
String的构造方法String(StringBuffer buffer)
面试题:String、StringBuilder、StringBuffer的区别?
String | StringBuilder | StringBuffer | |
可变与否 | 不可变 | 可变 | 可变 |
线程安全与否 | 非线程安全 | 非线程安全 | 线程安全 |
运行速度 | 慢 | 快 | 较快 |
操作的情况 | 少量的字符串操作[拼接]的情况 | 单线程下字符串操大量操作的情况 | 多线程下字符串操大量操作的情况 |
时间日期的操作类
1.Calendar如何创建对象,有哪些常用方法?
Calendar类的定义
public abstract class Calendar 他是一个抽象类,它不能通过new+构造方法的方式构建对象
构造方法:
protected | Calendar() 构建具有默认时区和默认的 FORMAT语言环境的日历。 |
---|---|
protected | Calendar(TimeZone zone, Locale aLocale) 构造具有指定时区和区域设置的日历。 |
Calendar类的定义和它的protected修饰符修饰的构造方法,决定Calendar类的对象创建和特殊,不会是new+构造方法的方式
通过本类提供静态方法完成
static Calendar | getInstance() 使用默认时区和区域设置获取日历。 |
---|---|
static Calendar | getInstance(Locale aLocale) 使用默认时区和指定的区域设置获取日历。 |
static Calendar | getInstance(TimeZone zone) 使用指定的时区和默认语言环境获取日历。 |
static Calendar | getInstance(TimeZone zone, Locale aLocale) 获取具有指定时区和区域设置的日历。 |
//创建Calendar类对象
Calendar calendar=Calendar.getInstance();
日历的作用1.看日子 2.定日子
int | get(int field) 返回给定日历字段的值。 参数:int field--具体查询的问题 Calendar 的静态变量 |
void | set(int year, int month, int date, int hourOfDay, int minute, int second) 设置字段中的值 YEAR , MONTH , DAY_OF_MONTH , HOUR_OF_DAY , MINUTE和 SECOND 。 |
Date | getTime() 返回一个 Date表示此物体 Calendar的时间值(毫秒从偏移 Epoch “)。 |
Calendar 的静态变量
static int | YEAR get现场编号和 set表示年份。 |
static int | MONTH get和 set字段号表示月份。 |
static int | DAY_OF_MONTH get字段编号和 set本月的日期。 |
static int | DAY_OF_WEEK get字段编号和 set表示一周中的日期。 |
static int | DAY_OF_YEAR get和 set字段编号, set本年度的日数。 |
static int | WEEK_OF_YEAR get和 set字段编号, set本年度的周数。 |
static int | WEEK_OF_MONTH get和 set字段编号, set当月的周数。 |
package com.wangxing.test1;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestCalendar {
public static void main(String[] args) {
//创建Calendar类对象
Calendar calendar=Calendar.getInstance();
//int get(int field) 返回给定日历字段的值。
//int field---给定的日历字段
//YEAR---年
System.out.println("YEAR--"+calendar.get(Calendar.YEAR));
//MONTH --月【从0开始数】
System.out.println("MONTH--"+(calendar.get(Calendar.MONTH)+1));
//DAY_OF_MONTH--月中的天
System.out.println("DAY_OF_MONTH--"+calendar.get(Calendar.DAY_OF_MONTH));
//DAY_OF_WEEK---周中的星期【从星期天开始数】
System.out.println("DAY_OF_WEEK--"+calendar.get(Calendar.DAY_OF_WEEK));
//DAY_OF_YEAR---年中的天
System.out.println("DAY_OF_YEAR--"+calendar.get(Calendar.DAY_OF_YEAR));
//WEEK_OF_YEAR--年中的周
System.out.println("WEEK_OF_YEAR--"+calendar.get(Calendar.WEEK_OF_YEAR));
//WEEK_OF_MONTH--月中的周
System.out.println("WEEK_OF_MONTH--"+calendar.get(Calendar.WEEK_OF_MONTH));
//set(int year, int month, int date, int hourOfDay, int minute, int second)
//设置字段中的值 YEAR , MONTH , DAY_OF_MONTH , HOUR_OF_DAY , MINUTE和 SECOND 。
calendar.set(2020, 9, 1, 12, 30,30);
Date date =calendar.getTime();
System.out.println(date);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");
String newdate=sdf.format(date);
System.out.println(newdate);
}
}