JavaSE0018_String类详解

【学习源代码】

 

String类详解

 

1、 String类

 

public final class String extends Object

implements Serializable,Comparable<String>, CharSequence

 

The String class represents characterstrings. All string literals in Java programs, such as "abc", areimplemented as instances of this class. Strings are constant; their valuescannot be changed after they are created. String buffers support mutablestrings. Because String objects are immutable they can be shared.

[String类表示字符串。在Java程序中,如“ABC”,所有的字符串文字作为这个类的实例实现。字符串是常量;创建后不能改变。字符串缓冲区支持可变的字符串。由于String对象是不可改变的,他们可以共享的。]

For example:

String str = "abc";

is equivalent to:

char data[] = {'a', 'b', 'c'};

String str = new String(data);

 

Here are some more examples of how stringscan be used:

System.out.println("abc");

                 String cde = "cde";

                 System.out.println("abc" +cde);

                 String c ="abc".substring(2,3);

                  String d = cde.substring(1, 2);

 

The class String includes methods forexamining individual characters of the sequence, for comparing strings, forsearching strings, for extracting substrings, and for creating a copy of astring with all characters translated to uppercase or to lowercase. Casemapping is based on the Unicode Standard version specified by the Characterclass.

[String类包括方法研究单个字符的序列,比较字符串,搜索字符串中提取子字符串,并创建了一个字符串的所有字符转换为大写或小写的副本。大小写映射基于Character类指定的Unicode标准版本。]

 

The Java language provides special supportfor the string concatenation operator ( + ), and for conversion of otherobjects to strings. String concatenation is implemented through theStringBuilder(or StringBuffer) class and its append method. String conversionsare implemented through the method toString, defined by Object and inherited byall classes in Java. For additional information on string concatenation andconversion, see Gosling, Joy, and Steele, The Java Language Specification.

[Java语言提供了字符串连接符(+)的特别支持,并为其他对象转换为字符串。字符串串联实施的StringBuilder(或StringBuffer的)类及其append方法。字符串转换是通过toString方法实现,定义在Java中所有类的对象和继承。字符串串联和转换的其他信息,请参阅高斯林,喜悦,和Steele,Java语言规范。]

 

Unless otherwise noted, passing a nullargument to a constructor or method in this class will cause aNullPointerException to be thrown. A String represents a string in the UTF-16format in which supplementary characters are represented by surrogate pairs(see the section Unicode Character Representations in the Character class formore information). Index values refer to char code units, so a supplementarycharacter uses two positions in a String. The String class provides methods fordealing with Unicode code points (i.e., characters), in addition to those fordealing with Unicode code units (i.e., char values).

 

[除非另有说明,否则将null参数传递到在这个类的构造函数或方法将导致抛出一个NullPointerException。字符串表示增补字符由代理对(有关详细信息,请参阅Character类节的Unicode字符表示)为代表的UTF - 16格式的字符串。索引值是指为char代码单元,因此增补字符在一个字符串使用两个职位。 String类提供处理Unicode代码点(即字符),除了处理Unicode代码单元(即char值),的方法。]

 

2、String类的方法

 

2.1)equals()方法详解

 

源代码:

public boolean equals(Object anObject) {

       if(this == anObject) {

           return true;

       }

       if(anObject instanceof String) {

           String anotherString = (String)anObject;

           int n = count;

           if (n == anotherString.count) {

              charv1[] = value;

              charv2[] = anotherString.value;

              inti = offset;

              intj = anotherString.offset;

              while(n-- != 0) {

                  if (v1[i++] != v2[j++])

                     returnfalse;

              }

              returntrue;

           }

       }

       returnfalse;

}

此段程序详解:

首先判断调用此方法的String引用与传入的Object引用是否指向同一个对象,相同直接返回true,否则转入下面流程。在两个指向不同的基础上,在保证传入的引用类型是String类型的实例基础上,进行一个字符一个字符的比对其字面值是否相等。若相等返回true。注意,在逐个字符判断之前为了调用子类特有的方法,进行了一个向下类型转化的操作(Object类型转化为String类型,调用只有子类String才有的count方法)。

 

标准定义

 

public boolean equals(Object anObject)

Compares this string to the specifiedobject. The result is true if and only if the argument is not null and is aString object that represents the same sequence of characters as this object.

Overrides:

equals in class Object

Parameters:

anObject - The object to compare thisString against

Returns:

true if the given object represents aString equivalent to this string, false otherwise

See Also:

compareTo(String), equalsIgnoreCase(String)

 

 

2.2)toStrng()方法详解

 

源代码:

public StringtoString()  {return this;}

 

标准定义:

 

public String toString()

This object (which is already a string!) isitself returned.

Specified by:

toString in interface CharSequence

Overrides:

toString in class Object

Returns:

the string itself.

 

 

String类详解

[Class017/ StringTest.java]

String类陷阱深度剖析

[Class017/ StringTest1.java]

 

 

对于String类的equals()方法来说,它是判断当前字符串与传进来的字符串内容是否一致。

 

对于String 对象的相等性判断来说,请使用equals()方法,而不是使用==。

 

String是常量,其对象一旦创建完毕就无法改变。当使用+拼接字符串时,会生成新的String对象,而不是向原有的String对象追加内容。[Class017/ StringTest2.java]

 

 

String pool(字符串池)位于栈中

 

String s = “aaa”;(采用字面值方式赋值)

1)

查找String Pool中是否存在“aaa”这个对象,如果不存在,则在String Pool中创建一个“aaa”对象,然后将String Pool中的这个“aaa”对象的地址返回来,赋给应用变量s,这样s会指向Sting Pool中的这个“aaa”字符串对象。

2)如果存在,则不创建任何对象,直接将String Pool中的 这个“aaa”对象地址返回来,赋给引用s。

 

String s = new String(“aaa”);

 

1)

首先在String Pool中查找有没有“aaa”这个字符串对象,如果有,则不在String Pool中再去创建“aaa”这个对象了,直接在堆中(heap)中创建一个“aaa”字符串对象,然后将堆中的这个“aaa”对象的地址返回来,赋给s引用,导致s指向了堆中创建的这个“aaa”字符串对象。

2)

如果没有,则首先在StringPool中创建“aaa”这个对象,然后再在堆中(heap)中创建一个“aaa”字符串对象,然后将堆中的这个“aaa”对象的地址返回来,赋给s引用,导致s指向了堆中创建的这个“aaa”字符串对象。

[Class017/ StringTest1.java]

 

2.3)Intern()方法详解

 

拘留词

源代码:

public native String intern();

 

标准定义:

public String intern()

 

Returns a canonical representation for thestring object. A pool of strings, initially empty, is maintained privately bythe class String. When the intern method is invoked, if the pool alreadycontains a string equal to this String object as determined by theequals(Object) method, then the string from the pool is returned. Otherwise,this String object is added to the pool and a reference to this String objectis returned.

[返回一个字符串对象的规范表示。一个字符串池,最初是空的,是由String类单独维护。当intern方法被调用时,如果池已经包含一个字符串相等的equals(Object)方法确定此String对象,然后从池中的字符串返回。否则,将此String对象添加到池中,并且返回此String对象的引用。]

 

It follows that for any two strings s and t, s.intern() ==t.intern() is true if and only if s.equals(t) is true.

 

All literal strings and string-valuedconstant expressions are interned. String literals are defined in section 3.10.5of the The Java Language Specification.

 

Returns:

a string that has the same contents as thisstring, but is guaranteed to be from a pool of unique strings.

 

 

 

 

 

String重点小程序分析:

 

[Class017/ StringTest3.java]

 

详细解答见:Java 2SE 6 Documentation 文件中 The Java Language Specification

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值