aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Puzzling String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Puzzling String " Watch "Puzzling String " New topic
Author

Puzzling String

Sugat Thulkar
Greenhorn

Joined: Nov 16, 2000
Posts: 4
String one= "Hello";
String two= "Hell"+"o";
bolean answer = one==two;
Now u will think the answer should be false but no! if u check it out it will TRUE? whats the reason behind this
I Knon the answer! Do u?
Balamurugan Kandasamy
Greenhorn

Joined: Dec 03, 2000
Posts: 19
Hi ,
There goes my guess .. During compilation since that string one and two are initiated with same literal ( after concatenation for two), the string pool is used and both point to same literal. Hence the result ( though the value of the object two can be changed down the line and the similar assignment would result in false). Someone pls correct if Im wrong...
Thanks
MKBala...
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Balamurugan
when we use operator '+' append() method of StringBuffer is invoked on a StringBuffer Object.
After the concatenation has been done compiler calls toString() to convert StringBuffer Object back into a String Object.
but in this kind of stuations resuting string belongs to a heap not to a pool.
I changed Sugat example
String one= "Hello";
String two= one+"o";
bolean answer = one==two;
Now answer gives false and whatever I said about concatenation above applies here .
but in the original example String two= "Hell"+"o"; it seems append method of Stringbuffer is not called also no String
object is created into the pool.Anyway it needs some explaination and i'm waiting for that....
Thanks

Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Balamurugan
yes its correct..as compiler knows the value of string at compile time, so it picks up from String pool
prasad
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Prasad
you mean during the compile time the value of
String two= "Hell"+"o";
is evaluated which is already in the pool so
append method is not called.
Hmm sounds reasonable..
Sugat Thulkar
Greenhorn

Joined: Nov 16, 2000
Posts: 4
Hello Friends !
you are going near
Here is one more CLUE
In switch construct we can use only CONSTANTS in case statements
But any variable is allowed there? NO!
But you can use expressions involving only constants
Like 3+2 etc
TRY HARD!!
luv SUGAT
Sugat Thulkar
Greenhorn

Joined: Nov 16, 2000
Posts: 4
Hello Friends !
you are going near
Here is one more CLUE
In switch construct we can use only CONSTANTS in case statements
But any variable is allowed there? NO!
But you can use expressions involving only constants
Like 3+2 etc
TRY HARD!!
luv SUGAT
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Puzzling String
 
Similar Threads
doubt in John Meyer's mock exam - Garbage collection!
Is it possible to remove all spaces from a String?(SOLVED)
question on GC
Vector.
Strign Objects