不可忽视理解的String

Q:String a="“和String a=new String(”")的的关系和异同?

参考回答:
通过String a="“直接赋值的方式得到的是一个字符串常量,存在于常量池;注意,相同内容的字符串在常量池中只有一个,即如果池已包含内容相等的字符串会返回池中的字符串,反之会将该字符串放入池中
通过new String(”")创建的字符串不是常量是实例对象,会在堆内存开辟空间并存放数据,且每个实例对象都有自己的地址空间
引申:对于用String a="“和String a=new String(”")两种方式定义的字符串,判断使用equals()、"=="比较结果是什么

  • String s = new String(“hello”)和String s = “hello”;的区别?
    有。前者会创建2个对象,后者创建1个对象。(准确的讲前者会创建两个或者是一个,而后者会创建一个或0个,因为方法区如果有的话)
  • ==:比较引用类型比较的是地址值是否相同
    equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
String s1 = new String("hello");
String s2 = "hello";
System.out.println(s1 == s2);// false
System.out.println(s1.equals(s2));// true
		String s1 = new String("hello");
		String s2 = new String("hello");
		System.out.println(s1 == s2);// false
		System.out.println(s1.equals(s2));// true

		String s3 = new String("hello");
		String s4 = "hello";
		System.out.println(s3 == s4);// false
		System.out.println(s3.equals(s4));// true

		String s5 = "hello";
		String s6 = "hello";
		System.out.println(s5 == s6);// true
		System.out.println(s5.equals(s6));// true

  • 字符串如果是变量相加,先开空间,在拼接。
  • 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
       String s1 = "hello";
		String s2 = "world";
		String s3 = "helloworld";
		System.out.println(s3 == s1 + s2);// false
		System.out.println(s3.equals((s1 + s2)));// true

		System.out.println(s3 == "hello" + "world");// false 这个我们错了,应该是true
		System.out.println(s3.equals("hello" + "world"));// true

常见的方法
1 构造方法:
* 	public String():空构造
*	public String(byte[] bytes):把字节数组转成字符串
*	public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
*	public String(char[] value):把字符数组转成字符串
*	public String(char[] value,int index,int count):把字符数组的一部分转成字符串
*	public String(String original):把字符串常量值转成字符串
*
2 字符串的方法:
* 	public int length():返回此字符串的长度。
* 
3 String类的判断功能:
* boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串中是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
* boolean isEmpty():判断字符串是否为空。

4 String类的获取功能
* int length():获取字符串的长度。
* char charAt(int index):获取指定索引位置的字符
* int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
* 		为什么这里是int类型,而不是char类型?
* 		原因是:'a'和97其实都可以代表'a'
* int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
* int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
* int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
* String substring(int start):从指定位置开始截取字符串,默认到末尾。
* String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。


5 String的转换功能:
* byte[] getBytes():把字符串转换为字节数组。                   byte类型是一种-128~+128的数字
* char[] toCharArray():把字符串转换为字符数组。                是一种……
* static String valueOf(char[] chs):把字符数组转成字符串。
* static String valueOf(int i):把int类型的数据转成字符串。
* 		注意:String类的valueOf方法可以把任意类型的数据转成字符串。          那如果是这样的话,还需要前面的方法吗?是不是多余了?
* String toLowerCase():把字符串转成小写。
* String toUpperCase():把字符串转成大写。
* String concat(String str):把字符串拼接。                            ---有没有什么缺点没有??

6 String类的其他功能:
* 
* 替换功能:
* String replace(char old,char new)
* String replace(String old,String new)
*
* 去除字符串两空格	
* String trim()
* 
* 按字典顺序比较两个字符串  
* int compareTo(String str)
* int compareToIgnoreCase(String str)
*/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值