- 浏览: 68786 次
- 性别:
- 来自: 北京
文章分类
最新评论
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."
*/
}
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."
*/
}
发表评论
-
How to be a Programmer: A Short,Comprehensive,and Personal Summary
2013-10-28 10:38 547well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 358http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 629this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 699http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 632http://archive.plugins.jquery.c ... -
What is Optimistic Locking vs. Pessimistic Locking
2011-09-09 16:50 795What is Optimistic Locking vs. ... -
what is DAO
2011-04-15 13:42 735http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 673I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 605http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 674http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1642http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 798Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 859The XML declaration typically a ... -
UCM
2010-05-08 11:41 711Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 693refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 813As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 903copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 779copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 841It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1278copy from http://blog.cornetdes ...
相关推荐
Understand集成了代码编辑器,代码跟踪器和代码分析器,提供了很强大的界面,将分析结果以各种形式(图形、图表、架构...目前Understand支持C/C++/C#, Ada, Java, FORTRAN, Delphi 和Jovial等编程语言。 注册版,可用。
You will understand how to set a simple operator in Java You will learn all the technical Java programming language such as Loops and Arrays, Boolean Logic, Methods, Inheritance and Polymorphism, ...
a Java developer needs understand to use the Java programming language effectively in a project, as well as to get a certification. I initially planned to cover all essential topics in Java in 700 to ...
Understand.for.Java.v1.4.340.Incl.KeygenAndInstall.zip
Understand软件的功能主要定位于代码的阅读理解。 具备如下特性: 1、支持多语言:Ada, C, C++, C#, Java, FORTRAN, Delphi, Jovial, and PL/M ,混合语言的project也支持 2、多平台: Windows/Linux/Solaris/HP-...
In short, I wrote this book to help the Java community understand and use the Java programming language effectively, without having to read many books on the same subject. I wrote this book with the ...
Learn various string-related performance improvements, including compact string and modify string concatenation Explore various underlying compiler improvements, such as tiered attribution and Ahead-...
You’ll see how to use the Java runtime tools, understand the Java environment, and create a simple namesorting Java application. Further on, you’ll learn about advanced technologies that Java ...
CIC Java Reskill Program Java Quiz it is easy to understand
understand详细使用教程,详细介绍了各种understand使用技巧
By giving easy to follow examples, you will understand the concepts with increasing depth. The examples presented are straightforward and the topic is presented in a concise manner. Key features of ...
skills necessary to understand the programs in this text. It is not a complete Java reference; for a more thorough coverage of Java, consult the Bibliographical Notes.We do not assume that you are ...
1.支持多语言:Ada, C, C++, C#, Java, FORTRAN, Delphi, Jovial, and PL/M ,混合语言的project也支持 2.多平台: Windows/Linux/Solaris/HP-UX/IRIX/MAC OS X 3.代码语法高亮、代码折迭、交叉跳转、书签等...
SciTools Understand代码查看工具 6.5.1159 Windows版 英文版 带补丁
中文Understand+破解器+中文使用说明书(绝对好用)
scitools, understand5.1.1029, linux
Understand basic techniques used in object-oriented design Examine concurrency and memory, and how they’re intertwined Work with Java collections and handle common data formats Delve into Java’s ...
Understand5--ubuntu18.04-64bit.zip
JieYuan 捷源 SciTools Understand 6.5.1159 Windows版 中文版 带补丁
wrote this book to help the Java community understand and use the Java programming language effectively, without having to read many books on the same subject. I wrote this book with the plan that it ...