Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Why does it do this???

 
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When I run it the ouput is True and then Flase, but shouldn't it hit the .equals first and output Equals and False? If I comment out the if statement that displays True then equals is displayed. I thought it used the fist valid statement.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each of your "if" statements is independent of the others. They're three separate statements so each one of the conditions is evaluated and every one that applies is executed. For the first class, the string is first set to "Equals" because the first condition is true, but the method keeps on executing and the string is then set to "True" because that condition is true also.

I think you're thinking of how "if... else" behaves. The "else" keyword connects multiple if statements together so that only the first matching condition executes its statement:



Note that the last condition (if (a != b)) could be removed, and you'd just use the "else", since if the second condition isn't true the third one must be, making the last condition redundant.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James,

Ernest is perfectly right in his explanation. Just to elaborate further, Ernest mentioned that your second condition "if (a == b)" will also return True. If you are wondering why, then it's because in Java two strings that have the same "value" WILL point to the SAME object in memory (on the heap). So, in your code, in the first check, "a" and "b" have the same value and point to the same object. Hence a == b.

Hope this helps.

Ciao,
Jagan.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two literal Strings will refer to the same object -- two Strings that both appear as double-quoted text within the program. Strings created some other way -- for example, read as user input -- won't behave this way.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic