Java中的String字符串对象

package lesson;
/*本代码,在eclipse下运行,请自行打断点,查看堆栈分配
 * 2022.04.17
字符串 它的真正的实例,是通过字符数组去实现的
字符串有一个很明显或者说很重要的特点:你创建了,他就不能修改了
字符串保存在【常量池】

我们今天主要是聊一聊,字符串,常用的方法

String 是一个对象

1 String是不属于8 中基本数据类型的

2 JAVA的8中基本数据类型:基本数据类型分为三类
	字符串char、
	布尔型boolean、
	数值型byte、short、int、long、float、double

数值类型又分为两种,
	整数型:int、short、long
	浮点型:float、double

3【3】 String类在java.lang包中,java使用String类创建一个字符串变量,
字符串变量属性对象。String类对象创建后不能修改


1 对象的默认值是null,所以String的默认值也是null。
但它又是一种独特的对象,有自己图特的一些性质
2 new String() 和 new String(""); 【都是声明一个空串】,要注意声明的【是空串】 而 不是null
	String类大量的拼接,会导致内存资源浪费。
3 如果需要对字符串做很多修改,那么应该选择使用StringBuffer & StringBuilder 类。

执行速度上StringBuilder > StringBuffer > String
StringBuilder是非线程安全的,StringBuffer(jdk1.6之后)是线程安全的


Java中字符串对象创建有两种形式:
一种为字面量形式,如String str1 = "Hello";
另一种就是使用new这种标准的构造对象的方法,如String s7 = new String("Hello");
这两种方式我们在代码编写时都经常使用,尤其是字面量的方式。
然而这两种实现其实存在着一些性能和内存占用的差别。
这一切都是源于JVM为了减少字符串对象的重复创建,其维护了一个特殊的内存,
这段内存被成为字符串常量池或者字符串字面量池。

*/

public class Num03 {
	public static void main(String[] args) {
		
		String str1 = "Hello";
		String str2 = "HelloWorld";
		int a = 1;
		int b = 2;
		Integer c = new Integer(1);
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		
		String str3 = "";
		String str4 = "Hello";
		
		
		//----------------------------
	
		String str5 = "jcc";
		String str6 = new String();
		System.out.println(str6);
		System.out.println("+"+str6+"+");

		//-------"" 与  null 是不一样的,完全不同的概念---------------------

		str6 = "";
		str6 = null;
		System.out.println(str6);

		//----------------------------
//当代码中出现字面量形式创建字符串对象时,JVM首先会对这个字面量进行检查,如果字符串常量池中存在相同内容的字符串对象的引用,
//则将这个引用返回,否则新的字符串对象被创建,然后将这个引用放入字符串常量池,并返回该引用。所以我们看堆空间,str1 和 str4 valueid相同
		String str7 = "Hello";
		String str8 = "World";
		System.out.println(str7 + str8);


		
//	 	将每一句打上断点,debug单步运行,我们可以看到:
//		不管是String str7 ="Hello",还是String s7 = new String("Hello")
//		他们都是分配在对堆上面的(都有id,而int 类型的num1\num2没有id)

		
//当我们使用了new来构造字符串对象的时候,不管字符串常量池中有没有相同内容的对象的引用,新的字符串对象都会创建。
		String s7 = new String("Hello");
		System.out.println("str7 和 s7 ==对比:"+ (str7 ==s7));
		int num1 = 1;
		Integer num2 = new Integer(1);
		System.out.println(num1);
		System.out.println(num2);
		System.out.println("---------------");
		System.out.println(str4 == str7);
//str4 == str7 为 true,是因为执行str4="Hello"这条语句,
//系统系统先检测了存在value为“Hello”的这个地址空间(id=xx),
//为了节省空间,也就不再为str7重新new一块区域,
//而是直接指向str4所在堆区间,所以str4和str7的id相等。		
		
		//---------下面两个是可以修改的-------------------
	
		StringBuilder sbuilder = new StringBuilder();
		sbuilder.append("World");
		System.out.println(sbuilder.toString());
		
		StringBuffer sbuffer = new StringBuffer("Hello");
		sbuffer.append("World");
		System.out.println(sbuffer.toString());
		
		//--------String类的【常用方法】--------------------
//		1、int length(); 语法:字符串变量名.length();	返回值为int类型
//			得到一个字符串的字符个数(中、英、空格、转义字符皆为字符,计入长度)
		String i = new String("wo是个好ren");
		System.out.println(i.length());	//8
		String id = "41138119990307037x";
		if(id.length() != 18) {
			System.out.println("您的身份在号码有误");
		}else {
			System.out.println(id.length()+" 身份验证通过");
		}

		//--------String类的【常用方法】--------------------
//		2、char charAt(值);	语法:字符串名.charAt(值);	
//			返回值为char类型。从字符串中取出指定位置的字符
		//在一个字符型的数组里面存了这一串的东西
		char[] ic = {'我','是','个','好','人'};	//字符型数组
		System.out.println(ic);
		
		char j = i.charAt(2);
		System.out.println(j);

		//--------String类的【常用方法】--------------------
//		3、char toCharArray();	语法:字符串名.toCharArray();	
//			返回值为char数组类型。	将字符串变成一个字符串数组
		
		
		String str = "我是个好人";
		char[] i3 = str.toCharArray();	//这个方法返回的是一个char类型的数组,所以我们用char类型的数组进行接收
		System.out.println("转换为数组输出:");
		for(int z = 0; z<i3.length; z++) {
			System.out.print(i3[z]+ "-");
		}
		//通过这个方法,我们就可以取出字符串中单独一个字符,toCharArray(),不字符串变成一个字符数组

		//--------String类的【常用方法】--------------------
//		4、int indexOf("字符");	语法:字符串名.indexOf("字符");		
//			字符串名.indexOf("字符", 值); 查找一个指定的字符串是否存在,
//			返回的是字符串的位置, 如果不存在, 则返回-1
//			int lastIndexOf("字符") 得到指定内容最后一次出现的下标
		String st4 = "我是个好人我是个好人";
		int index1 = st4.indexOf("是");		//查找字符串的位置
		System.out.println("是的位置为:" + index1);	//1
		int index2 = st4.indexOf("好",4);		//打个逗号,加上一个数字;这又是什么意思呢?;查找字符串的位置,从第5个位置开始
		System.out.println("好的位置为:" + index2);	//3
		int index3 = st4.lastIndexOf("好");	
		System.out.println("最后出现的位置为:"+ index3);

		//--------String类的【常用方法】--------------------
//		5、toUpperCase();	toLowerCase(); 	字符串大小写的转换
		String st5 = "Hello World";
		System.out.println("将字符串转换成大写为:"+ st5.toUpperCase());
		System.out.println("将字符串转换成小写为:"+ st5.toLowerCase());
		System.out.println("将字符串转换成小写为:"+ st5.toUpperCase().toLowerCase());

		//--------String类的【常用方法】--------------------
//		6、String[] split("字符");	根据给定的正则表达式的匹配来【分割】此字符串。
//			形成一个新的String数组。		
		String st6 = "zhangsan:lisi:wangwu";
		String[] arr1 = st6.split(":");	//split里面放的是什么呢?里面放的就是你想要【分割】的字符,比如把冒号作为分割的依据
		String[] arr2 = st6.split("w");
		for(int i6 = 0; i6 < arr1.length; i6++) {
			System.out.print(arr1[i6] + "-");	//加一个-号分隔符
		}
		System.out.println("\n" + arr1.length);
		for(int i6 = 0; i6 < arr2.length; i6++) {
			System.out.print(arr2[i6] + "-");
		}
		System.out.println("\n" + arr2.length);

		//--------String类的【常用方法】--------------------
//		7、boolean equals(Object anObject)	语法:字符串变量名.equals(字符串变量名);
//			返回值为布尔类型。所以这里用if演示。
//			equals【比较两个字符串(具体的值)是否相等】,返回布尔值。	
//			boolean equalslgnoreCase(String)忽略大小写,比较两个字符串的值是否一模一样,返回一个布尔值	
		//【等号是地址比较】
		String word1 = "hello";
		String word2 = "world";
		String word3 = "hello";
		String word4 = new String("hello");
		//注意【word5\6\7】 的地址是不一样的
		String word5 = word1+word2;
		String word6 = "helloworld";
		String word7 = new String("helloworld");

		if(word2.equals(word1)) {
			System.out.println("这两个字符串值【相等】");
		}else {
			System.out.println("这两个字符串的值【不相等】");
		}
		if(word1 == word3) {
			System.out.println("这两个字符串值【相等】");
		}else {
			System.out.println("这两个字符串的值【不相等】");
		}
		if(word1 == word4) {
			System.out.println("这两个字符串值【相等】");
		}else {
			System.out.println("这两个字符串的值【不相等】");
		}	
		
		System.out.println(word5 == word6);
		System.out.println(word5.equals(word6));

		System.out.println(word5 == word7);
		System.out.println(word5.equals(word7));	
	
		//--------String类的【常用方法】--------------------
//		8、String substring(int beginIndex, int endIndex)	截取字符串
//			包含【一个参数和两个参数】的方法 返回字符串。	
		String st8 = "我是个喜欢猫的好人";
		System.out.println("截取后字符为:" + st8.substring(2));	//个喜欢猫的好人; 从第3个位置开始截取,包含2(0、1、2第三个位置)
			//当然也可以截取前面的,比如说我们从0开始,截取到3,它是怎么样的结果呢?
		System.out.println("截取后的字符为:" + st8.substring(0, 3));	//我是个;截取0-3个位置的内容,不包含3

		//--------String类的【常用方法】--------------------
//		9、boolean contains(String)	判断一个【字符串】里面【是否包含指定的内容】,返回一个布尔值
		String word = "wo shi ge xi huan mao de ren";
		String st09 = "xi";
		if(word.contains(st09)) {
			System.out.println("文本内容中存在:"+st09);
		}else {
			System.out.println("Sorry,文本内容不存在");
		}

		//--------String类的【常用方法】--------------------
//		10、replace(char oldChar, char newChar);	新字符替换旧字符,
//			也可以达到去空格的效果。
		String cont = "wo shi ge xi huan mao de ren";
	//字符串连写是一个比较特色的地方,一个对象【点】什么什么【再点】什么什么;
		String newCont = cont.replace("xi huan", "love").replace("ren", "shua ge");
		System.out.println("替换后的cont为:"+ newCont);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值