"Java Puzzles" & and &&, | and | | Use

Source: Internet
Author: User


The code is as follows:

public class example042 {public static void  main (String[] args)  {int[][] tests = { { 6, 5, 4, 3,  2, 1 }, { 1, 2 }, { 1, 2, 3 },{ 1, 2,  3, 4 }, { 1 } }; System.out.println ("func1 out : "  + func1 (Tests));} PRIVATE STATIC INT FUNC1 (int[][] tests)  {int successcount = 0;try  {int i = 0;while  (True)  {if  (ThirdElementIsThree1 (tests[i++]))  { successcount++;}}}  catch  (arrayindexoutofboundsexception e)  {// No more tests to  Process}return successcount;} Private static boolean thirdelementisthree1 (Int[] a)  {return a.length >=  3 & a[2] == 3;}} 


Output Result:

Func1 out:0


Results Analysis:

The above code has two very serious bugs. The first bug is the use of conditions that may produce exceptions for loop control, and the second is to use "&" and "&&" incorrectly.

Using conditions that can produce anomalies is not only difficult to read, but also very slow to run. do not use exceptions for loop control, and exceptions should be used only for exception conditions . You can use the following code to override Func1:

private static int func1 (int[][] tests) {int successcount = 0;try {for (int[] test:tests) {if (ThirdElementIsThree1 (test ) {successcount++;}}} catch (ArrayIndexOutOfBoundsException e) {e.printstacktrace ();} return successcount;}

The corrected code output still has problems, and it captures the exception that the array is out of bounds. The problem occurs on the ThirdElementIsThree1 method. This method mistakenly used "&", originally here is required to use "&&", as follows:

private static Boolean thirdElementIsThree1 (Int[] a) {return a.length >= 3 && a[2] = = 3;}

Why do you use "&" to throw an exception? Causes the output of the result to be 0 in func1? the & operator has other meanings. In addition to the common bit and operator, which is treated as an integer operand, its functionality is overloaded to the logical AND operator when it is used for Boolean operands. This operator differs from the more frequently used condition and operator, and the,& operator always calculates its two operands, while the && operator no longer calculates the operand to the right when its left operand is evaluated to false. Therefore, the ThirdElementIsThree1 method always tries to access the third element of its array parameter, even if the array parameter has less than 3 elements.

Also , a logical OR operator (|) is accompanied by the conditional or operator (| |), |  The operator always computes its two operands, and | | Operator no longer calculates the operand to the right when the operand to its left is evaluated as true.


note: This "Java Confusion" series are bloggers reading "Java doubts" The original book after the interpretation of the original book and examples of the adaptation and then write a post to publish. All examples are personally tested and shared on GitHub. Use these examples to motivate yourself to benefit others. At the same time, all the posts in this series will be published in the blogger personal public search "Love Ape" or "ape_it" to facilitate reading. If there is any infringement of the original rights of the author, please inform the blogger in time to delete if the reader has objections to the content of the text or questions are welcome through the blog post or public messages and other ways to discuss together.

Source code Address Https://github.com/rocwinger/java-disabuse


This article from "Winger" blog, declined reprint!

"Java Puzzles" & and &&, | and | | Use

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.