Java 常用类 String的使用

  1 package com.bytezero.stringclass;
  2 
  3 import com.sun.tools.javac.Main;
  4 import jdk.jfr.StackTrace;
  5 import org.junit.Test;
  6 
  7 import java.util.Locale;
  8 
  9 /**
 10  *String的使用
 11  *
 12  *
 13  *
 14  * @author Bytezero1·zhenglei!    Email:420498246@qq.com
 15  * create  2021-10-19 10:34
 16  */
 17 public class StringTest {
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25         /*
 26          结论:
 27             1.常量与常量的拼接结构在常量池,且常量池中不会存在相同内容的常量
 28             2.只要其中有一个是变量,结构就在堆中
 29             3.如果拼接的结果调用intern()方法,返回值就是在常量池中
 30 
 31 
 32          */
 33     @Test
 34     public void test3(){
 35         String s1 = "javaEE";
 36         String s2 = "hadoop";
 37 
 38         String s3 = "javaEEhadoop";
 39         String s4 = "javaEE"+"hadoop";
 40 
 41         String s5 = s1 + "hadoop";
 42         String s6 = "javaEE" + s2;
 43         String s7 = s1 +s2;
 44 
 45         System.out.println(s3 == s4);  //true
 46         System.out.println(s3 == s5);  //false
 47         System.out.println(s3 == s7);   //false
 48         System.out.println(s3 == s6);  //false
 49         System.out.println(s5 == s6);  //false
 50         System.out.println(s5 == s7);  //false
 51         System.out.println(s6 == s7);  //false
 52 
 53         String s8 = s5.intern();//返回得到的s8使用的常量池中已经存在的 "javaEEhadoop"
 54         System.out.println(s3 == s8);  //true
 55 
 56         System.out.println("**************************");
 57         String s9 = s6.intern();//返回得到的s8使用的常量池中已经存在的 "javaEEhadoop"
 58         System.out.println(s3 == s9);  //true
 59 
 60 
 61     }
 62 
 63 
 64 
 65 
 66 
 67     /*
 68 
 69     String 的实例化方式:
 70     方式一:通过字面量定义的方式
 71     方式二:通过new + 构造器的方式
 72      */
 73     @Test
 74     public void  test2(){
 75 
 76         //通过字面量定义的方式:此时的 s1 和 s2 的数据“zhenglei”声明在方法区中的字符串常量池中
 77         String s1 = "zhenglei";
 78         String s2 = "zhenglei";
 79 
 80         //通过 new + 构造器的方式:此时的S3 和 S4保存的地址值,是数据在堆空间中开辟空间以后对应的
 81         //地址值。
 82         String s3  = new String("zhenglei");
 83         String s4 = new String("zhneglei");
 84 
 85         System.out.println(s1 == s2);  //true
 86         System.out.println(s1 == s3); //false
 87         System.out.println(s1 == s4); //false
 88         System.out.println(s3 == s4); // false
 89 
 90 
 91     }
 92 
 93     /*
 94     String:字符串,使用一对" "引起来表示
 95     1.String 声明为final的,不可被继承
 96     2.String实现了 Serializable接口:表示字符串时支持序列化的。
 97              实现了Comparable接口,表示Sting可以比较大小
 98     3.String内部定义了final char[] value用于储存字符串数据
 99     4.String:代表不可变的字符序列,简称:不可变性
100            体现:1.当都对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的
101            value进行赋值
102                  2.当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能
103             使用原有的value进行赋值
104                  3.当调用Stringe的replace()方法修改指定字符或字符串时,也需要重新指定
105              内存区域赋值,不能使用原有的value进行赋值.
106 
107 
108     5.通过字面量的方式(区别于 new)给一个字符串赋值,此时的字符串值声明在字符串常量池中
109     6.字符串常量池中是不会储存相同内容的字符串的
110 
111     String s = new String("abc"); 方式创建对象,在内存中创建了几个对象?
112            两个:一个是堆空间中new结构,另一个是char[]对应的常量池的数据:“abc”
113 
114 
115 
116     String 对象的创建
117     String str = "hello";
118 
119     //从本质上this.value = new char[0];
120      String s1 = new String();
121 
122      //this.value = original.value;
123      String s2 = new String(String original);
124 
125      //this.value = Arrays.copyOf(value,value.length);
126      String s3 = new String(char[] a);
127 
128      String s4 = new String(char[] a,int startIndex,int count);
129 
130 
131 
132 
133 
134 
135      */
136     @Test
137     public void test1(){
138 
139         String s1 = "abc";    //字面量的定义方式
140         String s2 = "abc";    //
141         s1 = "Hello";
142 
143         System.out.println(s1 == s2);   //地址值
144 
145         System.out.println(s1);    //Hello
146         System.out.println(s2);     //abc
147 
148         System.out.println("********************************");
149         String s3 = "abc";
150         s3 += "def";
151         System.out.println(s3);   //abcdef
152         System.out.println(s2);
153 
154         System.out.println("********************************");
155         String s4 = "abc";
156         String s5 = s4.replace('a', 'm');
157         System.out.println(s4);  //abc
158         System.out.println(s5);  //mbc
159     }
160 }

 

posted on 2021-10-21 22:05  Bytezero!  阅读(54)  评论(0编辑  收藏  举报