Home  >  Article  >  Java  >  Summarize the example tutorials of String class in Java

Summarize the example tutorials of String class in Java

零下一度
零下一度Original
2017-05-22 11:03:081271browse

Because we need to summarize all aspects involved in the String class here, we also give some SCJP questions. If there are any shortcomings, please tell me in the comments, thank you.
1. Invariant mode:
The value of a string object cannot be changed after it is created.

String str1="hello";//创建一个对象hello,不会变;
System.out.println(str1);
str1+=" world!";//两个字符串对象粘粘,系统其实创建了一个新的对象,把Str1的指向改了,指向新的对象;hello就                     //变成了垃圾;
System.out.println(str1);

//If you keep creating it like this, it will affect the efficiency of the system; if you want to frequently change the value of a string object, use StringBuffer to describe it;

StringBuffer sb=new StringBuffer("[");
sb.append("hehe");
sb.append("]");//append();不会制造垃圾,真正在改sb的值;
System.out.println(sb);

2. Object Pool

First of all, you must understand that Object obj = new Object();

obj is a reference to the object, which is located on the stack, and new Object() is the object, which is located on the heap.

It can be understood that obj is the rope tied to the hydrogen balloon, and new Object() is the hydrogen balloon. We access the object through the reference of the object, just like we are holding the rope to prevent the hydrogen balloon from running away.

There are two main ways to create a Stirng object:

java 代码
String str1 = new String("abc");    
Stirng str2 = "abc";

Although both statements return a reference to a String object, the jvm handles both. are different. For the first type, the jvm will immediately create a String object in the heap, and then return the reference to the object to the user. For the second type, jvm will first use the equals method of String in the internally maintained strings pool to find whether the String object is stored in the object pool. If so, the existing String object will be returned to the user,
instead of A new String object will be re-created in the heap; if there is no such String object in the object pool, jvm will create a new String object in the heap, return its reference to the user, and add the reference to strings poo
l in. Note: When using the first method to create an object, the jvm will not actively put the object into the strings pool unless the program calls the intern method of String. Look at the following example:
java code
String str1 = new String("abc"); //jvm creates a String object on the heap

//jvm cannot be found in the strings pool The string value is "abc", so
//Create a String object on the heap and add the reference of the object to the strings pool
//There are two String objects on the heap at this time

Stirng str2 = "abc";   //并没有创建对象,因为对象池里已经有"abc" 了
  
 if(str1 == str2){   
         System.out.println("str1 == str2");   
 }else{   
         System.out.println("str1 != str2");   
 }

//The printed result is str1 != str2, because they are two different objects on the heap


 String str3 = "abc";

//At this time, the jvm found that there are already strings in the strings pool "abc" object, because "abc" equals "abc"

//So directly return the object pointed by str2 to str3, that is to say, str2 and str3 are references pointing to the same object

if(str2 == str3){   
         System.out.println("str2 == str3");   
  }else{   
         System.out.println("str2 != str3");   
  }

//The print result is str2 == str3

3. String / StringBuffer / StringBuilder
The String class is a string constant and is an unchangeable constant. StringBuffer is a string variable, and its object can be expanded and modified.

StringBuffer sb=new StringBuffer("[");//创建StringBuffer对象
sb.append("hehe");//把hehe加入字符串,变成 "[hehe"
sb.append("]");//append();不会制造垃圾,真正在改sb的值;
System.out.println(sb);
String str = sb.toString();//把"[hehe]",赋值给一个字符串对象str

StringBuilder, a variable sequence of characters. This class provides a StringBuffer-compatible API but does not guarantee synchronization. This class is designed to be used as a drop-in replacement for StringBuffer when the string buffer is used by a single thread (which is a common situation). It is recommended to prefer this class if possible, as it is faster than StringBuffer in most implementations.


StringBuilder sb=new StringBuilder("[");//创建StringBuilder对象
sb.append("hehe");//把hehe加入字符串,变成 "[hehe"
sb.append("]");//append();不会制造垃圾,真正在改sb的值;
System.out.println(sb);
String str = sb.toString();//把"[hehe]",赋值给一个字符串对象str

4. Commonly used methods in the String class

The String class includes methods that can be used to check individual characters of a sequence, compare strings, search strings, extract substrings, and create strings Copy and convert all characters to uppercase or lowercase. There’s no need to remember it, just look up

API when you need it. You’ll naturally become familiar with it if you use it a lot.

