-4
public boolean isValidCardDetails(CardDetailsTypeBean cardDetailsTypeBean) throws EnrollmentReqInvalidException {
        if (cardDetailsTypeBean.getCardNumber() == null || "".equals(cardDetailsTypeBean.getCardNumber())) {
            throw new EnrollmentReqInvalidException("ECDOO16", "card no is mandatory");
        }
        if (cardDetailsTypeBean.getNameOnCard() == null || "".equals(cardDetailsTypeBean.getNameOnCard())) {
            throw new EnrollmentReqInvalidException("ECDOO17", "name on card is mandatory");
        }
        if (cardDetailsTypeBean.getCvv() == 0 || "".equals(String.valueOf(cardDetailsTypeBean.getCvv()))) {
            throw new EnrollmentReqInvalidException("ECDOO18", "cvv is mandatory");
        }
        if (cardDetailsTypeBean.getExpDate() == null || "".equals(cardDetailsTypeBean.getExpDate())) {
            throw new EnrollmentReqInvalidException("ECDOO19", "exp date must be required");
        }
        return false;

    }

Well here i want to ask after getting card number and checking null,why we use "".equals there..?? can anyone explain me this? little confused?

  • Cause you are dealing with String value. it could be null and also could be an empty string. isnt it sounds natural ? – Shafin Mahmud Oct 29 '18 at 15:33
  • 1
    Possible duplicate of [What is the difference between == vs equals() in Java?](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java) – forgivenson Oct 29 '18 at 15:35
  • The conditions are checking for `null` first and then, if the object is initialized, they check for an empty `String`. – deHaar Oct 29 '18 at 15:35
  • null means there is no instance of a object, it has not bean instantiated and does not have a values. With equals you check if the values is "" (empty), so that means that it is instantiated but its value is "" (empty). – Paris Karagiannopoulos Oct 29 '18 at 15:36
  • 1
    Possible duplicate of [Check whether a string is not null and not empty](https://stackoverflow.com/questions/3598770/check-whether-a-string-is-not-null-and-not-empty) – Shafin Mahmud Oct 29 '18 at 15:36

3 Answers3

0

This line of code:

cardDetailsTypeBean.getCardNumber() == null || "".equals(cardDetailsTypeBean.getCardNumber())

simply verifies if cardNumber is null or if is equal to the empty string. Empty string is different from null value, so this code checks if every field read by a getter returns a non-empty, non-null value.

Lorelorelore
  • 3,335
  • 8
  • 29
  • 40
  • 1
    It's worth noting that because of the first condition, there is literally no benefit to the [Yoda condition](https://en.wikipedia.org/wiki/Yoda_conditions). It should be `cardDetailsTypeBean.getCardNumber().equals("")` – Michael Oct 29 '18 at 15:36
  • Better to do the reverse: `"".equals(cardDetailsTypeBean.getCardNumber())` actually performs correctly the check with no need of doing any nullcheck. – Lorelorelore Oct 29 '18 at 15:38
  • Or maybe using Apache Commons `StringUtils.isEmpty` – Lorelorelore Oct 29 '18 at 15:39
  • It doesn't. `"".equals(null)` would return false, which would not cause a failure in OP's validation. – Michael Oct 29 '18 at 15:39
0

It's superfluous, actually.

The reason for this is that the order of the equals statement on the other side of the logical comparison is guaranteed not to produce a NullPointerException, since String.equals(null) is engineered to produce false.

The reason it likely exists the way it does is that it's being made explicit that the code is checking for null and an empty string.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

"" isn't the same as null because "" is a String value. Your card number might instantiated with ""

null means the reference of card number has no value.

Hülya
  • 3,353
  • 2
  • 12
  • 19