Understand Java String Identity

来源:互联网 发布:java高并发分布式视频 编辑:程序博客网 时间:2024/04/28 18:37

import junit.framework.TestCase;
import junit.framework.Assert;

public class TestStringIdentical extends TestCase {

 public void testTwoConstantStringAreTheSame()
 {
  Assert.assertSame("hi", "hi");
 }
 
 public void testConstantStringIsTheSameAsItsReference()
 {
  String reference = "hi";
  Assert.assertEquals(reference, "hi");
 }
 
 public void testTwoConsantStringReferenceAreTheSame()
 {
  String hi = "hi";
  String hey = "hi";
  Assert.assertSame(hi, hey);
 }

 public void testConstantStringPlusAReferenceIsNotConstant()
 {
  String reference = "lo";
  //since "hel" + reference is computed at runtime,which is newly created and therefore distinct. check apendix below
  Assert.assertNotSame("hello", "hel" + reference);
 }
 
 public void testConstantStringPlusConstantIsStillConstant()
 {
  //computed during compile-time, so still a constant.
  Assert.assertSame("hello", "hel" + "lo");
  Assert.assertSame("hello123", "hel" + "lo" + 123);
 }
 
 public void testInternedStringAlwaysIsConstant()
 {
  Assert.assertSame("hello", new String("hello").intern());
  Assert.assertSame("hello", "hello".intern());
 }
 
 public void testConstantStringIsNotTheSameAsTheOneConctructedByNew()
 {  
  String hi = new String("hi");

  Assert.assertNotSame(hi, "hi");
  
  String empty = new String();
  Assert.assertNotSame("", empty);
  
  String original = new String("sometext");
  String refer = original;
  Assert.assertSame(original, refer);
 }
 
 public void testSubStringFromIndexIsInclusiveButEndIndexIsExclusive()
 {
  String abc = "abc";
  Assert.assertEquals("c", abc.substring(2,3));
  Assert.assertEquals("b", abc.substring(1,2));
 }
 
 
 /**
  * Appendix 1:
  *     package testPackage;
    class Test {
     public static void main(String[] args) {
      String hello = "Hello", lo = "lo";
      System.out.print((hello == "Hello") + " ");
      System.out.print((Other.hello == hello) + " ");
      System.out.print((other.Other.hello == hello) + " ");
      System.out.print((hello == ("Hel"+"lo")) + " ");
      System.out.print((hello == ("Hel"+lo)) + " ");
      System.out.println(hello == ("Hel"+lo).intern());
     }
    }
    class Other { static String hello = "Hello"; }

and the compilation unit:

    package other;
    public class Other { static String hello = "Hello"; }

produces the output:

    true true true true false true

This example illustrates six points:

    * Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).
    * Literal strings within different classes in the same package represent references to the same String object.
    * Literal strings within different classes in different packages likewise represent references to the same String object.
    * Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
    * Strings computed at run time are newly created and therefore distinct.
    * The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
  */
 
 
 /**
  * Appendix 2:
  * A compile-time constant expression is an expression denoting a value of primitive type or a String that is composed using only the following:

    * Literals of primitive type and literals of type String
    * Casts to primitive types and casts to type String
    * The unary operators +, -, ~, and ! (but not ++ or --)
    * The multiplicative operators *, /, and %
    * The additive operators + and -
    * The shift operators <<, >>, and >>>
    * The relational operators <, <=, >, and >= (but not instanceof)
    * The equality operators == and !=
    * The bitwise and logical operators &, ^, and |
    * The conditional-and operator && and the conditional-or operator ||
    * The ternary conditional operator ? :
    * Simple names that refer to final variables whose initializers are constant expressions
    * Qualified names of the form TypeName . Identifier that refer to final variables whose initializers are constant expressions

Compile-time constant expressions are used in case labels in switch statements (§14.10) and have a special significance for assignment conversion (§5.2).

A compile-time constant expression is always treated as FP-strict (§15.4), even if it occurs in a context where a non-constant expression would not be considered to be FP-strict.

Examples of constant expressions:

    true
    (short)(1*2*3*4*5*6)
    Integer.MAX_VALUE / 2
    2.0 * Math.PI
    "The integer " + Long.MAX_VALUE + " is mighty big."
  */
}

原创粉丝点击