3, talk about the difference between & and && -Java answer interview questions

Common: & && and can be used as logic (and) operation, when the operator is true both sides operator only result is true, otherwise false.

Different points: && operator functional short circuit, i.e., to the left when the operator is true, the right operation is not performed, skip.

           & Operator functions not short-circuited, but can be used for the bit arithmetic.

 

Copy the code
package com.n3;

public class Demo {
public static void main(String[] args) {
System.out.println("true&true="+(true&true));
System.out.println("true&false="+(true&false));
System.out.println("false&false="+(false&false));
System.out.println("----------");
System.out.println("true&&true="+(true&&true));
System.out.println("true&&false="+(true&&false));
System.out.println("false&&false="+(false&&false));
System.out.println("----------");
int i=0;
System.out.println(false&++i==0);

输出结果:

true&true=true
true&false=false
false&false=false
----------
true&&true=true
true&&false=false
false&&false=false
----------
false
1
false
1
----------
false
0
false
1
----------
5&3=19
Copy the code

 

 

Output:

Copy the code
true&true=true
true&false=false
false&false=false
----------
true&&true=true
true&&false=false
false&&false=false
----------
false
1
false
1
----------
false
0
false
1
----------
5&3=19
Copy the code

 

 

 


System.out.println(i);
i=0;
System.out.println(true&++i==0);
System.out.println(i);
System.out.println("----------");
i=0;
System.out.println(false&&++i==0);
System.out.println(i);
i=0;
System.out.println(true&&++i==0);
System.out.println(i);
System.out.println("----------");
System.out.println("5&3="+(5&3));
/*
* 5二进制 :0000 0000 0000 0000 0000 0000 0000 0101
* 3二进制 :0000 0000 0000 0000 0000 0000 0000 0011
* 5&3 :0000 0000 0000 0000 0000 0000 0000 0001
* 1:可理解成true
* 0:可理解成false
*/
}
}

Guess you like

Origin www.cnblogs.com/helenwq/p/11646153.html