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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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