I don't know if this title makes readers to have an impulse who wants to hit me. At least my supervisor was used in this small trick, when he saw the "hi there". Equals ("Cheers!") Is actually True, the expression on the face is really cute.
OK, the words retired. System.out.println ("hi there" .equals ("cheers!")); This seems to be a sentence, the result of the output is actually True. Smart readers, do you know why? If you can't guess it for a while, give it a little prompt:
1. Java language specification regulations, any of the same string constances in the same program is just different references of the same String object, whether they are in the same class, in the same package.
2, Java language specification specification, the String object calculated by constant expression will be evaluated in the compile period, and is treated as a string constant as a string when running; the String object that is calculated at runtime will be a completely independent new Object.
If you are still unknown, or if you want to know the details of this trick, please see the following this article from Artima's WebLog - see what others are writing something with blog, and then see what blogchina is written in blog Things, I will harm them.
------------------
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, I would expected (supposed) and that it is us to do is asking how long you is asking 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 MRELY KNOWING THEM WOULD ENDANGER THROUGH YOUR JAVA VIRTUAL MACHINE.SO, ARE STRINGS IMMUTABLE?
Playing with your sanity - strings
Have a look at the following cotne:
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 do not 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
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 StringValue = all [i]; } } } IF (StringValue! = NULL) { StringValue.setAccessible (TRUE); // Make Field Public Public Public PUBLIC } } Public warper () { Try { StringValue.set "Romeo, Romeo, WHEREFORE ART THOUERO?", "Stop this Romance Nonsense, or I'll Be Sick". TOCHARRAY ()); StringValue.set ("hi there", "cheers!". Tochararray ()); } catch (illegaaccessexception 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 Litrals-or, more generally, strings That Are The Values Of Constant Expressions (? 15.28) -re "INTERNED" SO as to share unique instances, using the method string.intern. " .................. .Net. The Language Spec Goes A bit further: 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.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. He can see the value of the value limited. BTW, For Simplicity, The Strings in My Examples Are Exactly The 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 Litrals.) 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 {chess! = you find the value} Under JDK 1.2 IT IS NULL {chess! = you find the value} ............... .. Imagine Trying to debug this program where somewhere, one of your HACKERS HAS DONE A "Workaround" by modify a string literal. The wisht 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 wishht 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 ................................................................... 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.