java知识2-----核心1-面向对象基础-----续

本文详细介绍了Java中String类的12种常用方法,包括字符数组与字符串互转、byte数组转换、字符提取、长度计算、查找子串、去除空格、截取、拆分、大小写转换、字符串比较和替换。此外,还讨论了引用传递的三种范例和this关键字的用途,如构造方法调用和表示当前对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String类的常用方法

查询jdk-API文档是最权威的使用方式,java提供的各种方法、接口都做了详细的说明。
1,字符数组与字符串的互转:

public class StringAPIDemo01{
	public static void main(String args[]){
		String str1 = "hello" ;			// 定义字符串
		char c[] = str1.toCharArray() ;	// 将一个字符串变为字符数组
		for(int i=0;i<c.length;i++){	// 循环输出
			System.out.print(c[i] + "、") ; 
		}
		System.out.println("") ;		// 换行
		String str2 = new String(c) ;	// 将全部的字符数组变为String
		String str3 = new String(c,0,3) ;	// 将部分字符数组变为String
		System.out.println(str2) ;		// 输出字符串
		System.out.println(str3) ;		// 输出字符串
	}
};

2,byte数组与字符串互转:

public class StringAPIDemo03{
	public static void main(String args[]){
		String str1 = "hello" ;			// 定义字符串
		byte b[] = str1.getBytes() ;	// 将字符串变为byte数组
		System.out.println(new String(b)) ;	// 将全部的byte数组变为字符串
		System.out.println(new String(b,1,3)) ;	// 将部分的byte数组变为字符串
	}
};

3,从一个字符串中取出指定字符:

public class StringAPIDemo02{
	public static void main(String args[]){
		String str1 = "hello" ;			// 定义String对象
		System.out.println(str1.charAt(3)) ;	// 取出字符串中第四个字符
	}
};

4,取得字符串长度:

public class StringAPIDemo04{
	public static void main(String args[]){
		String str1 = "hello jakezhang" ;		// 定义字符串变量
		System.out.println("\""+str1+"\"的长度为:"+str1.length()) ;
	}
};

5,从头开始查找,或者从指定位置开始查找,字符串中指定字符的位置:

public class StringAPIDemo05{
	public static void main(String args[]){
		String str1 = "abcdefgcgh" ;				// 声明字符串
		System.out.println(str1.indexOf("c")) ;		// 查到返回位置
		System.out.println(str1.indexOf("c",3)) ;	// 查到返回位置,从第4个位置开始查找
		System.out.println(str1.indexOf("x")) ;		// 没有查到返回-1
	}
};

6,清除左右两端空格:

public class StringAPIDemo06{
	public static void main(String args[]){
		String str1 = "    hello    " ;		// 定义字符串
		System.out.println(str1.trim()) ;	// 去掉左右空格后输出
	}
};

7,截取字符串:

public class StringAPIDemo07{
	public static void main(String args[]){
		String str1 = "hello world" ;		// 定义字符串
		System.out.println(str1.substring(6)) ; // 从第7个位置开始截取
		System.out.println(str1.substring(0,5)) ; // 截取0~5个位置的内容
	}
};

8,字符串拆分:

public class StringAPIDemo08{
	public static void main(String args[]){
		String str1 = "hello world" ;		// 定义字符串
		String s[] = str1.split(" ") ;		// 按空格进行字符串的拆分
		for(int i=0;i<s.length;i++){		// 循环输出
			System.out.println(s[i]) ;
		}
	}
};

9,大小写转换:

public class StringAPIDemo09{
	public static void main(String args[]){
		System.out.println("将\"hello world\"转成大写:" + "hello world".toUpperCase()) ;
		System.out.println("将\"HELLO WORLD\"转成小写:" + "HELLO WORLD".toLowerCase()) ;
	}
};

10,判断字符串是否以xx开头,是否以xx结尾:

public class StringAPIDemo10{
	public static void main(String args[]){
		String str1 = "**HELLO" ;			// 定义字符串
		String str2 = "HELLO**" ;			// 定义字符串
		if(str1.startsWith("**")){			// 判断是否以“**”开头
			System.out.println("(**HELLO)以**开头") ;
		}
		if(str2.endsWith("**")){			// 判断是否以“**”结尾
			System.out.println("(HELLO**)以**结尾") ;
		}
	}
};

