1.请运行以下示例代码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 } }

程序运行结果:

结论:

1、在Java中,内容相同的字符常量只保存一份以节省内存,所以s0,s1,s2实际上引用的是同一个对象。

2、编译器在编译s2一句时,会去掉“+”号,直接把两个字串连接起来得一个字串。这种优化工作由Java编译器自动完成。

3、当直接使用new关键字创建字符串对象时,虽然值一致,但仍然是两个独立的对象。

再看……

 public class Test {

     public static void main(String[] args) {

         String s1 = "a";

         String s2 = s1;

         System.out.println(s1 == s2);

         s1 += "b";

         System.out.println(s1 == s2);

         System.out.println(s1 == "ab");

         System.out.println(s1.equals("ab"));

     }

 }

程序运行结果:

为什么会有上述的输出结果?从中你又能总结出什么?

1、给字串变量赋值意味着:两个变量(s1,s2)现在引用同一个字符串对象。

2、String对象的内容是只读的,使用“+”修改s1变量的值,实际上是得到了一个新的字符串对象,其内容为“ab”,它与原先s1所引用的对象“a”

无关,所以s1==s2返回false;

3、代码中的“ab”字符串是一个常量,它所引用的字符串与s1所引用的“ab”对象无关。

4、String.equals()方法可以比较两个字符串的内容。

2、请查看String.equals()方法的实现代码,注意学习其实现方法。

String.equals()源代码:

 public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

由代码可知,这个函数首先判断这两个对象是否引用同一个字符串对象,如果是直接比较是否相同,如果不是,则比较两个对象中的字符是否相等。而且比较的方式是比较单个字符。

3、整理String类的length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明。

(1)、int length():返回字符串的长度。

例:

 public class Test {

     public static void main(String[] args) {

         String s1 = new String("hello world!");

         String s2 = new String("hello");

         System.out.println("s1的长度为:"+s1.length());

         System.out.println("s2的长度为:"+s2.length());

     }

 }

执行结果:

(2)、 char charAt(int index):取字符串中的某一个字符,其中的参数index值得是字符串中序数。字符串的序数从0开始到length()-1。

例:

 public class Test {

     public static void main(String[] args) {

         String s1 = new String("hello world!");

         System.out.println("排在s1的第7位的字符为:"+s1.charAt(6));

     }

 }

执行结果:

(3)、void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin):将字符从字符串复制到目标字符数组。

  • srcBegin -- 字符串中要复制的第一个字符的索引。

  • srcEnd -- 字符串中要复制的最后一个字符之后的索引。

  • dst -- 目标数组。

  • dstBegin -- 目标数组中的起始偏移量。

  • 无返回值,但会抛出IndexOutOfBoundsException异常

例:

 public class Test {

     public static void main(String[] args) {

         String s1 = new String("hello world!");

         char s2[] = new char[10];

         s1.getChars(6, 12, s2, 0);

         System.out.println(s2);

     }

 }

执行结果:

(4)、String replace(char oldChar,char newChar):将字符串中的所有oldChar替换成newChar

例:

 public class Test {

     public static void main(String[] args) {

         String s1 = new String("hello world!");

         String s2 = s1.replace('o','a');

         System.out.println("修改之后的字符串为:"+s2);

     }

 }

执行结果:

(5)、String toUpperCase():全部字符变为大写,返回新字符串。

例:

 public class Test {

     public static void main(String[] args) {

         String s1 = new String("hello world!");

         System.out.println("修改之后的字符串为:"+s1.toUpperCase());

     }

 }

执行结果:

(6)、String toLowerCase():全部字符变为小写,返回新字符串。

例:

 public class Test {

     public static void main(String[] args) {

         String s1 = new String("HELLO WORLD!");

         System.out.println("修改之后的字符串为:"+s1.toLowerCase());

     }

 }

执行结果:

(7)、String trim():去掉字符串首尾的空格。

例:

 public class Test {

     public static void main(String[] args) {

         String s1 = new String("   hello world!    ");

         System.out.println("修改之后的字符串为:"+s1.trim());

     }

 }

执行结果:

(8)、char [] toCharArray():将该String对象转换成char数组。

例:

 public class Test {

     public static void main(String[] args) {

         String s1 = new String("hello world!");

         char s2[] = new char[12];

         s2 = s1.toCharArray();

         for(int i = 0 ; i < 12 ; i ++){

             System.out.print(s2[i]+" ");

         }

     }

 }

执行结果:

最新文章

  1. Lucene4.1 视频学习
  2. 2016HUAS暑假集训训练2 O - Can you find it?
  3. 手动安装 atom 扩展包 packages
  4. Android adt v22.6.2 自动创建 appcompat_v7 解决方法,最低版本2.2也不会出现
  5. autorelease应用
  6. MVC 自定义过滤器/特性来实现登录授权及验证
  7. NodeJS学习历程 - (一)工具篇
  8. HDU 1025 (LIS+二分) Constructing Roads In JGShining&#39;s Kingdom
  9. SHell命令总结
  10. Emacs快捷键列表
  11. 使用模板类导致error LNK2019: 无法解析的外部符号
  12. MySQL各种日期类型与整型(转)
  13. xtrabackup在线备份及还原
  14. 《Mastering Opencv ...读书笔记系列》车牌识别(I)
  15. FreeRTOS 启动进程调度后,程序卡死的部分原因分析。
  16. iptables nat 技术转发
  17. Python—函数的名称空间
  18. LeetCode(112):路径总和
  19. 微信小程序的界面下拉刷新
  20. eclips环境下开发spring boot项目,application.properties配置文件下中文乱码解决方案

热门文章

  1. C语言数组成绩排序
  2. 中间件kingshard入门(一):基本安装
  3. 关于python中format占位符中的 {!} 参数
  4. 最新版本GIT安装
  5. Vue项目中v-for无法渲染数据
  6. C++面试常见问题——12虚函数
  7. Docker基础——从入门到精通
  8. 037、Java中利用判断语句实现三目运算的功能
  9. PHP: isset与empty的区别
  10. yolov3.cfg参数解读