3

Actually I tried to change null check of particular string, previously

    {if (empName.equals("") || (empName.equals(null)))
//Do something 
}  

which was not a proper way to check a string is null, as

    While ( empName == null) 
     {if (empName.equals("") || (empName.trim().IsEmpty()==true))
//Do something 
}

But it didn't work.can anyone suggest What's wrong with this?

2
  • 1
    if(empName ==null || empName.isEmpty()){ ... do stuff } should work Commented Aug 13, 2015 at 0:09
  • Thanks for your immediate response.. But will this check for whitespace also @Angel koh Commented Aug 13, 2015 at 0:22

2 Answers 2

3

I tried to change null check of particular string, previously

if (empName.equals("") || (empName.equals(null)))

Two things are wrong with the above piece of code:

  • if empName is null, you get an exception
  • The call empName.equals(null) is required to return false, so it has no effect

A proper way of checking a string for being null or whitespace is as follows:

if (empName==null || empName.trim().isEmpty())

You do not need the == true part, because isEmpty() returns a boolean.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much @dasblinkenlight it worked. But what was wrong with my work
@ani The call empName.equals("") throws an exception when empName is null.
2

There are a couple of basic ideas that would be handy to have in mind here. These are:

  • Pointer Dereferencing

  • Short Circuit Evaluation

Pointer Dereferencing. In Java, types that are not primitive are called reference types. The variables for these types do not contain the value for the type, instead they contain a pointer to memory. For example:

String str;  

String is a reference type. Once initialized, the variable str will hold a reference or pointer to the memory location of the assigned String instance. Until initialized, variables for reference types have the value null.

Before any member of a class instance (eg. methods or fields) can be accessed and used, the pointer for a reference type must be accessed to get the location of the member in memory. This is called dereferencing. If a reference type variable contains the null value, then it is not possible to access any members. This is why an expression like this one is perilous:

`if (myString.equals("")) ...`

If the variable myString contains null or is not initialized, it cannot be dereferenced to a heap location. In this case an exception is raised and your program fails.

If you cannot guarantee that a String variable won't contain null then you should not use statements like the one above because it may raise a NullPointerException in your code.

Short Circuit Evaluation. Short circuit evaluation is where the evaluation of a multi-part expression is terminated before all of the parts have been evaluated -because- finishing the complete evaluation would not change the value of the expression. This most commonly applies to boolean expressions.

Consider an expression like this:

if (myString.trim().isEmpty()) { ... }

As discussed above, this expression leaves the program vulnerable to NullPointerExpressions whenever myString contains null and cannot be dereferenced. However this expression is safe:

if (myString == null || myString.trim().isEmpty) { ... }

In the above case, a NullPointerException can never be thrown by this expression, even if myString contains null. If myString is equal to null, the first part of the expression is true. In a boolean OR expression, true OR xx, the result of the expression is always true no matter what xx is. The Java environment therefore only executes the 1st part and not the second part. This is a good thing, because a NullPointerException would result if it had been.

Short circuit evaluation is commonly used in Java if-then expressions to test for null conditions that would raise exceptions in subsequent parts of a logical expression.

1 Comment

Thanks @scottb you made my java learning perfect . Will be needing your guidance throughout my java journey

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.