Difference between && and & in java - Java @ Desk

Thursday, May 30, 2013

Difference between && and & in java

Consider the following code:
String test = null;
if(test != null && test.equals(“”)) {

}




&& double ampersand works in following manner
1) First it checks for test != null,if this returns false it will not execute second part test.equals(“”)
2) This saves time and memory

& single ampersand works in following manner
1) First it checks for test != null,if this returns false it will execute second part test.equals(“”)
2) This eventually will lead to a NullPointerException plus the overhead of checking all the conditions even if previous fails






No comments:

Post a Comment