0

I am having trouble making sense of "publishResultCode", can someone please break down the meaning of that line and the operators?

String publishResultCode = value == null ? null : value.toString();
if ("SUCCESS".equals(publishResultCode)) {
  return true;
stfudonny
  • 53
  • 1
  • 9

4 Answers4

3

Ok, so first let's clear up order of operations

String publishResultCode = ((value == null) ? null : value.toString());

Now the == operator you know is just comparison. The question probably arises with the ? : operator. Generally A ? B : C means "if A is true, return B; otherwise return C". And lastly, of course = is assignment.

So: If value is null, set publishResultCode to null; but if value is not null, set publishResultCode to the value returned by calling value.toString().

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
2
if(value==null)
    publishResultCode=null;

else publishResultCode=value.toString();
John Kane
  • 4,383
  • 1
  • 24
  • 42
1

This sets String publishResultCode as null if value is null and sets it as value if it isn't. The code then returns if publishResultsCode is the word "SUCCESS". The question mark after value==null is basically an if-else statement,if value ==null then publishResultCode = null ,else publishResultCode = value.

Ali Camilletti
  • 116
  • 1
  • 3
  • 11
1

I think deep down you need just this :D

return value != null && "SUCCESS".equals(value.toString());
Parag Kutarekar
  • 975
  • 11
  • 10
  • Maybe. He didn't show what happens if `"SUCCESS".equals(value.toString());` returns false. Maybe it doesn't immediately return. And if it does, `return "SUCCESS".equals(value.toString());` would suffice, as this will already be false if `value` is null – Mark Adelsberger Feb 13 '17 at 19:57
  • Maybe +1. Also dont we wanna avoid NullPointer and return false when value is null? (If that is what suppose to happen) – Parag Kutarekar Feb 14 '17 at 14:08
  • Hm. I was looking at what `"SUCCESS".equals()` would do and neglected that a method call was required. Yeah, the extra check is worth while – Mark Adelsberger Feb 14 '17 at 14:24