今天给大家介绍一下:“&”,“|”,“&&”,“||”运算.
“&”:与运算符
“|”:或运算符
“&&”:逻辑与
“||” :逻辑或
给大家看一个简单的demo,然后自己跑一下,就知道了
/** * 运算符"&","|","&&","||"来检测短路运算符 * @author potter */ public class test { public static int a=1; public static int b=1; public static void main(String[] args) { if(a<b &check()){ System.out.println("Oh,That's Impossible!!!"); }else{ System.out.println("That's in my control."); } } public static Boolean check(){ System.out.println("run check"); if(a>b){ return true; }else{ return false; } } }
结果得出:
run check
That's in my control.
所以真正的短路运算符为“&&”和“||”,而“&”和“|”会运行所有的表达式后才返回值。
为了提升效率,介意使用短路运算符
1164

被折叠的 条评论
为什么被折叠?



