Short Circuit Operators ( &&, || )
These operators are exactly same as normal operator |, ‘&’ except the following difference.
General Operators ( &, | ) | Short Circuit Operator ( &&, || ) |
Both arguments should be evaluated always. | Second argument evaluation is optional. |
Relatively performance is low. | Performance is high. |
Applicable for both Boolean and integral types. | Applicable only for Boolean type. |
e1 && e2 ==> e2 will be evaluated if e1 is true.
e1 || e2 ==> e2 will be evaluated if e1 is false.
int x = 10 , y = 10 ;
if ( + + > 10 || + + y < 10 ) {
x + + ;
} else {
y + + ;
}
s.o.p ( x + "-----" + y ) ;
Cases:-
& | & & | | | | | |
x = 11 | x = 11 | x = 12 | x = 12 |
y = 12 | y = 12 | y = 11 | y = 10 |
int x = 10 ; if ( ( ++x < 10) && (x > 10) ) { System.out.println ( “Hello” ) ; } else { System.out.println ( “Hi” ) ; } Output Hi. | int x = 10; if ( ( ++x < 10) || (x > 10) ) { System.out.println ( “Hello” ) ; } else { System.out.println ( “Hi” ) ; } Output Hello. |
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.