- 浏览: 71956 次
- 性别:
- 来自: 北京
文章分类
最新评论
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 587well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 398http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 660this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 745http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 697http://archive.plugins.jquery.c ... -
What is Optimistic Locking vs. Pessimistic Locking
2011-09-09 16:50 834What is Optimistic Locking vs. ... -
what is DAO
2011-04-15 13:42 769http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 708I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 665http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 714http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1675http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 833Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 901The XML declaration typically a ... -
UCM
2010-05-08 11:41 746Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 763refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 843As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 936copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 817copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 866It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1309copy from http://blog.cornetdes ...
相关推荐
Understand是一款跨平台的代码分析工具,适用于多种编程语言,如C、C++、Java、Python、C#等。它提供了丰富的特性,帮助开发者深入理解复杂的代码结构,提高代码质量和维护性。通过其强大的分析引擎,开发者能够快速...
在本文中,我们将深入探讨 Understand 的功能特性,以及如何利用它来提升 C++ 和 Java 代码的审查效率。 首先,"Understand" 提供了一种直观的方式来理解项目中的代码结构。它能够快速地生成代码的层次视图,帮助...
此外,Ubuntu 18.04集成了许多开发者友好的特性,例如对Python、Java、C++等编程语言的开箱即用支持,以及对Git、Docker等开发工具的无缝集成。 接下来,我们转向核心话题——代码阅读器Understand5。Understand是...
1. **源码结构分析**:Understand可以解析多种编程语言的源码,如C、C++、Java、Python等,展示类、函数、变量等元素的层次结构,帮助开发者快速定位到特定的代码部分。 2. **依赖关系图**:工具能够生成直观的图形...
SciTools Understand是一款专业的源代码分析工具,适用于多种编程语言,如C、C++、Java、C#、Python等。它提供了以下核心功能: 1. **代码导航**:通过强大的搜索和索引功能,用户可以快速定位代码中的特定函数、类...
1. **代码浏览器**:Understand提供了一个直观的代码浏览器,支持多种编程语言,包括C、C++、Java、Python等,可以查看类、函数、变量等定义,以及它们之间的关系。 2. **代码结构分析**:通过构建代码的抽象语法树...
Understand集成了代码编辑器,代码跟踪器和代码分析器,提供了很强大的界面,将分析结果以各种形式(图形、图表、架构...目前Understand支持C/C++/C#, Ada, Java, FORTRAN, Delphi 和Jovial等编程语言。 注册版,可用。
《深入探索:Scitools Understand 5.1.1029 在 Linux 平台的应用》 Scitools Understand 是一款强大的源代码分析工具,它为开发者提供了对代码的全面理解,帮助他们进行代码审查、复杂性分析、依赖关系可视化等任务...
首先,Understand 5.0提供了全面的代码分析能力,支持多种编程语言,如C、C++、C#、Java、Python等,能够帮助开发者快速了解项目的整体结构和关系。通过其直观的界面,用户可以查看代码依赖性、函数调用图、类继承...
首先,Understand是一款强大的代码分析和管理工具,它支持多种编程语言,包括C、C++、Java等,因此对于AUTOSAR中广泛使用的C++代码,它是理想的选择。该工具的核心功能包括源码浏览、结构分析、代码统计、依赖关系图...
understand详细使用教程,详细介绍了各种understand使用技巧
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, ...
它不仅仅是一个编译器,而是一个全面的代码分析和管理平台,支持多种编程语言,如C、C++、Java、Python等。通过它,开发者可以对项目进行深入的静态分析,包括但不限于代码复杂性度量、依赖关系图、代码质量检查、...
同时,它还支持多种编程语言,包括但不限于C、C++、Java、Python、C#等,适应了多元化开发需求。 为了充分利用Understand,用户应学习其提供的各种工具和功能,例如自定义视图以适应个人工作习惯,使用代码覆盖率...
Understand.for.Java.v1.4.340.Incl.KeygenAndInstall.zip
### Understand 5.952 for Linux 下载与安装教程 #### 一、引言 在软件开发领域,代码阅读工具对于提升开发效率至关重要。在Linux环境下,Understand 被誉为是一款堪比 Windows 下 SourceInsight 的神器,特别适用...
《深入理解Understand软件在MISRA-C规则检测中的应用》 在当今的汽车行业,软件已经成为汽车核心竞争力的重要组成部分。为了确保汽车软件的可靠性和安全性,业界广泛采用了一套严格的编码标准——MISRA-C。MISRA-C...
Understand软件的功能主要定位于代码的阅读理解。 具备如下特性: 1、支持多语言:Ada, C, C++, C#, Java, FORTRAN, Delphi, Jovial, and PL/M ,混合语言的project也支持 2、多平台: Windows/Linux/Solaris/HP-...