Basic Java syntax

Source: Internet
Author: User
1. Differences between & and:

Similarities:

& And & can both be used as operators of logic and, indicating logic and (and ). When the expression on both sides of the operator returns true. The result of the entire operation is true. Otherwise, if either of them is false, the result is false.

Differences:

(1) & it also has the short circuit function, that is, if the first expression is false, the second expression is not calculated. For example, for if (STR! = NULL &&! Str. Equals ("") expression. When STR is null, the following expression is not executed, so nullpointerexception is not thrown if & is changed. If (x = 33 & ++ y> 0) y will increase, if (x = 33 & ++ y> 0) will not increase

(2) & it can also be used as a bitwise operator. When the expressions on both sides of the & operator are not 'boolean' type, & it indicates bitwise AND operation. We usually use 0x0f to perform & operation with an integer, for example, 0x31 & 0x0f returns 0x01.

2. How to jump out of the current multi-nested loop in Java? In Java, to jump out of multiple loops, you can define a label before an External Loop statement and Code Use a break statement with a label to exit the outer loop. For example,
 
OK:
 
For (INT I = 0; I <10; I ++) {for (Int J = 0; j <10; j ++) {system. out. println ("I =" + I + ", j =" + J); If (j = 5) Break OK ;}}

In addition, I usually do not use labels. Instead, the results of the outer loop condition expressions can be controlled by the code of the internal loop body. For example, you need to find a number in a two-dimensional array.

 
Int arr [] [] = {1, 2, 3}, {4, 5, 6, 7}, {9}; Boolean found = false; For (INT I = 0; I <arr. length &&! Found; I ++) {for (Int J = 0; j <arr [I]. length; j ++) {system. out. println ("I =" + I + ", j =" + J); If (ARR [I] [J] = 5) {found = true; break ;}}}

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.