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."
*/
}
- Understand Java String Identity
- Understand Java Thread state
- Understand ClassLoader in Java
- To understand java command
- Understand <Java Concurrency in Practice>
- 关于java.lang.NoSuchFieldError: STRING at org.jbpm.identity.hibernate.PermissionUserType.
- Understand for Java v1.4.366b
- Java学习札记9:Understand The SerialVersionUID
- Java Persistence/Identity and Sequencing
- identity
- @@IDENTITY
- @@IDENTITY
- @@identity
- 解决User.Identity.IsAuthenticated=false 或User.Identity.Name=string.empty的问题
- Java Thread Examples (Will updated as I understand it better)
- Tutorial Java – #8 Understand Stack and Heap
- Understand JVM:Java内存区域模型与内存溢出异常
- Understand inline
- 多服务器间共享Session的解决方案
- vs2005开发程序引起的:"应用程序初始化失败"的问题
- JAVA经典算法收集整理
- System.getProperty()参数大全
- js中escape,encodeURI,encodeURIComponent三个函数的区别
- Understand Java String Identity
- 编码转换工具native2ascii的使用
- winxp 下 硬盘 装 suse linux
- 读取并修改App.config文件
- 于Java的相关技术网站
- 【zt】可以让你少奋斗10年的工作经验
- 调用栈相关。--飞雪所为
- div背景图片不显示的问题
- 从B表中检索数据,和A表匹配。。根据别人的问题写的。带注释