java.lang.String
 char charAt (int index) 返回index所指定的字符
 String concat(String str) 将两字符串连接
 boolean endsWith(String str) 测试字符串是否以str结尾
 boolean equals(Object obj) 比较两对象
 char[] getBytes 将字符串转换成字符数组返回
 char[] getBytes(String str) 将指定的字符串转成制服数组返回
 boolean startsWith(String str) 测试字符串是否以str开始
 int length() 返回字符串的长度
 String replace(char old ,char new) 将old用new替代
 char[] toCharArray 将字符串转换成字符数组
 String toLowerCase() 将字符串内的字符改写成小写
 String toUpperCase() 将字符串内的字符改写成大写
 String valueOf(Boolean b) 将布尔方法b的内容用字符串表示
 String valueOf(char ch) 将字符ch的内容用字符串表示
 String valueOf(int index) 将数字index的内容用字符串表示
 String valueOf(long l) 将长整数字l的内容用字符串表示
 String substring(int1,int2) 取出字符串内第int1位置到int2的字符串
[java] view plain copy
import java.util.Collections;  
import java.util.Comparator;  
import java.util.LinkedList;  
import java.util.List;  
public class StringUsage {  
 // 几个欧不太用的方法。  
 public static void main(String[] args) {  
  // 判断字符串首/尾。  
  String contentS = "test String again. test ";  
  System.out.println(contentS);  
  System.out.println("contentS.startsWith(/"test/")/t"  
    + contentS.startsWith("test"));  
  System.out.println("contentS.endsWith(/"test/")/t"  
    + contentS.endsWith("test"));  
  // 忽略大小写的字符串比较方法。  
  System.out  
    .println("contentS.compareToIgnoreCase(contentS.toUpperCase()))/t"  
      + contentS.compareToIgnoreCase(contentS.toUpperCase()));  
  // 不区分大小写的字符串比较。  
  Comparator<String> cmprStr = String.CASE_INSENSITIVE_ORDER;  
  List<String> lstStr = new LinkedList<String>();  
  // 准备数据。  
  lstStr.add("test");  
  lstStr.add("Test");  
  lstStr.add("tEst");  
  lstStr.add("rest");  
  lstStr.add("sest");  
    
  // 不区分大小写的排序。  
  Collections.sort(lstStr, cmprStr);  
  System.out.println(lstStr);  
  // 区分大小写的排序。  
  Collections.sort(lstStr);  
  System.out.println(lstStr);  
  System.out.println("/ntest String.valueOf()");  
    
  // 用 valueOf 处理 null 对象的字符串转换。  
  try {  
   String ss = null;  
   System.out.println(ss);  
   System.out.println(String.valueOf(null)); // 比较奇怪的地方  
  } catch (RuntimeException e1) {  
      e1.printStackTrace();  
  }  
    
  BigDecimal db = null;  
  try {  
   System.out.println(((BigDecimal)db).toString());  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
  try {  
   System.out.println(String.valueOf(db));  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
  db = new BigDecimal("100.00");  
  try {  
   System.out.println(((BigDecimal)db).toString());  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
 }  
}

5. SCJP question


String a="Hello World!";
String b="Hello World!";
a=b?  a和b是否相等 ? 为什么?
String a=new String("Hello World!");
String b="Hello World!";
a=b?  a和b是否相等 ? 为什么?

Explanation:

1. First of all, String does not belong to the 8 basic data types. String is an object.

Because the default value of the object is null, the default value of String is also null; but it is a special object and has some characteristics that other objects do not have.

2. New String() and new String("") both declare a new empty string, which is an empty string and not null;

3. String str="kvill";
String str=new String ("kvill"); The difference:
Here, we do not talk about the heap, nor the stack, but simply introduce the simple concept of the constant pool first.
The constant pool (constant pool) refers to
some data that is determined during compilation and saved in the compiled .class file. It includes constants in classes, methods, interfaces, etc., as well as string constants.
Look at Example 1:

String s0="kvill";  
String s1="kvill";  
String s2="kv" + "ill";  
System.out.println( s0==s1 );  
System.out.println( s0==s2 );

结果为:  
true  
true  
首先,我们要知道Java会确保一个字符串常量只有一个拷贝。  
因为例子中的s0和s1中的”kvill”都是字符串常量,它们在编译期就被确定了,所以
s0==s1为true;而”kv”和”ill”也都是字符串常量,当一个字符串由多个字符串常量连
接而成时,它自己肯定也是字符串常量,所以s2也同样在编译期就被解析为一个字符串常量,所以s2也是常量池中”kvill”的一个引用。所以我们得出s0==s1==s2;  

用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String()
创建的字符串不放入常量池中,它们有自己的地址空间。  

看例2:  

String s0="kvill";  
String s1=new String("kvill");  
String s2="kv"+ new String("ill");  
System.out.println( s0==s1 );  
System.out.println( s0==s2 );  
System.out.println( s1==s2 );

结果为:  
false  
false  
false  
例2中s0还是常量池中”kvill”的应用,s1因为无法在编译期确定,所以是运行时创
建的新对象”kvill”的引用,s2因为有后半部分new String(“ill”)所以也无法在编译
期确定,所以也是一个新创建对象”kvill”的应用;明白了这些也就知道为何得出此结果了。  
4. String.intern():  
再补充介绍一点:存在于.class文件中的常量池,在运行期被JVM装载,并且可以扩充。String的intern()方法就是扩充常量池的一个方法;当一个String实例str调用intern()
方法时,Java查找常量池中是否有相同Unicode的字符串常量,如果有,则返回其的引用,如果没有,则在常量池中增加一个Unicode等于str的字符串并返回它的引用;看例3就清楚了。 
例3:  

String s0= "kvill";  
String s1=new String("kvill");  
String s2=new String("kvill");  
System.out.println( s0==s1 );  
System.out.println( “**********” );  
s1.intern(); 
s2=s2.intern(); //把常量池中"kvill"的引用赋给s2  
System.out.println( s0==s1);  
System.out.println( s0==s1.intern() );  
System.out.println( s0==s2 );

结果为:  
false  
**********  
false //虽然执行了s1.intern(),但它的返回值没有赋给s1  
true //说明s1.intern()返回的是常量池中”kvill”的引用  
true
最后我再破除一个错误的理解:  
有人说,“使用String.intern()方法则可以将一个String类的保存到一个全局Strin
g表中,如果具有相同值的Unicode字符串已经在这个表中,那么该方法返回表中已有字符串的地址,如果在表中没有相同值的字符串,则将自己的地址注册到表中“如果我把
他说的这个全局的String表理解为常量池的话,他的最后一句话,“如果在表中没有相同值的字符串,则将自己的地址注册到表中”是错的:  

看例4:  

String s1=new String("kvill");  
String s2=s1.intern();  
System.out.println( s1==s1.intern() );  
System.out.println( s1+" "+s2 );  
System.out.println( s2==s1.intern() );

结果:  
false  
kvill kvill  
true  
在这个类中我们没有声名一个"kvill"常量,所以s1.intern()同new String("kvill")是不同的,当我们调用s1.intern()后就在常量池中新添加了一个"kvill"常量,原来的
不在常量池中的"kvill"仍然存在,也就不是“将自己的地址注册到常量池中”了。  
s1==s1.intern()为false说明原来的“kvill”仍然存在;  
s2现在为常量池中“kvill”的地址,所以有s2==s1.intern()为true。  
5. 关于equals()和==:  
这个对于String简单来说就是比较两字符串的Unicode序列是否相当,如果相等返回true;而==是比较两字符串的地址是否相同,也就是是否是同一个字符串的引用。  
6. 关于String是不可变的  
这一说又要说很多,大家只要知道String的实例一旦生成就不会再改变了,比如说:
String str=”kv”+”ill”+” “+”ans”;  
就是有4个字符串常量,首先”kv”和”ill”生成了”kvill”存在内存中,然后”kvill”又和” “ 生成 ”kvill “存在内存中,最后又和生成了”kvill ans”;
并把这个字符串的地址赋给了str,就是因为String的“不可变”产生了很多临时变量,这也就是为什么建议用StringBuffer的原因了,因为StringBuffer是可 font-family: Arial;font-size:12px;">改变的。</span> <span style="color: rgb(51, 51, 153); font-family: Arial;font-size:12px;"> </span> <span style="color: rgb(51, 51, 153); font-family: Arial;font-size:12px;">

【相关推荐】

1. java中String是对象还是类?详解java中的String

2. 分享Java中String类的实例教程

3. Java中String类的常用方法是什么?总结Java中String类的常用方法

4. 分享java String部分源码解读

The above is the detailed content of Summarize the example tutorials of String class in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn