Home  >  Article  >  Java  >  Java basic String class

Java basic String class

ringa_lee
ringa_leeOriginal
2017-06-22 16:45:171497browse


1. String class represents string

All string literals in Java programs (such as "abc" ) are implemented as instances of this class.

Strings are constants; their values ​​cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable, they can be shared. For example:

1

String str = "abc";

is equivalent to:

char data[] = {'a', 'b', 'c'}; 2 String str = new String(data);


given below Here are some more examples of how to use strings:

1 System.out.println("abc");2      String cde = "cde";3      System.out.println("abc" + cde);4      String c = "abc".substring(2,3);5      String d = cde.substring(1, 2);

String The class includes methods that can be used to check individual characters of a sequence, compare strings, search for characters string, extract substrings, create a copy of the string, and convert all characters to uppercase or lowercase. Case mapping is based on the Unicode standard version specified by the Character class.

The Java language provides special support for the string concatenation symbol ("+") and for converting other objects into strings. String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method. String conversion is implemented through the toString method, which is defined by the Object class and can be inherited by all classes in Java. For more information on string concatenation and conversion, see The Java Language Specification by Gosling, Joy, and Steele.

Unless otherwise noted, passing a null argument to a constructor or method in this class will throw a null pointer exception.

String Represents a UTF-16 formatted string in which supplementary characters are represented by surrogate pairs. The index value refers to the char code unit, so the supplementary character occupies two positions in the String. The

String class provides methods for working with Unicode code points (that is, characters) and Unicode code units (that is, char values).

2. How to create a string

1 String s="abc";

means, first create a reference s on the stack. It will first go to the constant pool to see if there is the constant "abc". If there is, then point s to "abc" in the constant pool.

If not, create abc in the constant pool,

1 String s=new String("abc");

Equivalent to String obj="abc"; String s=new String(obj); After this operation, there are two copies of data in the memory: one in the constant pool and one on the heap. Due to the new operation, regardless of whether there is "abc" in the constant pool, it will create a copy on the heap

3. Comparison of strings

Example 1: Use of string constant pool

1 String s0 = "abc"; 
2 String s1 = "abc"; 
3 System.out.println(s0==s1); //true 
4 //s0  和  s1 都指向了常量池中的同一个 "abc"

Example 2: The difference between == and equals in String

1 String s0 =new String ("abc");   //new 这个操作,将在堆上产生对象,s0指向了堆2 String s1 =new String ("abc"); 
3 System.out.println(s0==s1); //false  s0 和 s1 指向的是堆上不同de的对象System.out.println(s0.equals(s1)); //true 因为String类重写了equals方法,比的是实体的内容

Example 3: Compilation Determined at the time

1 String s0="helloworld";2 String s1="helloworld";3 String s2="hello" + "world";   //编译的时候,直接就编译成了 helloworld4 System.out.println( s0==s1 );   //true5 System.out.println( s0==s2 );   //true

Example 4: Unable to determine at the compile time

1 String s0="helloworld";2 String s1=new String("helloworld");3 String s2="hello" + new String("world");4 System.out.println( s0==s1 ); //false  一个指向常量池,一个指向堆System.out.println( s0==s2 ); //false5 System.out.println( s1==s2 ); //false

Example 5: Optimization at the compile time

 1 String s0 = "a1"; 2 String s1 = "a" + 1; 3 System.out.println((s0 == s1)); //true 4                    5 String s2 = "atrue"; 6 String s3= "a" + "true"; 7 System.out.println((s2 == s3))  //true 8                    9 String s4 = "a3.4";10 String s5 = "a" + 3.4;11 System.out.println((s4 == s5));  //true

Example 6 Unable to determine at compile time

1 String s0 = "ab";2 String s1 = "b";3 String s2 = "a" + s1;   //s1不是常量,编译期无法确定4 System.out.println((s0 == s2)); //false

Example 7: Determined at compile time

1 String s0 = "ab";2 final String s1 = "b";     //加上final 就变成了常量3 String s2 = "a" + s1;  //对于两个常量相加,编译器能确定它的值4 System.out.println((s0 == s2));     //true

四、String对象内存分析

//例一

String a = "abc"; ①  

String b = "abc"; ② 