11,字符串不区分大小写比较:

public class StringAPIDemo11{
	public static void main(String args[]){
		String str1 = "HELLO" ;			// 定义字符串
		String str2 = "hello" ;			// 定义字符串
		System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;
		System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "
				+ str1.equalsIgnoreCase(str2)) ;	// 不区分大小写的比较
	}
};

12,替换字符串中的指定字符:

public class StringAPIDemo12{
	public static void main(String args[]){
		String str = "hello" ;			// 定义字符串
		String newStr = str.replaceAll("l","x")	;	// 现在将所有的l替换成x
		System.out.println("替换之后的结果:" + newStr) ;
	}
};

引用传递及其应用

三种引用传递的范例。

1,

class Demo{
	int temp = 30 ;		// 此处为了方便,属性暂时不封装
};
public class RefDemo01{
	public static void main(String args[]){
		Demo d1 = new Demo() ;	// 实例化Demo对象,实例化之后里面的temp=30 
		d1.temp = 50 ;		// 修改temp属性的内容
		System.out.println("fun()方法调用之前:" + d1.temp) ;   // 50
		fun(d1) ;
		System.out.println("fun()方法调用之后:" + d1.temp) ;   // 1000
	}
	public static void fun(Demo d2){		// 此处的方法由主方法直接调用,所以方法要加static
		d2.temp = 1000;						// 修改temp值
	}
};

在这里插入图片描述

2,

public class RefDemo02{
	public static void main(String args[]){
		String str1 = "hello" ;			// 实例化字符串对象
		System.out.println("fun()方法调用之前:" + str1) ;  // hello
		fun(str1) ;						// 调用fun()方法
		System.out.println("fun()方法调用之后:" + str1) ;  // 修改没有被保留,依然打印的是hello
	}
	public static void fun(String str2){		// 此处的方法由主方法直接调用
		str2 = "MLDN" ;					// 修改字符串内容
	}
};

在这里插入图片描述
根因分析:
再次强调重点:String是一个特殊的类,其内容不可改变。

3,

class Demo{
	String temp = "hello" ;		// 此处为了方便,属性暂时不封装
};
public class RefDemo03{
	public static void main(String args[]){
		Demo d1 = new Demo() ;	// 实例化Demo对象,实例化之后里面的temp=30 
		d1.temp = "world" ;		// 修改temp属性的内容
		System.out.println("fun()方法调用之前:" + d1.temp) ; // world
		fun(d1) ;
		System.out.println("fun()方法调用之后:" + d1.temp) ; // 修改被保留了下来,打印 MLDN
	}
	public static void fun(Demo d2){		// 此处的方法由主方法直接调用
		d2.temp = "MLDN";						// 修改temp值
	}
};

在这里插入图片描述

4,
接收本类引用:

class Demo{						// 定义Demo类
	private int temp  = 30 ;	// 声明temp属性并封装
	public void fun(Demo d2){	// 接收本类的引用,这样相当于跳过了封装
		d2.temp = 150 ;			// 直接通过对象调用本类的私有属性
	}
	public int getTemp(){		// getter
		return temp ;
	}
	public void setTemp(int t){	// setter
		temp = t ;
	}
};
public class RefDemo04{
	public static void main(String args[]){
		Demo d1 = new Demo() ;	// 实例化Demo对象
		d1.setTemp(50) ;		// 只能通过setter方法修改内容
		d1.fun(d1) ;			// 此处把Demo的对象传回到自己的类中
		System.out.println("temp = " + d1.getTemp()) ; // 打印150
	}
};

结论:
只要符合引用传递的语法,就可以向任意的地方传递。

this关键字

this关键字的作用

使用this强调本类中的方法,除此之外,this还有如下作用:

  1. 表示类中的属性;
class Person{		// 定义Person类
	private String name ;		// 姓名
	private int age ;			// 年龄
	public Person(String name,int age){	// 通过构造方法赋值
		this.name = name ; // 为类中的name属性赋值
		this.age = age ;// 为类中的age属性赋值
	}
	public String getInfo(){	// 取得信息的方法
		return "姓名:" + name + ",年龄:" + age ;
	}
};
public class ThisDemo02{
	public static void main(String args[]){
		Person per1 = new Person("张三",33) ;	// 调用构造实例化对象
		System.out.println(per1.getInfo()) ;	// 取得信息
	}
};
  1. 可以使用this调用本类的构造方法
class Person{		// 定义Person类
	private String name ;		// 姓名
	private int age ;			// 年龄
	public Person(){	// 无参构造
		System.out.println("新对象实例化") ;
	}
	public Person(String name){
		this() ;// 调用本类中的无参构造方法
		this.name = name ;
		// this() ;// 调用本类中的无参构造方法  this必须放在首行,否则报错
	}
	public Person(String name,int age){	// 通过构造方法赋值
		this(name) ;// 调用有一个参数的构造方法
		this.age = age ;// 为类中的age属性赋值
	}
	public String getInfo(){	// 取得信息的方法\
		// this() ;// 调用本类中的无参构造方法,非构造方法中不能调用构造
		return "姓名:" + name + ",年龄:" + age ;
	}
};
public class ThisDemo04{
	public static void main(String args[]){
		Person per1 = new Person("张三",33) ;	// 调用构造实例化对象
		System.out.println(per1.getInfo()) ;	// 取得信息
	}
};
  1. this表示当前对象
class Person{		// 定义Person类
	public String getInfo(){	// 取得信息的方法
		System.out.println("Person类 --> " + this) ; // 直接打印this
		return null ; // 为了保证语法正确,返回null
	}
};
public class ThisDemo06{
	public static void main(String args[]){
		Person per1 = new Person() ;	// 调用构造实例化对象
		Person per2 = new Person() ;	// 调用构造实例化对象
		System.out.println("MAIN方法 --> " + per1) ;	// 直接打印对象
		per1.getInfo() ;	// 当前调用getInfo()方法的对象是per1
		System.out.println("MAIN方法 --> " + per2) ;	// 直接打印对象
		per2.getInfo() ;	// 当前调用getInfo()方法的对象是per2
	}
};

两个对象比较,属性值相同时认为是两个相同的对象:

class Person{		// 定义Person类
	private String name ;	// 姓名
	private int age ;		// 年龄
	public Person(String name,int age){
		this.setName(name) ;
		this.setAge(age) ;
	}
	public boolean compare(Person per){
		// 调用此方法时里面存在两个对象:当前对象、传入的对象
		Person p1 = this ;	// 当前的对象,就表示per1
		Person p2 = per ;	// 传递进来的对象,就表示per2
		if(p1==p2){	// 判断是不是同一个对象,用地址比较
			return true ;
		}
		// 之后分别判断每一个属性是否相等
		if(p1.name.equals(p2.name)&&p1.age==p2.age){
			return true ;	// 两个对象相等
		}else{
			return false ;	// 两个对象不相等
		}
	}
	public void setName(String name){	// 设置姓名
		this.name = name ;
	}
	public void setAge(int age){		// 设置年龄
		this.age = age ;
	}
	public String getName(){
		return this.name ;
	}
	public int getAge(){
		return this.age ;
	}
};
public class ThisDemo08{
	public static void main(String args[]){
		Person per1 = new Person("张三",30) ;	// 声明两个对象,内容完全相等
		Person per2 = new Person("张三",30) ;	// 声明两个对象,内容完全相等
		// 直接在主方法中依次取得各个属性进行比较
		if(per1.compare(per2)){
			System.out.println("两个对象相等!") ;
		}else{
			System.out.println("两个对象不相等!") ;
		}
	}
};

使用this关键字调用其他构造方法

注意点:
1,this调用构造方法的语句只能放在构造方法的首行,否则编译会报错。
2,在使用this调用本类中的其他构造的时候,至少有一个构造方法是不用this调用的。构造方法不能出现递归调用。

使用this表示当前对象

1、this.属性、this.方法,实际上都表示当前的对象中的属性或者当前对象调用的方法。
2、this的核心:表示当前对象。当前正在操作本方法的对象称为当前对象。
3、使用this可以调用其他构造方法,但是次语句必须放在构造方法的首行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值