字符串操作类StringBuilder、StringBuffer

字符串操作类StringBuilder、StringBuffer

1.String类中的equals(Object anObject)

     1.equals方法并不是String类的实例方法,它原本是Object类的实例方法,只是String继承Object类,将这个方法重写了。

     2.所有的java类都默认继承了Object,包括我们自己定义的Java类。只是我们在自定义java类的时候没有使用extends关键字去继承Object类。【一切皆对象】

       equals方法的含义是用来做比较的,比较两个对象是否相同。区别如下:

        “==”常用来比较基本数据类型,8种基本数据类型有byte、short、long、double、char、int、float、boolean,因为变量直接存储的就是他们的值,所以用"=="去比较,比较的就是他们的值。但是复合数据类型用“==”比较的是他的堆内存地址【引用地址】。

       “equals”对于复合数据类型比较的也是它的堆内存地址(不能作用于基本数据类型的变量)。由于String类重写了equals方法所以String类“equals”比较的是存储对象的内容是否相等

 

“==”

“equals”

 

比较运算符

Object类的实例方法

基本数据类型

可以,保存在变量中的数据值

不能被比较

复合数据类型

堆内存地址【引用地址】

堆内存地址【引用地址】

如果被重写比较的是存储对象的内容

      例如:

package com.wangxing.equalstest;

public class Student {

}
package com.wangxing.equalstest;

public class Test1 {
	public static void main(String args[]){
		/*
		//"=="
		//1.可以比较基本数据类型【比较的是保存在变量中的数据值】
			int num1=10;
			int num2=20;
			if(num1==num2){
				System.out.println("num1等于num2");
			}else{
				System.out.println("num1不等于num2");//实际输出的结果
			}
		//2.可以比较复合数据类型【比较的是他的堆内存地址【引用地址】】
			Student st1=new Student();
				System.out.println(st1.hashCode());
			Student st2=new Student();
				System.out.println(st2.hashCode());
			if(st1==st2){
				System.out.println("st1等于st2");
			}else{
				System.out.println("st1不等于st2");//实际输出的结果
			}	
		*/
		//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");
			String str2=new String("world");
			if(str1.equals(str2)){
				System.out.println("str1=str2");
			}else{
				System.out.println("str1!=str2");//实际输出的结果
			}
			
			String str3=new String("hello");
			String str4=new String("hello");
			if(str3.equals(str4)){
				System.out.println("str3=str4");//实际输出的结果
			}else{
				System.out.println("str3!=str4");
			}
			
	}
}

7.6.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”);

    此类提供与StringBuffer相同的API,但不保证线程安全

       构造方法:

StringBuilder() 构造一个初始容量为16个字符的空StringBuilder对象。

StringBuilder(CharSequence seq) 通过其他的StringBuilder对象创建一个新的StringBuilder对象

StringBuilder(int capacity) 构造一个由 capacity参数指定的初始容量得空StringBuilder对象。

StringBuilder(String str) 构造一个初始化为指定字符串内容的StringBuilder对象。

package com.wangxing.stringbuilder;

public class StringBuildTest1 {

	public static void main(String[] args) {
		//StringBuilder()构造一个初始容量为16个字符的空StringBuilder对象
			StringBuilder b1=new StringBuilder();
		//StringBuilder(CharSequence seq)通过StringBuilder对象创建的StringBuilder对象
		//只要参数是CharSequence接口类型
		//CharSequence接口类型---String  
		//CharSequence接口类型---StringBuilder
		//CharSequence接口类型---StringBuffer
			StringBuilder b2=new StringBuilder(b1);
		//StringBuilder(int capacity)构造一个由capacity参数指定的初始容量得空StringBuilder对象
			StringBuilder b3=new StringBuilder(30);
		//StringBuilder(String str)构造一个初始化为指定字符串内容的StringBuilder对象
			StringBuilder b4=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.stringbuilder;

public class StringBuildTest2 {

	public static void main(String[] args) {
			StringBuilder b1=new StringBuilder();//默认初始容量16,超过16则2倍加2扩容
		//StringBuilder	append(Object o) 将参数的字符串表示追加到序列中。 
				b1.append("hello");
				b1.append("world");
				b1.append(1234);
			System.out.println(b1);//helloworld1234
		//int	capacity() 返回当前容量。
			System.out.println(b1.capacity());//16
		//char	charAt(int index) 返回 char在指定索引在这个字符值。
			System.out.println(b1.charAt(6));//o
		//StringBuilder	delete(int start, int end) 删除此序列的子字符串中的字符。
			System.out.println(b1.delete(10,14));//helloworld
		//int	indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引。
			System.out.println(b1.indexOf("w"));//5
		//StringBuilder	deleteCharAt(int index) 删除 char在这个序列中的指定位置。
			System.out.println(b1.deleteCharAt(9));//helloworl
		//int	lastIndexOf(String str) 返回指定子字符串最右边出现的字符串内的索引。
			System.out.println(b1.lastIndexOf("l"));//8
		//StringBuilder	insert(int offset, Object o) 将参数的字符串表示插入到此序列中指定的 
        //位置。
			System.out.println(b1.insert(9, "d1234"));//helloworld1234
		//int	length() 返回长度(字符数)。
			System.out.println(b1.length());//14
		//StringBuilder	reverse() 导致该字符序列被序列的相反代替。
			//System.out.println(b1.reverse());//4321dlrowolleh
		//StringBuilder	replace(int start, int end, String str) 用指定的String中的字符替换 
        //此序列的子字符串中的 String 。
			System.out.println(b1.replace(5, 10, "zhangsan"));//hellozhangsan1234
		//String	substring(int start) 返回一个新的 String ,其中包含此字符序列中当前包含的 
        //字符的子序列。 
		//String	substring(int start, int end) 返回一个新的 String ,其中包含此序列中当前 
        //包含的字符的子序列。 	
			String s1=b1.substring(0,13);
			System.out.println(s1);//hellozhangsan
			System.out.println(b1);//hellozhangsan1234
		//String	toString() 返回表示此顺序中的数据的字符串。
			System.out.println(b1.toString());//String类型的"hellozhangsan1234"						

	}

}

7.7.String类与StringBuilder类之间的相互转换

     String转换成StringBuilder---》StringBuilder(String str)

     StringBuilder转换成String--》StringBuildertoString()

                                                   String的构造方法String(StringBuilder builder)

7.8.StringBuffer类如何创建对象,有哪些常用方法?

     StringBuffer定义:public final class StringBuffer一个可变的字符序列[线程安全]

     构造方法:

StringBuffer() 构造一个初始容量为16个字符的空StringBuffer对象。

StringBuffer(CharSequence seq) 通过其他的StringBuffer对象创建一个新的StringBuffer对象

StringBuffer(int capacity) 构造一个由 capacity参数指定的初始容量得空StringBuffer对象

StringBuffer(String str) 构造一个初始化为指定字符串内容的StringBuffer对象

 

package com.wangxing.stringbuffer;

public class StringBuffer1 {

	public static void main(String[] args) {
		//StringBuilder()构造一个初始容量为16个字符的空StringBuilder对象
			StringBuffer b1=new StringBuffer();
		//StringBuffer(CharSequence seq)通过StringBuffer对象创建的StringBuffer对象
		//只要参数是CharSequence接口类型
		//CharSequence接口类型---String  
		//CharSequence接口类型---StringBuilder
		//CharSequence接口类型---StringBuffer
			StringBuffer b2=new StringBuffer(b1);
		//StringBuffer(int capacity)构造一个由capacity参数指定的初始容量得空StringBuffer对象
			StringBuffer b3=new StringBuffer(30);
		//StringBuilder(String str)构造一个初始化为指定字符串内容的StringBuffer对象
			StringBuffer b4=new StringBuffer("hello");

	}

}

      实例方法:

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() 返回表示此顺序中的数据的字符串。

7.9.String类与StringBuffer类之间的相互转换

     String转换成StringBuffer---》StringBuffer(String str)

     StringBuffer转换成String--》StringBuffer的toString()

                                                  String的构造方法String(StringBuffer buffer)

String、StringBuilder、StringBuffer的区别?

 

String

StringBuilder

StringBuffer

可变与否

不可变

可变

可变

线程安全与否

非线程安全

非线程安全

线程安全

运行速度

较快

操作的情况

少量的字符串操作[拼接]的情况

单线程下字符串操大量操作的情况

线程下字符串操大量操作的情况

时间日期的操作类

7.10.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.定日子

     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.calendar;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarTest {
	public static void main(String args[]){
		//Calendar类的对象不能用new+构造方法来创建
		//1.Calendar类是个抽象类
		//2.构造方法是protected修饰的,非子类无法直接使用
		//怎样创建Calendar对象?
		//通过Calendar类提供的静态方法获取
		//static Calendar getInstance()使用默认时区和区域设置获取日历
			Calendar c1=Calendar.getInstance();
		//常见的日历方法
		//1.查日历
			/*
			 * get(int field) 返回给定日历字段的值。 
				参数:int field--具体查询的问题
				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当月的周数。 
			 */
			System.out.println("年=="+c1.get(Calendar.YEAR));
			System.out.println("月=="+(c1.get(Calendar.MONTH)+1));//月份从0开始数
			System.out.println("日=="+c1.get(Calendar.DAY_OF_MONTH));
			System.out.println("时=="+c1.get(Calendar.HOUR_OF_DAY));
			System.out.println("分=="+c1.get(Calendar.MINUTE));
			System.out.println("秒=="+c1.get(Calendar.SECOND));
			System.out.println("一周中的日期==" 
            (c1.get(Calendar.DAY_OF_WEEK_IN_MONTH)-1));//星期是从星期天开始数
			System.out.println("一年中的日期=="+c1.get(Calendar.DAY_OF_YEAR));
			System.out.println("一年中的周数=="+c1.get(Calendar.WEEK_OF_YEAR));
			System.out.println("一月中的周数=="+c1.get(Calendar.WEEK_OF_MONTH));
		//2.定日子
		//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(year, month, date);
		//calendar.set(year, month, date, hourOfDay, minute);
		//calendar.set(int year, int month, int date, int hourOfDay, int minute, 
        //int second) 
			c1.set(2022,2,28);
			System.out.println("年=="+c1.get(Calendar.YEAR));
			System.out.println("月=="+(c1.get(Calendar.MONTH)+1));
			System.out.println("日=="+c1.get(Calendar.DAY_OF_MONTH));
		//getTime() 返回一个 Date表示此物体 Calendar的时间值(毫秒从偏移 Epoch “)。
			java.util.Date date=c1.getTime();
			System.out.println("date=="+date);
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			System.out.println("date=="+sdf.format(date));
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值