分析:

①代码执行后在常量池(constant pool)中创建了一个值为abc的String对象,

②执行时,因为常量池中存在 "abc" 所以就不再创建新的String对象了。

//例二

String   c   =   new   String("xyz");①  

String   d   =   new   String("xyz");②  

分析:

①Class被加载时,"xyz"被作为常量读入,在常量池(constant pool)里创建了一个共享的值为"xyz"的String对象;

然后当调用到new String("xyz")的时候,会在堆(heap)里创建这个new   String("xyz")对象;

②由于常量池(constant pool)中存在"xyz"所以不再创建"xyz",然后创建新的new String("xyz")。

//例三

String   s1   =   new   String("xyz");     //创建二个对象(常量池和堆中),一个引用   

String   s2   =   new   String("xyz");     //创建一个对象(堆中),并且以后每执行一次创建一个对象,一个引用   

String   s3   =   "abc";     //创建一个对象(常量池中),一个引用     

String   s4   =   "abc";     //不创建对象(共享上次常量池中的数据),只是创建一个新的引用s4)

//例四

 public static void main(String[] args) {    
 //以下两条语句创建了1个对象。"凤山"存储在字符串常量池中     3 String str1 = "凤山";    
 String str2 = "凤山";     
 System.out.println(str1==str2);//true     
 //以下两条语句创建了3个对象。"天峨",存储在字符串常量池中,两个new String()对象存储在堆内存中      7 String str3 = new String("天峨");     
 String str4 = new String("天峨");     
 System.out.println(str3==str4);//false      
//以下两条语句创建了1个对象。9是存储在栈内存中   //这里所说的一个对象,是指的9 , i 和 j 则是对9的引用  11 int i = 9;     
int j = 9;     
System.out.println(i==j);//true      
//由于没有了装箱,以下两条语句创建了2个对象。两个1对象存储在堆内存中    15 Integer l1 = new Integer(1);    注意这里是没有装箱操作的16 Integer k1 = new Integer(1);    
System.out.println(l1==k1);//false  18  //以下两条语句创建了1个对象。1对象存储在栈内存中。自动装箱时对于值从127之间的值,使用一个实例。    19 Integer l = 20;//装箱     20 Integer k = 20;//装箱     21 System.out.println(l==k);//true    22 Integer i1 = 256;     //以下两条语句创建了2个对象。i1,i2变量存储在栈内存中,两个256对象存储在堆内存中   23 Integer i2 = 256;     
System.out.println(i1==i2);//false  25 }

五、String 类常见操作

字符串的常见操作,大致有以下几类    获取,判断,转换,替换和切割

1) 获取类操作   

String str="春花秋月何时了,往事知多少?小楼昨夜又东风,故国不堪回首月明中";

1 这个字符串到底有多长

2 第4个字是什么  即根据索引获取字符   

3 第一个逗号是第几个字符 即根据字符取索引( 取字符(或字符串)的位置)

4 最后一个“月”字的索引

5 是否含有“月明”  这个字符序列

6 是不是以"春花"开头,是否以“月明中”结尾

7 这个串是否为空

8 是否和另一个串相等

String str="春花秋月何时了,往事知多少?小楼昨夜又东风,故国不堪回首月明中";
System.out.println("长度:" + str.length());  //31
System.out.println("第四个字是"+str.charAt(3)); //月
System.out.println("第一个逗号的位置是"+str.indexOf(',')); //7
System.out.println("第一个逗号的位置是"+str.indexOf(",")); //7
System.out.println("第一个往事的位置是"+str.indexOf("往事")); //8
System.out.println("最后一个月字的索引"+str.lastIndexOf("月")); //28
System.out.println("是否含有月明"+str.contains("月明"));  //true
System.out.println("是否以春花开头"+str.startsWith("春花"));  //true
System.out.println("是否以月明中结尾"+str.endsWith("月明中"));  //true
System.out.println("是否为空"+str.isEmpty());  //false
System.out.println(str.equals("另一个字符串")); //false
String s1="abc";
String s2="aBC";
System.out.println(s1.equalsIgnoreCase(s2));  //true  equalsIgnoreCase 比较的时候忽略大小写。


The above is the detailed content of Java basic String class. 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