• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Equals

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am confused with the following line code which i came across online. Can any one help what this line is doing



I understood the First condition. I am unable to follow the && condition. what exactly the equals method and ? is doing.

Thanks
Ram
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you familiar with the ternary operator?

x = boolean ? y : z;

Basically, if the boolean is true, then y is assigned to x. Otherwise, z is assigned to x.

The && is a short-circuiting "and" used between two booleans. If the first boolean evaluates to false, then it's impossible for the expression as a whole to be true, so it "short circuits" and the second is not evaluated. In this example, the short-circuiting is used to test whether paint.getSeq() returns a null value. If it's not null, then it can be dereferenced to call the equals method. But if it is null, then the short circuiting prevents a NullPointerException.

It appears that getSeq() is intended to return a String reference, because the equals method is checking it against a String literal (an empty String, in this case).

So if getSeq is not null, and not an empty String...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Basically, it is calling the getSeq() method of the paint object, which most likely returns a string. The reason I am guessing this is because it then calls the equals() method to compare it to another string.

Next, it is taking a NOT of the equals condition, with the "!" operator. Meaning it is checking to see if it is NOT equals to an empty string.

The next two terms are part of the ternary operator. If the condition is true the value is the first term, otherwise it is the second. So the value is either the string that is returned by calling paint.getPSeq(), or the string "A".

Henry
 
Ram Adsumalli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marck and Henry
reply
    Bookmark Topic Watch Topic
  • New Topic