当前位置:   article > 正文

Java String类_"现有两个string变量,分别为 teststring=\"hello\",teststring2

"现有两个string变量,分别为 teststring=\"hello\",teststring2=\"world\",要得到h"

String类的特点

  1. /*
  2. * java.lang.String:
  3. * 1、特点
  4. * (1)String类型不能被继承,因为String是由final修饰
  5. * (2)String类型的对象是不可变
  6. * 换句话说,只要修改字符串,就会产生新对象
  7. * (3)String对象不可变的特性,使得我们可以把一些字符串存到常量池中,
  8. * 字符串有常量池。常量池中的是可以共享的。
  9. *
  10. * 字符串常量池在哪里?Oracle官方虚拟机HotSpot
  11. * (1)JDK1.6以及之前:方法区
  12. * (2)JDK1.7:挪到堆中,即在堆中单独划分了一块来存字符串常量
  13. * (3)JDK1.8:从堆中挪出,挪到一个“元空间meta space”,即类似于方法区
  14. *
  15. * (4)String对象底层的存储
  16. * JDK1.9之前:底层是用char[]存储
  17. * JDK1.9之后:底层选用byte[]存储
  18. *
  19. * (5)String对象怎么就不可变
  20. * ①底层char[]数组有final修饰,意味着这个数组不能扩容等,来达到存更多的字符
  21. * ②char[]数组是私有的,我们程序员无法直接操作这个char[]数组,而且String没有提供这样的方法,来修改char[]数组的元素的值。
  22. * String提供的所有的方法,对字符串的修改都是给你返回一个新的字符串对象。
  23. */
  24. public class TestString01 {
  25. @Test
  26. public void test02(){
  27. String s1 = "hello";
  28. String s2 = "hello";
  29. System.out.println(s1 == s2);
  30. }
  31. @Test
  32. public void test01(){
  33. /* String s1 = "hello";
  34. s1 = "world";
  35. s1 = s1 + "java";*/
  36. String s = "";
  37. change(s);
  38. System.out.println(s);
  39. }
  40. public void change(String str){
  41. str = "hello";
  42. }
  43. }

String对象的比较

  1. import java.text.Collator;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.Locale;
  5. import org.junit.Test;
  6. /*
  7. * 2、字符串的比较
  8. * (1)==:比较对象的地址
  9. * 结论:只有两个字符串的常量对象比较时才会返回true,其他的都是false
  10. * (2)equals:比较字符串的内容,严格区分大小写
  11. * 因为String类型重写了Object的equals
  12. * (3)equalsIgnoreCase(String anotherString) :比较字符串内容,不区分大小写
  13. * (4)大小比较
  14. * String类型实现了Comparable接口,重写了compareTo方法,严格区分大小写
  15. * 依次比较对应位置的字符
  16. * hello和Hello,先[0]位置的h和H,h>H,就直接认定为hello>Hello
  17. * hello和hella,先[0][1][2][3]比较,都一样,最后到[4]o>a,认定hello>hella
  18. * hello和helloworld,发现前面都一样,长的大
  19. *
  20. * (5)大小比较:不区分大小写
  21. * String类型提供了一个方法compareToIgnoreCase,可以忽略大小写比较大小
  22. *
  23. * (6)按照每个国家的语言校对顺序
  24. * java.text.Collator:Collator 类执行区分语言环境的 String 比较。使用此类可为自然语言文本构建搜索和排序例程。
  25. * Collator实现了Comparator接口 ,
  26. * Collator是抽象类,不能直接创建对象,它有一个直接子类RuleBasedCollator
  27. * Collator内部提供了一个静态方法,可以获取一个它的子类对象
  28. *
  29. * 自然排序:实现java.lang.Comparable接口,int compareTo(Object obj)
  30. * 定制排序
  31. */
  32. public class TestString02 {
  33. @Test
  34. public void test12(){
  35. String[] arr = {"柴林燕","张三","李四","崔志恒","甄玉禄"};
  36. //希望按照拼音顺序,字典顺序
  37. Arrays.sort(arr, Collator.getInstance(Locale.CHINA));//Locale.CHINA指定语言环境
  38. System.out.println(Arrays.toString(arr));
  39. }
  40. @Test
  41. public void test11(){
  42. String[] arr = {"柴林燕","张三","李四","崔志恒","甄玉禄"};
  43. //希望按照拼音顺序,字典顺序
  44. Arrays.sort(arr, Collator.getInstance());//默认语言环境,因为我现在的操作系统的平台是中文win
  45. System.out.println(Arrays.toString(arr));
  46. }
  47. @Test
  48. public void test10(){
  49. String[] arr = {"柴林燕","张三","李四","崔志恒","甄玉禄"};
  50. Arrays.sort(arr);//按照自然顺序,按照每一个字符的Unicode编码值排序的
  51. System.out.println(Arrays.toString(arr));
  52. }
  53. @SuppressWarnings("all")
  54. @Test
  55. public void test09(){
  56. String[] arr = {"hello","chai","Java","Alice","Hi"};
  57. //排序
  58. //按照字母的顺序排列,不区分大小写
  59. Arrays.sort(arr,new Comparator(){
  60. @Override
  61. public int compare(Object o1, Object o2) {
  62. String s1 = (String) o1;
  63. String s2 = (String) o2;
  64. return s1.compareToIgnoreCase(s2);
  65. }
  66. });//按照元素的自然顺序排序
  67. System.out.println(Arrays.toString(arr));
  68. }
  69. @Test
  70. public void test08(){
  71. String[] arr = {"hello","chai","Java","Alice","Hi"};
  72. //排序
  73. //按照字母的顺序排列
  74. Arrays.sort(arr);//按照元素的自然顺序排序
  75. System.out.println(Arrays.toString(arr));
  76. }
  77. @Test
  78. public void test07(){
  79. String s1 = new String("hello");
  80. String s2 = new String("Hello");
  81. if(s1.compareToIgnoreCase(s2) > 0){
  82. System.out.println(s1 + ">" + s2);
  83. }else if(s1.compareToIgnoreCase(s2) < 0){
  84. System.out.println(s1 + "<" + s2);
  85. }else{
  86. System.out.println(s1 + "=" + s2);
  87. }
  88. }
  89. @Test
  90. public void test06(){
  91. String s1 = new String("hello");
  92. String s2 = new String("helloworld");
  93. /* if(s1 > s2){//不能直接使用比较运算符
  94. }*/
  95. if(s1.compareTo(s2) > 0){
  96. System.out.println(s1 + ">" + s2);
  97. }else if(s1.compareTo(s2) < 0){
  98. System.out.println(s1 + "<" + s2);
  99. }else{
  100. System.out.println(s1 + "=" + s2);
  101. }
  102. }
  103. @Test
  104. public void test05(){
  105. String s1 = new String("hello");
  106. String s2 = new String("hello");
  107. System.out.println(s1.equalsIgnoreCase(s2));//true
  108. String s3 = "hello";
  109. System.out.println(s3.equalsIgnoreCase(s1));//true
  110. String s4 = "Hello";
  111. System.out.println(s3.equalsIgnoreCase(s4));//true
  112. }
  113. @Test
  114. public void test04(){
  115. String s1 = new String("hello");
  116. String s2 = new String("hello");
  117. System.out.println(s1.equals(s2));//true
  118. String s3 = "hello";
  119. System.out.println(s3.equals(s1));//true
  120. String s4 = "Hello";
  121. System.out.println(s3.equals(s4));//false
  122. }
  123. @Test
  124. public void test03(){
  125. String s1 = new String("hello");
  126. String s2 = new String("hello");
  127. System.out.println(s1 == s2);//false
  128. }
  129. @Test
  130. public void test02(){
  131. String s1 = new String("hello");
  132. String s2 = "hello";
  133. System.out.println(s1 == s2);//false
  134. }
  135. @Test
  136. public void test01(){
  137. String s1 = "hello";
  138. String s2 = "hello";
  139. System.out.println(s1 == s2);//true
  140. }
  141. }

