Differences and instances between java and

  • 2020-05-12 02:35:31
  • OfStack

The difference between java & & and & : & is both a bit operator and a logical operator, and the two sides of & can be int or boolean expressions. When the two sides of & are int, the Numbers on both sides of the operator must be converted to base 2 before operation, while the two sides of the short circuit and (& &) must be Boolean expressions.

I think a lot of people will encounter it when they are studying java or in the interview

& and &&

However, if you don't really understand what they mean, it can lead to a lot of trouble with your thinking

In this blog, when you're done reading it, it's easy to tell them apart

Here's my demo



 /**
   *
   */
  package com.b510.test;
 
  /**
   * @author Jone Hongten
   * @create date : 2013-11-2
   * @version 1.0
  */
 public class Test {
 
     public static void main(String[] args) {
         String str = null;
        if(str != null && !"".equals(str)){
             //do something
         }
         if(str != null & !"".equals(str)){
             //do something
         }
     }
 }

We may have a bit of a blur now, but let's look at the ampersand and ampersand circuitry first:

For: &&

if(str != null && !"".equals(str))

When: str! = null, then it will be executed:!" "equals (str)

If: str! = null = false, then at this time, the program is in a short circuit, then,!" ".equals (str) will not be executed.

But for: &

if(str != null & !"".equals(str))

No matter: str! = "null" (true, false), the program will execute:!" "equal (str)

Summary of circuit problems:

For: & -- > Either way, it's going to execute the left and right ampersand, right

For: && -- > Only when the program to the left of the symbol "&&" is true (true) will the program to the right of the symbol "&&" be executed.

Here are the rules:

For: & -- > As long as one is false on the left and one on the right, it is false; It's only going to be true if it's all true

For: && -- > As long as the left side of the symbol is false, the result is false; When the left side is true and the right side is true, the result is true

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: