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

[Java细节]"hi there".equals("cheers !") == true

阅读更多

不知道这个标题是否让读者产生一种想打我的冲动。至少今天我的主管被我用这个小把戏诧异了一把,当他看到"hi there".equals("cheers !") 的结果居然是true时,脸上的表情实在是可爱。

OK,言归正传。System.out.println("hi there".equals("cheers !")); 这个看来再显然不过的句子,输出的结果居然是true。聪明的读者,你知道是为什么吗?如果一时还猜不出来,给你一点提示:

1、Java语言规范规定,同一个程序中任何相同的字符串常量(literal string)都只是同一个String对象的不同引用,不论它们是否在同一个类、同一个包中。

2、Java语言规范规定,由常量表达式计算得到的String对象将在编译期被求值,并在运行时被作为字符串常量对待;在运行时计算得到的String对象将是一个完全独立的新对象。

如果你仍然不明就里,或者想知道这个把戏实现的细节,请看下面这篇来自artima的webLog——看看别人都在用blog写什么东西,再看看blogchina那帮人在用blog写什么东西,我都替他们害臊。

——————————————————

Artima Weblogs
"hi there".equals("cheers !") == true
by Heinz Kabutz
May 21, 2003
Summary
Java Strings are strange animals. They are all kept in one pen, especially the constant strings. This can lead to bizarre behaviour when we intentionally modify the innards of the constant strings through reflection. Join us, as we take apart one of Java's most prolific beasts.

Whenever we used to ask our dad a question that he could not possibly have known the answer to (such as: what's the point of school, dad?) he would ask back: "How long is a piece of string?"

Were he to ask me that now, I would explain to him that String is immutable (supposedly) and that it contains its length, all you have to do is ask the String how long it is. This you can do by calling length().

OK, so the first thing we learn about Java is that String is immutable. It is like when we first learn about the stork that brings the babies? There are some things you are not supposed to know until you are older! Secrets so dangerous that merely knowing them would endanger the fibres of electrons pulsating through your Java Virtual Machine.

So, are Strings immutable?

Playing with your sanity - Strings

Have a look at the following code:

public class MindWarp {
  public static void main(String[] args) {
    System.out.println(
      "Romeo, Romeo, wherefore art thou oh Romero?");
  }
  private static final String OH_ROMEO =
    "Romeo, Romeo, wherefore art thou oh Romero?";
  private static final Warper warper = new Warper();
}

If we are told that the class Warper does not produce any visible output when you construct it, what is the output of this program? The most correct answer is, "you don't know, depends on what Warper does". Now THERE's a nice question for the Sun Certified Java Programmer Examination.

In my case, running "java MindWarp" produces the following output

C:> java MindWarp <ENTER>
Stop this romance nonsense, or I'll be sick

And here is the code for Warper:

import java.lang.reflect.*;

public class Warper {
  private static Field stringValue;
  static {
    // String has a private char [] called "value"
    // if it does not, find the char [] and assign it to value
    try {
      stringValue = String.class.getDeclaredField("value");
    } catch(NoSuchFieldException ex) {
      // safety net in case we are running on a VM with a
      // different name for the char array.
      Field[] all = String.class.getDeclaredFields();
      for (int i=0; stringValue == null && i<all.length; i++) {
        if (all[i].getType().equals(char[].class)) {
          stringValue = all[i];
        }
      }
    }
    if (stringValue != null) {
      stringValue.setAccessible(true); // make field public
    }
  }
  public Warper() {
    try {
      stringValue.set(
        "Romeo, Romeo, wherefore art thou oh Romero?",
        "Stop this romance nonsense, or I'll be sick".
          toCharArray());
      stringValue.set("hi there", "cheers !".toCharArray());
    } catch(IllegalAccessException ex) {} // shhh
  }
}

How is this possible? How can String manipulation in a completely different part of the program affect our class MindWarp?

To understand that, we have to look under the hood of Java. In the language specification it says in ?3.10.5:

"Each string literal is a reference (?4.3) to an instance (?4.3.1, ?12.5) of class String (?4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (?15.28)-are "interned" so as to share unique instances, using the method String.intern."

The usefulness of this is quite obvious, we will use less memory if we have two Strings which are equivalent pointing at the same object. We can also manually intern Strings by calling the intern() method.

The language spec goes a bit further:

  1. Literal strings within the same class (?8) in the same package (?7) represent references to the same String object (?4.3.1).
  2. Literal strings within different classes in the same package represent references to the same String object.
  3. Literal strings within different classes in different packages likewise represent references to the same String object.
  4. Strings computed by constant expressions (?15.28) are computed at compile time and then treated as if they were literals.
  5. Strings computed at run time are newly created and therefore distinct.
  6. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.

This means that if a class in another package "fiddles" with an interned String, it can cause havoc in your program. Is this a good thing? (You don't need to answer ;-)

Consider this example

public class StringEquals {
public static void main(String[] args) {
  System.out.println("hi there".equals("cheers !"));
}
private static final String greeting = "hi there";
private static final Warper warper = new Warper();
}

Running this against the Warper produces a result of true, which is really weird, and in my opinion, quite mind-bending. Hey, you can SEE the values there right in front of you and they are clearly NOT equal!

BTW, for simplicity, the Strings in my examples are exactly the same length, but you can change the length quite easily as well.

Last example concerns the HashCode of String, which is now cached for performance reasons mentioned in "Java Idiom and Performance Guide", ISBN 0130142603. (Just for the record, I was never and am still not convinced that caching the String hash code in a wrapper object is a good idea, but caching it in String itself is almost acceptable, considering String literals.)

public class CachingHashcode {
  public static void main(String[] args) {
    java.util.Map map = new java.util.HashMap();
    map.put("hi there", "You found the value");
    new Warper();
    System.out.println(map.get("hi there"));
    System.out.println(map);
  }
  private static final String greeting = "hi there";
}

The output under JDK 1.3 is:

You found the value
{cheers !=You found the value}

Under JDK 1.2 it is

null
{cheers !=You found the value}

This is because in the JDK 1.3 SUN is caching the hash code so if it once is calculated, it doesn't get recalculated, so if the value field changes, the hashcode stays the same.

Imagine trying to debug this program where SOMEWHERE, one of your hackers has done a "workaround" by modifying a String literal. The thought scares me.

The practical application of this blog? Let's face it, none.

This is my first blog ever, I would be keen to hear what you thought of it?

Talk Back!

Have an opinion? Readers have already posted 24 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Heinz Kabutz adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Heinz Kabutz enjoys driving Java to the limits, and then a bit beyond. He has been programming in Java since 1997 on several very unimportant projects. During that time, he has picked up some horrifying tips on how you can get the most out of Java. These are published on his bi-monthly "The Java(tm) Specialists' Newsletter" (http://www.javaspecialists.co.za). It is not for the uninitiated :-) Heinz received a PhD in Computer Science from the University of CapeTown. He loves living in South Africa as it is both beautiful and interesting. Professionally, Heinz survives by writing Java code, insulting, ahem, consulting, and presenting courses on Java and Design Patterns.

This weblog entry is Copyright © 2003 Heinz Kabutz. All rights reserved.
分享到:
评论

相关推荐

    java 中String.equals和==的比较

    Java 中 String.equals 和 == 的比较 Java 中 String.equals 和 == 的比较是 Java 编程语言中一个常见的概念,但是一些初学者容易混淆这两个概念。下面我们将详细介绍 Java 中 String.equals 和 == 的比较。 ...

    C# oracle通用类

    if (this._dataAdapter.SelectCommand.Connection.Equals(this._transaction.Connection)) this._dataAdapter.SelectCommand.Connection = null; } if (this._dataAdapter.InsertCommand != null) { if (this...

    java中equals和==的区别

    Java 中 equals 和 == 的区别 Java 中的 equals 和 == 是两个不同的概念,很多开发者容易混淆它们。理解这两个概念的区别是非常重要的,因为它们对编程的正确性和性能都有很大的影响。 首先,我们需要了解 Java ...

    java中equals和==的区别.doc

    Java 中 equals 和 == 的区别 Java 中的 equals 和 == 是两个不同的运算符,它们之间的区别是非常重要的。 首先,我们需要了解 Java 中的内存模型。在 Java 中,变量可以分为两种:基本类型(primitive type)和...

    java中equals和==的区别.docx

    Java 中 equals 和 == 的区别 Java 中的 equals 和 == 是两个常用的操作符,经常用于比较对象或变量的值。然而,许多开发者不知道它们之间的区别,或者误用它们,导致程序出错。下面我们将详细解释 equals 和 == 的...

    2.javaequals()方法.zip

    2.javaequals()方法.zip2.javaequals()方法.zip2.javaequals()方法.zip2.javaequals()方法.zip2.javaequals()方法.zip2.javaequals()方法.zip2.javaequals()方法.zip2.javaequals()方法.zip2.javaequals()方法.zip2....

    java 资料 equals 与== 的区别

    Java 中的equals和==的区别 Java 是一种面向对象的编程语言,其中有两种数据类型:基本数据类型和引用数据类型。基本数据类型包括整数类型、浮点数类型、字符类型、布尔类型等,共有八种;而引用数据类型则包括 ...

    java中equals和==的区别[文].pdf

    在Java编程语言中,`equals()`方法和`==`运算符是两个经常被用来比较对象的工具,但它们之间存在着显著的区别。理解这些差异对于编写正确的代码至关重要。 首先,`equals()`方法是`java.lang.Object`类的一个成员...

    Java中Object.equals和String.equals的区别详解

    Java中Object.equals和String.equals的区别详解 Java中的Object.equals和String.equals是两个不同的equals方法,它们之间的区别是非常重要的,理解这两个方法的区别对于我们编写高质量的Java代码非常重要。 首先,...

    【Java面试题】equals与==的区别

    【Java面试题】equals与==的区别

    Java基础复习(内附String中equals与==区别的分析)

    在Java编程语言中,基础知识是每个开发者都需要扎实掌握的关键部分。本篇复习将重点讨论String类中的`equals()`方法和`==`运算符的区别,这对于理解对象比较和字符串操作至关重要。 首先,`==`运算符在Java中用于...

    Java中String判断值为null或空及地址是否相等的问题

    在Java编程中,字符串(String)是非常常见且重要的数据类型。本文主要讨论了如何正确判断Java中的String对象是否为null、空值("")以及它们的地址是否相等。在处理字符串时,了解这些概念对于避免程序出错至关重要。...

    java中equals和==的区别.pdf

    Java中equals和==的区别 Java是一门面向对象的编程语言,它提供了多种运算符和方法来比较对象和变量。在Java中,比较两个对象是否相等时,经常使用到的运算符有"=="和"equals"。虽然两者都可以用于比较,但它们有着...

    java中equals()函数的用法 equals和==的区别

    在Java编程语言中,`equals()`方法和`==`运算符是两个经常被用来比较对象是否相等的关键概念。理解它们的区别对于编写出正确、健壮的代码至关重要。 首先,`==`运算符用于基本类型(如int, char, boolean)的比较,...

    2021年java面试题集锦.docx

    代码解读:因为 x 和 y 指向的是同一个引用,所以 == 也是 true,而 new String() 方法则重写开辟了内存空间,所以 == 结果为 false,而 equals 比较的一直是值,所以结果都为 true。 equals 解读:equals 本质上...

    java中equals和==的区别.

    在Java编程语言中,`equals()`方法和`==`运算符是用于比较对象的两种不同方式,它们在处理不同类型的数据时有不同的行为。了解这两者的区别对于编写正确的代码至关重要。 `==`运算符: 1. `==`用于比较基本类型的...

    与==的区别.docxequal与==的区别.docx

    在Java编程语言中,`equals()`方法和`==`运算符是用于比较对象之间关系的两种常见方式,但它们有着显著的区别。理解这些差异对于编写正确的代码至关重要。 首先,`==`运算符主要用于比较基本类型(如int、char、...

Global site tag (gtag.js) - Google Analytics