Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String question

 
Mark Guo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm confused that no output for this code, can anybody explain the reason?
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Guo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, thank you for your patience, God bless you.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THAT would explain it. Thanks Imad
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you welcome Seema....and welcome to Java Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic