`
niwtsew
  • 浏览: 71956 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Understand Java String Identity

阅读更多
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."
  */
}
0
0
分享到:
评论

相关推荐

    Understand代码阅读带注册码

    Understand是一款跨平台的代码分析工具,适用于多种编程语言,如C、C++、Java、Python、C#等。它提供了丰富的特性,帮助开发者深入理解复杂的代码结构,提高代码质量和维护性。通过其强大的分析引擎,开发者能够快速...

    understand

    在本文中,我们将深入探讨 Understand 的功能特性,以及如何利用它来提升 C++ 和 Java 代码的审查效率。 首先,"Understand" 提供了一种直观的方式来理解项目中的代码结构。它能够快速地生成代码的层次视图,帮助...

    Understand5--ubuntu18.04-64bit.zip

    此外,Ubuntu 18.04集成了许多开发者友好的特性,例如对Python、Java、C++等编程语言的开箱即用支持,以及对Git、Docker等开发工具的无缝集成。 接下来,我们转向核心话题——代码阅读器Understand5。Understand是...

    SCI的源码阅读工具Understand6.2版本,运行于Linux平台

    1. **源码结构分析**:Understand可以解析多种编程语言的源码,如C、C++、Java、Python等,展示类、函数、变量等元素的层次结构,帮助开发者快速定位到特定的代码部分。 2. **依赖关系图**:工具能够生成直观的图形...

    JieYuan 捷源 SciTools Understand 6.5.1159 Windows版 中文版 带补丁

    SciTools Understand是一款专业的源代码分析工具,适用于多种编程语言,如C、C++、Java、C#、Python等。它提供了以下核心功能: 1. **代码导航**:通过强大的搜索和索引功能,用户可以快速定位代码中的特定函数、类...

    源码阅读工具Understand-3.1-Linux-64bit,源码安装包,使用手册,注册号

    1. **代码浏览器**:Understand提供了一个直观的代码浏览器,支持多种编程语言,包括C、C++、Java、Python等,可以查看类、函数、变量等定义,以及它们之间的关系。 2. **代码结构分析**:通过构建代码的抽象语法树...

    understand4.0 for windows part1

    Understand集成了代码编辑器,代码跟踪器和代码分析器,提供了很强大的界面,将分析结果以各种形式(图形、图表、架构...目前Understand支持C/C++/C#, Ada, Java, FORTRAN, Delphi 和Jovial等编程语言。 注册版,可用。

    scitools, understand5.1.1029, linux

    《深入探索:Scitools Understand 5.1.1029 在 Linux 平台的应用》 Scitools Understand 是一款强大的源代码分析工具,它为开发者提供了对代码的全面理解,帮助他们进行代码审查、复杂性分析、依赖关系可视化等任务...

    Scientific Toolworks understand 最新5.0版本(内含激活)

    首先,Understand 5.0提供了全面的代码分析能力,支持多种编程语言,如C、C++、C#、Java、Python等,能够帮助开发者快速了解项目的整体结构和关系。通过其直观的界面,用户可以查看代码依赖性、函数调用图、类继承...

    Autosar源码阅读工具Understand.7z

    首先,Understand是一款强大的代码分析和管理工具,它支持多种编程语言,包括C、C++、Java等,因此对于AUTOSAR中广泛使用的C++代码,它是理想的选择。该工具的核心功能包括源码浏览、结构分析、代码统计、依赖关系图...

    understand详细使用教程

    understand详细使用教程,详细介绍了各种understand使用技巧

    Java.Crash.Course.1541019245.epub

    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, ...

    Understand-6.1.1091-Linux-64bit.tgz

    它不仅仅是一个编译器,而是一个全面的代码分析和管理平台,支持多种编程语言,如C、C++、Java、Python等。通过它,开发者可以对项目进行深入的静态分析,包括但不限于代码复杂性度量、依赖关系图、代码质量检查、...

    understand4+注册机

    同时,它还支持多种编程语言,包括但不限于C、C++、Java、Python、C#等,适应了多元化开发需求。 为了充分利用Understand,用户应学习其提供的各种工具和功能,例如自定义视图以适应个人工作习惯,使用代码覆盖率...

    Understand.for.Java.v1.4.340.Incl.KeygenAndInstall.zip

    Understand.for.Java.v1.4.340.Incl.KeygenAndInstall.zip

    understand5.952 for linux下载破解安装教程(new)

    ### Understand 5.952 for Linux 下载与安装教程 #### 一、引言 在软件开发领域,代码阅读工具对于提升开发效率至关重要。在Linux环境下,Understand 被誉为是一款堪比 Windows 下 SourceInsight 的神器,特别适用...

    Understand

    《深入理解Understand软件在MISRA-C规则检测中的应用》 在当今的汽车行业,软件已经成为汽车核心竞争力的重要组成部分。为了确保汽车软件的可靠性和安全性,业界广泛采用了一套严格的编码标准——MISRA-C。MISRA-C...

    Understand 4.0.811 破解版

    Understand软件的功能主要定位于代码的阅读理解。 具备如下特性: 1、支持多语言:Ada, C, C++, C#, Java, FORTRAN, Delphi, Jovial, and PL/M ,混合语言的project也支持 2、多平台: Windows/Linux/Solaris/HP-...

Global site tag (gtag.js) - Google Analytics