& 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 &&