System.out.println(XXX.toString()==XXX.toString()); XXX - denotes objects created below. the results for each object is given with comment
Explain me on what basis it work
String s = new String ("hi"); // true Byte b = new Byte ("23");// false Character c = new Character('h');// false Short sor = new Short ("2"); // true Integer in = new Integer ("23"); // false Long l = new Long ("2345"); //false Float f = new Float ("123.4");// false Double d = new Double("1234.5");// false Boolean bb = new Boolean(" true");// true
And sure enough, the output is: true false false true false false false false true
The reason the Short example returns true is not that Short has a different process from Integer for creating a String value. The reason is that the value passed to Short was 2.
Here's what happens: sor.toString() invokes the Short method:
which passes the int value 2 to String.valueOf(int):
which passes (2, 10) to the method Integer.toString(int, int):
which passes 2 to the static method String.toString(int):
which takes the value 2, matches it to one of the switch cases, and returns the String "2", which is a String literal and therefore taken from the String pool.
If you change new Short("2") to new Short("45") you'll see that that line will evaluate to false instead of true. Likewise, if you change new Integer("23") to new Integer("7") then the Integer line will evaluate to true instead of false. This is because 2 and 7 are on the list of case clauses in the switch statement above, while 45 and 23 are not.
This sort of knowledge is definitely not required for the exam.
As for why String and Boolean produce true, that's simpler.
String's non-static toString() method is:
so s.toString() and s.toString() will both return the same value, s, so they will be equal.
Boolean's non-static toString() method is:
bb.toString() and bb.toString() will both return the same String literal from the pool, so the results will be equal. In this case, they will both return "false" because the String passed to the Boolean constructor was " true" which contains a space and is therefore not valid as a String that denotes true.
So basically, expect Boolean and String always to behave that way, and expect that MOST numbers passed into the other wrapper classes will produce unequal String references from toString, but a few numbers will produce equal String references.
Again, I refuse to believe that that last part is knowledge we need for the exam. [ May 06, 2005: Message edited by: Joe Sanowitz ]
SCJA 1.0 (98%), SCJP 1.4 (98%)
Joined: Jan 16, 2005
Joe, That was just awesome explanation.In short,i would like to confirm one thing,so if the number is between -3 and 10,the Integer,Short,Byte all would return the same object,on both the invocations of toString()....otherwise they would return two unequal objects.I need to know this for some question that might come on the exam.Thanks!!
Joined: Apr 10, 2005
Originally posted by jas oberai: Joe, That was just awesome explanation.In short,i would like to confirm one thing,so if the number is between -3 and 10,the Integer,Short,Byte all would return the same object,on both the invocations of toString()....otherwise they would return two unequal objects.I need to know this for some question that might come on the exam.Thanks!!
Thanks for the appreciation. Yes, if the number is an integer between -3 and 10, or if it's exactly -2147483648, then Integer, Short, and Byte would return the same object for each invocation of the non-static toString() method, otherwise they'll return a different object for each invocation.
HOWEVER, no, you do not have to know this for the exam. I got this by wading through the java.lang source code files, and I only did it because the original poster posted an intriguing question. It is by no means something that the exam requires you to know. It's meaningless trivia. The only uses for this information are to amuse oneself and to practice studying a complex situation. The conclusion however, has no practical value.
If this knowledge were required for the exam, it would be absurdly unreasonable. [ May 06, 2005: Message edited by: Joe Sanowitz ]