Java Basics Interview question: Talk about the difference between & and &&

Source: Internet
Author: User

& and && are both logical and

The difference is that both sides of the & judgment are to be carried out, and the && is short-circuit with, when && left condition is false then no longer judge the right condition, so the efficiency is higher

For example, for an if (str = = null &&!str.equals (")) expression, when STR is NULL, subsequent expressions do not execute, so the nullpointerexception does not appear if you change && to &, the NullPointerException exception is thrown. if (x==33 & ++y>0) y grows, if (x==33 && ++y>0) does not grow

& can also be used as a bitwise operator, when an expression on either side of the & operator is not a Boolean,,& represents bitwise AND operation, and we typically use 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of that integer, for example, 0x31 & The result of 0x0f is 0x01.

 Package Com.swift;  Public class and_test {    publicstaticvoid  main (string[] args) {        /*           * &  and &&         * *        String str=null ;         if null &!str.equals ("")) {            System.out.println ("There is an exception because STR has no space to open");     }}}

Bitwise-AND

 PackageCom.swift; Public classAnd_test { Public Static voidMain (string[] args) {/** & and &&*/String str=NULL; if(str! =NULL&&!str.equals ("") {System.out.println ("Is there an anomaly?" "); } System.out.println (Integer.tohexstring (0X3A + 0x45)); System.out.println (Integer.tohexstring (0x31 & 0x0f)); }}

Java Basics Interview question: Talk about the difference between & and &&

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.