public class j68 {
public static void main(String args[]){
System.out.print(1==2||true);
}
}
public class j68 {
public static void main(String args[]){
System.out.print(1==2|true);
}
}
以上两者都打印为true。这意味着我们可以使用|而不是|| ?
答案 0 :(得分:0)
是和否,但大多数情况下没有,原因有两个。
|
(按位)和||
(逻辑)之间存在很大差异:使用按位|
,两个操作数始终会被评估。 (我不认为有任何保证首先评估哪个;至少,我没有立即在JLS§15.22.2看到一个,但也许有保证其他。)使用逻辑||
,首先评估左侧操作数,然后如果左侧结果为假,则右侧评估 。
如果您正在采取防御措施,这可能很重要,例如:
boolean flag = obj == null || obj.flag;
如果|
||
为obj
,我们使用null
代替NullPointerException
时会失败;我们得到|
。
另外,还有其他人必须阅读并使用您的代码的问题,并且使用||
通常使用.center{
text-align:center;
width:200px;
height:100px;
background-color:red;
position:absolute;
top: 50%;
left:50%;
margin-left:-100px;
margin-top:-50px;
}
.outter{
width:100%;
height: 500px;;
background-color:black;
}
<div class="outter">
<div class="center">
<h1>Mywebsite</h1>
</div>
</div>
会令人惊讶。但这是一种风格问题。