我只是想知道为什么我们通常||在两个布尔值之间使用逻辑OR 而不是按位OR |,尽管它们都运行良好.
我的意思是,看看以下内容:
if(true | true) // pass
if(true | false) // pass
if(false | true) // pass
if(false | false) // no pass
Run Code Online (Sandbox Code Playgroud)
if(true || true) // pass
if(true || false) // pass
if(false || true) // pass
if(false || false) // no pass
Run Code Online (Sandbox Code Playgroud)
我们可以用|而不是||吗?同样的事情&和&&.
在调用对象的函数之前,我需要检查对象是否为null,以避免抛出一个NullPointerException.
最好的方法是什么?我考虑过这些方法.
哪一个是Java最好的编程实践?
// Method 1
if (foo != null) {
if (foo.bar()) {
etc...
}
}
// Method 2
if (foo != null ? foo.bar() : false) {
etc...
}
// Method 3
try {
if (foo.bar()) {
etc...
}
} catch (NullPointerException e) {
}
// Method 4 -- Would this work, or would it still call foo.bar()?
if (foo != null && foo.bar()) {
etc...
}
Run Code Online (Sandbox Code Playgroud) 我无法理解为什么跟随if块执行.如何评估if条件?
public class Test
{
public static void main(String[] args)
{
if (true || (false || true) && false)
{
System.out.println("How does this condition becomes true.");
}
if (false && (false || true) || true)
{
System.out.println("Same with this condition, why is it true.");
}
}
}
Run Code Online (Sandbox Code Playgroud) 如何将if中的条件语句以if的方式链接b,而不是检查c?
如果a并且c是假的,并且b是真的,c会被检查吗?
if (a || b || c)
Run Code Online (Sandbox Code Playgroud)
我在寻找类似的功能,PHP与持有之间的差异OR和||
在Java(Eclipse)中,当有这样的语句时if (true || false),它最终会成立,但问题是如果第一个是真的,编译器会评估第二个语句吗?这对我来说很重要,因为如果变量为null或者它有一定的值,我需要做一个操作.我的陈述看起来像if (array == null || array[i] < array[j]).你可以看到我的问题的原因,因为如果数组设置为null,那么第二个语句将产生错误.那么,真的是array == null足够还是编译器array[i] < array[j])也会评估呢?
对不起,如果这是重复,但我甚至想不出一种方法来搜索这个.我不确定要问这个问题的术语,所以我只会解决我的问题.
从我的测试来看,似乎if语句在尝试转到数组之前会退出[10].我总是这样吗?意思是,如果我在if语句中有一个&&而左侧是false,它会在测试第二个之前总是退出吗?就此而言,它会不会先测试左派?
public static void main(String[] args) {
boolean [] array = new boolean [10];
Arrays.fill(array, true);
int i = 10;
if(i < array.length && array[i]){
System.out.println("WhoHoo!");
}
}
Run Code Online (Sandbox Code Playgroud) public String generateURLSafeToken(String username, char[] password) throws CredentialTokenException {
this.tokenValid = false;
String token = null;
if ((username.length() < 1) || (username == null)) {
throw new CredentialTokenException("Username cannot be an empty string or null.");
}
if ((password.length < 1) || (password == null)) {
throw new CredentialTokenException("Password cannot be an empty or null.");
}
Run Code Online (Sandbox Code Playgroud)
我在第4行和第7行遇到此错误(用户名== null和密码== null)
我需要在我的代码中使用这一部分.我正在尝试isEmpty()而不是null,但也面临着问题.什么是解决此SONAR错误的替代方法或解决方案
用例是有一组方法需要根据前一个是否已经返回来执行true.例如:
class Test {
boolean method1() {...}
boolean method2() {...}
boolean method3() {...}
...
void callAll() {
if(method1()) {
if(method2() {
if(method3() {
...
}
}
} else {
error();
}
}
}
Run Code Online (Sandbox Code Playgroud)
else所有的ifs 必须有一个.有没有更好的方法来处理这种情况?
目前我正在开发一个具有内存密集检查过程的程序.在某些时候,代码看起来像这样.
if(isvalid() && false) //this false is acctually another method which will at this given
//point always return false
{
//rest ommited
}
Run Code Online (Sandbox Code Playgroud)
JVM是否总是检查第一个方法(isValid()),因为x && false是假的;
我不确定,因为调试调试器时每次迭代都跳转到isValid()方法.
"if-conditions"的顺序是否会影响性能?
在假设顺序重要的情况下,这段代码if (slowMethod() && fastMethod())会慢于if (fastMethod() && slowMethod()).
我假设顺序很重要,因为编译器不分析条件的执行时间.但我不确定.
java ×10
if-statement ×2
performance ×2
conditional ×1
eclipse ×1
jvm ×1
logical-or ×1
maven ×1
null ×1
sonarqube ×1