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

原创 2003年05月30日 15:03:00

不知道这个标题是否让读者产生一种想打我的冲动。至少今天我的主管被我用这个小把戏诧异了一把,当他看到"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:

 MindWarp {
   main(String[] args) {
    System.out.println(
      );
  }
   String OH_ROMEO =
    ;
   Warper warper =  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:

 java.lang.reflect.*;

 Warper {
   Field stringValue;
   {
    
     {
      stringValue = String..getDeclaredField();
    } (NoSuchFieldException ex) {
      
      Field[] all = String..getDeclaredFields();
       ( i=; stringValue ==  && i<all.length; i++) {
         (all[i].getType().equals(char[].)) {
          stringValue = all[i];
        }
      }
    }
     (stringValue != ) {
      stringValue.setAccessible(); 
    }
  }
   Warper() {
     {
      stringValue.set(
        ,
        .
          toCharArray());
      stringValue.set(, .toCharArray());
    } (IllegalAccessException ex) {} 
  }
}

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

 StringEquals {
 main(String[] args) {
  System.out.println(.equals());
}
 String greeting = ;
 Warper warper =  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.)

 CachingHashcode {
   main(String[] args) {
    java.util.Map map =  java.util.HashMap();
    map.put(, );
     Warper();
    System.out.println(map.get());
    System.out.println(map);
  }
   String greeting = ;
}

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

CSDN_Dev_Image_2003-5-291943471.jpg 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中空串 “”!=null..字符串要用equals判等

自己写测试用例,区别:字符串为空对象,空对象。 package leetCode; public class strstr { public static int strStrMy(String ...
  • hzw05103020
  • hzw05103020
  • 2016年01月10日 22:34
  • 375

两个对象用equals方法比较为true,它们的Hashcode值相同吗?

两个对象用equals方法比较为true,它们的Hashcode值相同吗?        答:不一定相同。正常情况下,因为equals()方法比较的就是对象在内存中的值,如果值相同,那么Hash...
  • chwshuang
  • chwshuang
  • 2015年07月18日 17:17
  • 4882

"aa".equals(a)这种写法为什么就可以避免空指针。

public static void main(String[] args) { String a=null; if("aa".equals(a))//这种情形,不出现空指针异常 //if(a...
  • HYM1987
  • HYM1987
  • 2014年05月04日 21:08
  • 1881

第8条:覆盖equals时请遵守通用约定

术语:
  • u014723123
  • u014723123
  • 2014年06月24日 20:12
  • 775

Java中==号与equals()方法的区别

==号和equals()方法都是比较是否相等的方法,那它们有什么区别和联系呢? 首先,==号在比较基本数据类型时比较的是值,而用==号比较两个对象时比较的是两个对象的地址值:int x = 10; ...
  • StriverLi
  • StriverLi
  • 2016年11月01日 17:29
  • 1921

java中 equals()方法 与 “==”的区别

摘要java语言的 equals() 方法 与 “==” 的区别,是每个初学者都会遇到过的问题。之前自己也很清楚,可日子一长,又渐渐遗忘,于是整理出一篇博客,复习一下。...
  • echosilly
  • echosilly
  • 2016年06月26日 18:04
  • 2360

JAVA经典及细节总结

写代码: 1,明确需求。我要做什么?    分析的时候:从具体到抽象 2,分析思路。我要怎么做?1,2,3。 实现的时候:从抽象到具体 3,确定步骤。每一个思路部分用到哪些语句,方法,和对象。 ...
  • sinat_24196195
  • sinat_24196195
  • 2015年11月12日 21:39
  • 1325

Java 中 Equals和==的区别

在谈论equals和==的区别前,我们先简单介绍一下JVM中内存分配的问题。 在JVM中 内存分为栈内存和堆内存。二者有什么区别呢? 当我们创建一个对象(new Object)时,就会调用它的构造函数...
  • tcytcy123
  • tcytcy123
  • 2016年03月09日 16:19
  • 16008

Java中的equals和hashCode方法详解

Java中的equals方法和hashCode方法是Object中的,所以每个对象都是有这两个方法的,有时候我们需要实现特定需求,可能要重写这两个方法,今天就来介绍一些这两个方法的作用。 e...
  • jiangwei0910410003
  • jiangwei0910410003
  • 2014年04月01日 16:15
  • 53547

Java学习从菜鸟变大鸟之一 hashCode()和equals()的本质区别和联系

equals()是判读两个Set是否相等[前提是equals()在类中被覆盖]。==决定引用值是否指向同一对象。 1、当向集合set中增加对象时,首先计算要增加对象的hashCode码,根据该值来得到...
  • lishehe
  • lishehe
  • 2014年01月28日 11:43
  • 5596
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Java细节]"hi there".equals("cheers !") == true
举报原因:
原因补充:

(最多只允许输入30个字)