Understand Java String Identity

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

public class TestStringIdentical extends TestCase {

public void [color=red]testTwoConstantStringAreTheSame[/color]()
{
Assert.assertSame("hi", "hi");
}

public void [color=red]testConstantStringIsTheSameAsItsReference[/color]()
{
String reference = "hi";
Assert.assertEquals(reference, "hi");
}

public void [color=red]testTwoConsantStringReferenceAreTheSame[/color]()
{
String hi = "hi";
String hey = "hi";
Assert.assertSame(hi, hey);
}

public void [color=red]testConstantStringPlusAReferenceIsNotConstant[/color]()
{
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 [color=red]testConstantStringPlusConstantIsStillConstant[/color]()
{
//computed during compile-time, so still a constant.
Assert.assertSame("hello", "hel" + "lo");
Assert.assertSame("hello123", "hel" + "lo" + 123);
}

public void [color=red]testInternedStringAlwaysIsConstant[/color]()
{
Assert.assertSame("hello", new String("hello").intern());
Assert.assertSame("hello", "hello".intern());
}

public void [color=red]testConstantStringIsNotTheSameAsTheOneConctructedByNew[/color]()
{
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 [color=red]testSubStringFromIndexIsInclusiveButEndIndexIsExclusive[/color]()
{
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."
*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值