This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in Strings" Watch "doubt in Strings" New topic
Author

doubt in Strings

Ankitt Dhebarr
Ranch Hand

Joined: Feb 13, 2009
Posts: 32



Can anyone help me out with this code and what could be the answer and why ? I have seen this code in Khalid Mughal . Can anyone explain this to me in details perfectly ..
Thanks in advance !!
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
true true true false true

SCJP 1.5
"A candle looses nothing by lighting another candle"
itechmentors.com
Ankitt Dhebarr
Ranch Hand

Joined: Feb 13, 2009
Posts: 32
harilal ithikkat wrote:true true true false true


Can you please explain me how did you do that !! Please I would like to know that !!
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
i compiled it using

javac -d . *.java


then i run it using

java testPackage.Test

i got
true true true false true

i dont know how to explain this
but its because of Pooling.

except in the second last case, same object is being compared thats why we got "true"

harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
if you are using

String hello = new String("Hello");

in Test.java


a new object will be created in the Pool.
so we will get all false


As far as no new object is creating same object will be refered and intern method will check for String object with same value.


try it.you will get your idea realized
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Ankitt Dhebarr wrote:
harilal ithikkat wrote:true true true false true

Can you please explain me how did you do that !! Please I would like to know that !!


Quite frankly, most of these are straightforward -- it may be better for you to tell us what you think the answers should be... and why? This way, we can tell what you are confused with.


Anyway, the most confusing one is probably the third one, so I'll answer that one for you.



The left variable is assigned a string that is in the string pool. You would think that the right side expression can't be in the string pool, since an addition is happening. However, if you look closer, you will see that it is an addition of two compile time constants. The compiler is able to calculate the result at compile time and also use the string pool with the right expression.

So the answer for the third expression is true.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Apoorv Srivastava
Ranch Hand

Joined: Jan 03, 2009
Posts: 43
Can anyone please explain line no 4..
After concatenation "Hel"+lo we get "Hello"..This is already in the pool.So,will it not return true when comparing..
So we should get true again..Please explain how come we are getting false after comparison..


SCJP 1.5...Preparing for SCWCD
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

well yes "Hel"+ lo will return "Hello" which is already in the string pool, but that will not be checked. A new object will be created for this Hello. If you want the object to be from the pool, then use intern. i.e. change the statement 4 to this and try again



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Apoorv Srivastava wrote:Can anyone please explain line no 4..
After concatenation "Hel"+lo we get "Hello"..This is already in the pool.So,will it not return true when comparing..
So we should get true again..Please explain how come we are getting false after comparison..


Basically, the lo local variable is not a compile time constant -- meaning the variable has to exist, the compiler can't replace it with a constant. It happens to contain a value that is in the pool, but the variable itself is not a compile time constant. So, the string concat must occur at runtime, which of course, generates a new string. A string which may have an equivalent value in the pool -- but is not in the pool..... hence, false.

Henry

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Ankit Garg wrote:well yes "Hel"+ lo will return "Hello" which is already in the string pool, but that will not be checked. A new object will be created for this Hello. If you want the object to be from the pool, then use intern. i.e. change the statement 4 to this and try again




Which is the explanation of why line 5 is true.

Henry
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Henry Wong wrote:Which is the explanation of why line 5 is true.

Henry


I didn't see the complete code
Apoorv Srivastava
Ranch Hand

Joined: Jan 03, 2009
Posts: 43
Now I got it..Thanks fellas..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in Strings
 
Similar Threads
String a-go-go
String literals
inner class doubt
Strings
String questions