String对象个数、拼接结果等

  1. /*
  2. * 1、面试题
  3. * (1)String str = new String("hello");几个对象
  4. * (2)String str1 = new String("hello");
  5. String str2 = new String("hello");几个对象
  6. *
  7. * 2、拼接的结果在堆还是在常量池?
  8. * 因为只有常量池中才是共享,==比较才为true
  9. *
  10. * (1)常量 + 常量在常量池
  11. * (2)变量 +常量在堆
  12. * (3)变量 + 变量在堆
  13. * (4)xx.intern():在常量池
  14. *
  15. * 3、空字符串
  16. * (1)""
  17. * (2)new String()
  18. * (3)new String("")
  19. *
  20. * 四种方式:
  21. * (1)if(str != null && str.length() == 0)
  22. * (2)if(str != null && str.equals("")){
  23. * (3)if("".equals(str)) 推荐
  24. * (4)if(str!=null && str.isEmpty())
  25. */
  26. public class TestString03 {
  27. @Test
  28. public void test08(){
  29. String str = null;
  30. System.out.println(test(str));
  31. String str2 = "";
  32. System.out.println(test(str2));
  33. }
  34. //判断str是否是空字符串,是就返回true,不是返回false
  35. public boolean test(String str){
  36. if(str!=null && str.isEmpty()){
  37. return true;
  38. }
  39. return false;
  40. }
  41. /*public boolean test(String str){
  42. if("".equals(str)){//推荐
  43. return true;
  44. }
  45. return false;
  46. }*/
  47. /*public boolean test(String str){
  48. if(str != null && str.equals("")){
  49. return true;
  50. }
  51. return false;
  52. }*/
  53. /* public boolean test(String str){
  54. if(str != null && str.length() == 0){
  55. return true;
  56. }
  57. return false;
  58. }*/
  59. @Test
  60. public void test07(){
  61. String s1; //局部变量未初始化
  62. String s2 = null;//初始化null
  63. String s3 = "";//空字符串常量对象
  64. String s4 = new String();//空字符串对象
  65. String s5 = new String("");//两个对象,一个是常量池中的,一个是堆中
  66. // System.out.println(s1);//无法使用
  67. // System.out.println(s2.length());//空指针异常
  68. System.out.println(s3.length());
  69. System.out.println(s4.length());
  70. System.out.println(s5.length());
  71. }
  72. @Test
  73. public void test06(){
  74. String s1 = "hello";
  75. String s2 = "world";
  76. String s3 = "helloworld";
  77. String s4 = (s1 + "world").intern();//把拼接的结果放到常量池中
  78. String s5 = (s1 + s2).intern();
  79. System.out.println(s3 == s4);//true
  80. System.out.println(s3 == s5);//true
  81. }
  82. @Test
  83. public void test05(){
  84. final String s1 = "hello";
  85. final String s2 = "world";
  86. String s3 = "helloworld";
  87. String s4 = s1 + "world";//s4字符串内容也helloworld,s1是常量,"world"常量,常量+ 常量 结果在常量池中
  88. String s5 = s1 + s2;//s5字符串内容也helloworld,s1和s2都是常量,常量+ 常量 结果在常量池中
  89. String s6 = "hello" + "world";//常量+ 常量 结果在常量池中,因为编译期间就可以确定结果
  90. System.out.println(s3 == s4);//true
  91. System.out.println(s3 == s5);//true
  92. System.out.println(s3 == s6);//true
  93. }
  94. @Test
  95. public void test04(){
  96. String s1 = "hello";
  97. String s2 = "world";
  98. String s3 = "helloworld";
  99. String s4 = s1 + "world";//s4字符串内容也helloworld,s1是变量,"world"常量,变量 + 常量的结果在堆中
  100. String s5 = s1 + s2;//s5字符串内容也helloworld,s1和s2都是变量,变量 + 变量的结果在堆中
  101. String s6 = "hello" + "world";//常量+ 常量 结果在常量池中,因为编译期间就可以确定结果
  102. System.out.println(s3 == s4);//false
  103. System.out.println(s3 == s5);//false
  104. System.out.println(s3 == s6);//true
  105. }
  106. @Test
  107. public void test03(){
  108. String str1 = new String("hello");
  109. String str2 = new String("hello");
  110. //这两行代码,几个对象?3个
  111. }
  112. @Test
  113. public void test02(){
  114. String str = new String("hello");//两个字符串对象
  115. //一个在常量池中:hello
  116. //另一个在堆中,String的对象
  117. //堆中的这个字符串对象char[]的value数组,指向常量池中"hello"的char[]的value
  118. }
  119. @Test
  120. public void test01(){
  121. String str = "hello";//一个字符串对象
  122. }
  123. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/237853
推荐阅读
相关标签
  

闽ICP备14008679号