Dark Horse Programmer--java Basic Learning Note 3

Source: Internet
Author: User
Tags bitwise operators logical operators

Dark Horse Programmer--java Basic Learning Note 3I. Summary of the contents of the notes:comparison operators & logical operators, bitwise operators, shift operators, ternary operators, if statements, local code blocks, switch statements, while statements, do-while statements, for loop statements, loop structure usage scenarios. second, the introduction of common content:1.>>> Unsigned Right shift, the data to move to the right, the high position of the vacancy, regardless of the original highest bit is what, empty space with 0 complement. features of the 2.^ operator:true ^ true = false;true ^ false = true;false ^ true = true;true ^ true = false;^ The result is the same on both sides of the symbol, and the result is false. the 3.& and && results are the same, but there are small differences in the operation process:3>5 && 3<6 3>5 & 3<6&: Regardless of the result on the left, the right side is involved in the operation. &&: When the left is false, the right side does not participate in the operation, improving the efficiency of the operation. the difference between a 4.if statement and a switch statement:if:A. Determine the specific value. B. Judge the interval. C. The expression that the result of the operation is a Boolean type is judged. Switch:A. Judge the specific value. B. The number of values is usually fixed. for several fixed values, it is recommended that you use a switch statement, because the switch statement loads the specific answer into memory, which is relatively efficient. In general, however, the IF statement is used because the switch statement has poor extensibility and trouble writing (more keywords), and if statements are relatively flexible, the if statement is often developed. the difference between 5.while and for:while and for can be interchanged, the difference is that the for variable defined for the loop is freed in memory after the for loop, while the variable used by the while loop can continue to be used after the loop ends. Order of 6.for loop execution:for (initialization expression; cyclic conditional expression; post-loop action expression)


Iii. Classic examples and explanations:

1. Analyze the following shift operations in accordance with the characteristics of traditional operations:

Package com.date3;/** * In fact, after many debugs, the speed is almost the same, but theoretically the shift operator is more efficient.  *  */public class Demo2 {public static void main (string[] args) {method1 ();} private static void Method2 () {Long time1 = System.currenttimemillis (); int x = 0;for (int i=0;i<10000000;i++) {x = 2 * 8; }long time2 = System.currenttimemillis (); System.out.print (x+ "\ t"); System.out.println ("Traditional operation mode:" + (TIME2-TIME1));} /** * uses the shift operator to handle */private static void Method1 () {Long time1 = System.currenttimemillis (); int x = 0;for (int i=0;i<100000 00;i++) {x = 2 << 3;} Long time2 = System.currenttimemillis (); System.out.print (x+ "\ t"); SYSTEM.OUT.PRINTLN ("Shift operation Mode" + (TIME2-TIME1));}}
2. Understand the following three different ways to exchange values:

Package com.date3;/** * Exchange value A, a value of three ways  */public class Demo3 {public static void main (string[] args) {method3 ();} Mode one: Use intermediate value private static void Method1 () {int a = 3,b = 5;int C;c = A;a = B;b = C; System.out.println ("a=" +a+ ", b=" +b);} Mode two: Do not use intermediate values (this method is not recommended, may overflow) private static void Method2 () {int a = 3,b = 5;a = a + b;b = A-b;a = A-B; System.out.println ("a=" +a+ ", b=" +b);} Mode three: Use XOR operator ^   a^b^b = a;private static void Method3 () {int a = 3,b = 5;a = a ^ b;b = a ^ B;a = a ^ b; System.out.println ("a=" +a+ ", b=" +b);}}
3. Understand the local code block and if (true); Features of the {} mode:

Package com.date3;/** * Due to if (true), the sentence is followed by a semicolon, the judgment will be executed, * But the result of the judgment will not affect the following local code block execution. * The following statement is a local code block that will automatically load and run, and the local code block can be used to free memory.  There are only four types of *switch statement choices: Byte,short,int,char. */public class Demo4 {public static void main (string[] args) {int x;if (true); { System.out.println ("Hello world!"); x = 5;} SYSTEM.OUT.PRINTLN (x);}}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse Programmer--java Basic Learning Note 3

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.