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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String question" Watch "String question" New topic
Author

String question

Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58

I'm confused that no output for this code, can anybody explain the reason?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2097
    
    7
Don't use "==".

Use the equals(...) method to compare objects. This is explained in detail in hundreds of postings around the web.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Mark Guo wrote:
I'm confused that no output for this code, can anybody explain the reason?

As said by Rob Camick, == means both Object should be identical in the sense of having same memory. But, in this case, their content are equal, but there are not identical.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
Thanks guys, thank you for your patience, God bless you.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Mark Guo wrote:I'm confused that no output for this code, can anybody explain the reason?

Also check out the article String Literally. Hope it help you.


http://muhammadkhojaye.blogspot.com/
Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35
Thanks for pointing to the String literal article. Interesting to note that c="a"+"b" will give you a different result as compared to c=a+b, even though they are both concat, a+b creates a new String object and is not in the String literal pool while "a"+"b" is in the String literal pool. Can someone explain that?


OCPJP 6
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
my guess is, because "a" +"b" is concatination of constant literals, the concatination is done by the compiler.
but since a, b are variable refrences thier value is not known till run time. as you said and as the above article pointed out, in this case a new String object is created in the heap.

Stephen Davies this:
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
is very funny (check the method again: you want a wife but you returned void. I think its better to use public void setWife(Woman theWoman) so your wife will not be exposed to the public)


love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35
THAT would explain it. Thanks Imad
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
you welcome Seema....and welcome to Java Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String question
 
Similar Threads
String addition
Total number of object string creates?
doubt in String object
Strings comparison
internal doubt abou string and string buffer