This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What does this output (S.O.P.) statement print false when it is concatenated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What does this output (S.O.P.) statement print false when it is concatenated " Watch "What does this output (S.O.P.) statement print false when it is concatenated " New topic
Author

What does this output (S.O.P.) statement print false when it is concatenated

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10905
    
  12

because when they are not in parenthesis, it first concatenates s to "s == s1: ", giving you "s == s1: Ted", and then compares to see if THAT is equal to s1, which it is not.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 72
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
It has to do with operator precedences; + has a higher precedence than ==
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

Campbell Ritchie wrote:It has to do with operator precedences; + has a higher precedence than ==


Campbell,

Thank-you for explaining this. I looked at my precedence chart and saw you are correct. I could not figure out why it was doing this, printing false, when there was all sorts of other stuff going on in the output statement.

Regards,

Ted
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

fred rosenberger wrote:because when they are not in parenthesis, it first concatenates s to "s == s1: ", giving you "s == s1: Ted", and then compares to see if THAT is equal to s1, which it is not.


Fred,

Thank-you for explaining these steps. I was seriously completely baffled by why and SOP would simply print false. I think I invented my own extremely tough OCAJP7 exam question.

Regards,

Ted.

P.S. - Is anyone else that is reading this not receiving automatic notices to their e-mail when a reply is posted in the Java Ranch forum?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
But you should know not to use == anyway.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

Campbell Ritchie wrote:But you should know not to use == anyway.


I was trying to test the String pool component of Java... This is when two String objects are created through the assignment operator and the double quotes they will point to the same String object in the String constant pool, even across classes and packages - basically the entire JVM. Two String objects initialized this way will point to the same String object and evaluate to true when using the double equals == . It is also possible to explicitly put a String object in the constant String pool by invoking the intern() method from the String class.

String objects created with the new keyword will however always point to different objects.

I will have to read this link though. Thank-you for the reply Campbell. I wish I had e-mail updates for replies. Even though I have my settings on JavaRanch configured to send me e-mail updates when a reply is sent I am still not receiving them. :/

Regards,

Ted
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
You're welcome
I checked that your profile has e‑mail for replies enabled. I think you may have to tick a box when you start a thread to request e‑mails for that thread, too.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18109
    
    8

And don't forget to check your Spam folder.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

Campbell Ritchie wrote:You're welcome
I checked that your profile has e‑mail for replies enabled. I think you may have to tick a box when you start a thread to request e‑mails for that thread, too.


Thanks again Campbell. I will have to watch for that option the next time I start a new thread on the programming ranch here.

The article you linked to was interesting. I definitely learned some new things.

Respectfully,

Ted
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

Paul Clapham wrote:And don't forget to check your Spam folder.


Paul,

Thank-you for the tip. I just checked my Spam folder and sure enough I had a few messages from java ranch dot com. :D

Thanks again for your help.

Respectfully,

Ted
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
You're welcome
You were right to try out == just to see what happens when you have interned Strings (search for “Strings, literally” and you will find an old but very useful JavaRanch Journal article). Remember, “I only want to see what happens if…” is an adequate excuse for almost anything short of deleting all your system files
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

Campbell,

Thank-you for the reply. I will search for this article you mention to get even more java knowledge.

Also, yes I will have to be careful when experimenting with computer stuff. Luckily, I am not on Windows so deleting System32 is not an option.

It is awesome that java runs in a JVM and is not really even touching my operating-system though. Another win for 'sand-boxing'.

Thank-you for the reply.

Respectfully,

Ted
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

Campbell Ritchie wrote:You're welcome
(search for “Strings, literally” and you will find an old but very useful JavaRanch Journal article)


I tried searching for this and came up with about 172 pages worth of results all the way back to the beginning of the millennium (2000). I started to click through these pages and scroll the posts but quickly exhausted my ability to do this because of some sort of programmed time out with the web application software. So, if it is not too much trouble could you please link directly to this article? Or, could you specify who the author is, what forum it was posted in, and whether "Strings, literally" applies to the subject line only or to the main body of the message.

Thank-you for reading. I hope this is not annoying, especially since you are the Sheriff of these parts.

Regards,

Ted
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7014
    
  16

Ted North wrote:I tried searching for this and came up with about 172 pages worth of results...

Which probably reflects the number of times this sort of question is asked.

The fact is, I suspect you're obsessing about something that you really don't need to.

As long as you remember that Strings in Java are objects, and that you're probably better off not creating them if you don't need to, the fact is that, as long as you AvoidTheEqualityOperator (←click), or you create thousands (many thousands) of them, you're not likely to run into any problems.

You might find the CachedObjects page worth reading.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

Winston Gutkowski wrote:
Ted North wrote:I tried searching for this and came up with about 172 pages worth of results...

Which probably reflects the number of times this sort of question is asked.

The fact is, I suspect you're obsessing about something that you really don't need to.

As long as you remember that Strings in Java are objects, and that you're probably better off not creating them if you don't need to, the fact is that, as long as you AvoidTheEqualityOperator (←click), or you create thousands (many thousands) of them, you're not likely to run into any problems.

You might find the CachedObjects page worth reading.

Winston


Winston,

Thank-you for the links to more java ranch articles. Campbell already linked to the 'Avoid the Equality Operator' one, it is in his post if you click on the operator in question! -> ==.

I hope I do not seem obsessed...although being obsessed with java may not be so bad. I just want to make sure I am learning as much as I can. I should probably just blow off some stuff and only focus on what I really need to though to pass the OCAJP and live like I am on vacation. Although, maybe focusing on the String constant pool this much is overboard.

Thank-you again for helpful tips.

Respectfully,

Ted
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
On my first search page for “strings literally” it said something about “this article”, which you will find a link to from this thread.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 177
    
    1

Campbell Ritchie wrote:On my first search page for “strings literally” it said something about “this article”, which you will find a link to from this thread.


Campbell,

Thank-you for the direct link.

Regards,

Ted
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does this output (S.O.P.) statement print false when it is concatenated
 
Similar Threads
Doubt 1
string literal GC
String comparison
null exception!
String question