微信登录

字符串 - ""

  1. package com.atguigu.java;
  2. import org.junit.Test;
  3. /**
  4. * String的使用
  5. *
  6. * @author shkstart
  7. * @create 2019 上午 10:26
  8. */
  9. public class StringTest {
  10. /*
  11. 结论:
  12. 1.常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
  13. 2.只要其中有一个是变量,结果就在堆中。
  14. 3.如果拼接的结果调用intern()方法,返回值就在常量池中
  15. */
  16. @Test
  17. public void test4(){
  18. String s1 = "javaEEhadoop";
  19. String s2 = "javaEE";
  20. String s3 = s2 + "hadoop";
  21. System.out.println(s1 == s3);//false
  22. final String s4 = "javaEE";//s4:常量
  23. String s5 = s4 + "hadoop";
  24. System.out.println(s1 == s5);//true
  25. }
  26. @Test
  27. public void test3(){
  28. String s1 = "javaEE";
  29. String s2 = "hadoop";
  30. String s3 = "javaEEhadoop";
  31. String s4 = "javaEE" + "hadoop";
  32. String s5 = s1 + "hadoop";
  33. String s6 = "javaEE" + s2;
  34. String s7 = s1 + s2;
  35. System.out.println(s3 == s4);//true
  36. System.out.println(s3 == s5);//false
  37. System.out.println(s3 == s6);//false
  38. System.out.println(s3 == s7);//false
  39. System.out.println(s5 == s6);//false
  40. System.out.println(s5 == s7);//false
  41. System.out.println(s6 == s7);//false
  42. String s8 = s6.intern();//返回值得到的s8使用的常量值中已经存在的“javaEEhadoop”
  43. System.out.println(s3 == s8);//true
  44. }
  45. /*
  46. String的实例化方式:
  47. 方式一:通过字面量定义的方式
  48. 方式二:通过new + 构造器的方式
  49. 面试题:String s = new String("abc");方式创建对象,在内存中创建了几个对象?
  50. 两个:一个是堆空间中new结构,另一个是char[]对应的常量池中的数据:"abc"
  51. */
  52. @Test
  53. public void test2(){
  54. //通过字面量定义的方式:此时的s1和s2的数据javaEE声明在方法区中的字符串常量池中。
  55. String s1 = "javaEE";
  56. String s2 = "javaEE";
  57. //通过new + 构造器的方式:此时的s3和s4保存的地址值,是数据在堆空间中开辟空间以后对应的地址值。
  58. String s3 = new String("javaEE");
  59. String s4 = new String("javaEE");
  60. System.out.println(s1 == s2);//true
  61. System.out.println(s1 == s3);//false
  62. System.out.println(s1 == s4);//false
  63. System.out.println(s3 == s4);//false
  64. System.out.println("***********************");
  65. Person p1 = new Person("Tom",12);
  66. Person p2 = new Person("Tom",12);
  67. System.out.println(p1.name.equals(p2.name));//true
  68. System.out.println(p1.name == p2.name);//true
  69. p1.name = "Jerry";
  70. System.out.println(p2.name);//Tom
  71. }
  72. /*
  73. String:字符串,使用一对""引起来表示。
  74. 1.String声明为final的,不可被继承
  75. 2.String实现了Serializable接口:表示字符串是支持序列化的。
  76. 实现了Comparable接口:表示String可以比较大小
  77. 3.String内部定义了final char[] value用于存储字符串数据
  78. 4.String:代表不可变的字符序列。简称:不可变性。
  79. 体现:1.当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值。
  80. 2. 当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
  81. 3. 当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
  82. 5.通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。
  83. 6.字符串常量池中是不会存储相同内容的字符串的。
  84. */
  85. @Test
  86. public void test1(){
  87. String s1 = "abc";//字面量的定义方式
  88. String s2 = "abc";
  89. s1 = "hello";
  90. System.out.println(s1 == s2);//比较s1和s2的地址值,true的,因为2个"abc"共用方法区(含字符串常量池)
  91. System.out.println(s1);//hello
  92. System.out.println(s2);//abc
  93. System.out.println("*****************");
  94. String s3 = "abc";
  95. s3 += "def";
  96. System.out.println(s3);//abcdef
  97. System.out.println(s2);
  98. System.out.println("*****************");
  99. String s4 = "abc";
  100. String s5 = s4.replace('a', 'm');
  101. System.out.println(s4);//abc
  102. System.out.println(s5);//mbc
  103. }
  104. }
字符串 - ""