动手动脑3

一.请运行以下示例代码StringPool.java,查看其输出结果。如何解释这样的输出结果?


public class StringPool {

public static void main(String args[])
{

String s0="Hello";

String s1="Hello";

String s2="He"+"llo";

System.out.println(s0==s1);//true

System.out.println(s0==s2);//true

System.out.println(new String("Hello")==new String("Hello"));//false

}


}

输出结果:

输出结果解释:s0,s1,s2字符串的内容相同,实际占用的是同一空间,引用的是同一个对象,所以前两个为true。而第三个中。两个“Hello”分别开辟了新的空间,引用的不同的对象,所以为false。

二.整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

 Length()表示获取字符串长度。 charAt()表示获取指定位置的字符。 getChars() 表示获取从指定位置开始的字符串复制到字符数组中。

    replace()表示字符串替换。    toUpperCase()、 toLowerCase()表示大小写的转换。trim()表示去除头尾空格。  toCharArray()将字符串对象转换为字符数组。

 

原文地址:https://www.cnblogs.com/877612838zzx/p/7744472.html