[] Operator must see White and expressions Detailed

Operators and Expressions

  1. Arithmetic
    1.1% operator.
    The Java addition to the usual arithmetic operators plus (+), subtract (-), multiply ( ), except (\), the modulo operation further comprises (%) and increment (+ ) and decrement (-) operations. +, - , / is relatively simple, you can experiment a bit.
    The modulo operation (%) means taking the remainder, and can only be applied to an integer type char. Sample code is as follows:
    1.// 255 outputs a remainder obtained by dividing 8.
    = 225 n-2.int;
    3.System.out.println (n-%. 8); // result is 1
    . 1.2 "++" and "-" operator
    Java-increment operator (+) and decrement operator (-) inherited from C ++, can make the value of the variable 1 is incremented or decremented, but is written before and after the variable variables have different effects:
    1. If written before variable, this variable indicates before adding or subtracting 1
    2. If written after the variable, this variable indicates after finished using plus or minus 1 a
    sample code is as follows:
    1.int A = 10, B = 20 is;
    2.int C1 = A ++; // first assign a value c1, and a further increase since
    3.int c2 = ++ b; // first the value of b is incremented and then assign C2
    4.System.out.println ( "a = "+ a +", b = "+ b +", c1 = "+ c1 +", c2 = "+ c2);
    results 5.// output is: a = 11, b = 21 , c1 = 10 , c2 = 21
  2. Relational operation
    2.1. Use relational operators
    in Java relational operators for determining the size relationship between the data, including greater than (>), less than (<), greater than or equal to (> =), less than or equal to (<=), equal to (==), not equal (! =) six operators.
    Often used in practical applications, it is assumed now want to achieve transfer function, enter the amount of the transfer, it is necessary to determine whether the amount transferred is greater than the sum of the current account, and that only two results, greater than or not more than, the java language, using boolean type to indicate whether this state, if the relationship established is true, otherwise false. Sample code is as follows:
    1.int max = 10;
    2.int NUM =. 9;
    3.boolean B1 = max> 15;
    4.boolean NUM = B2. 1% 2 ==;
    5.System.out.println (B1 ); // result to false
    6.System.out.println (B2); // result is true
  3. Logic operations
    3.1. Logic operation
    foregoing relational operators are used to compare the size relationship between the two values, the logical operator performs a logic operation is used, which is based on the above relational operator. When taking into account the need to consider two relational operators, logical operators may be used.
    Logical operators comprising: (&&), or (||) and not (!). Logic operations involved in both the boolean variables or expressions, calculation results of the boolean type.
    3.2 The use of "&&" operator
    two boolean variables involved, "&&" when the operation only when the two variables are true, the result of the operation was to true, otherwise the result is false. Sample code is as follows:
    1.int score = 80;
    2.boolean score = B> = 60 && score <90;
    3.System.out.println (B);
    4.// result is true, since the value of score greater than 60 and equal to satisfy these two conditions is less than 90, the logical expression "score> = 60" results "score <90" are true, it is the result of the && true.
    3.3. Use "||" operator
    when two boolean variables involved in "||" operation, when there is a two variables is true, the result is true, only when the two variables are false result is false. Sample code is as follows:
    1.boolean = In Flag to true;
    2.int = n-200 is;
    3.boolean In Flag || B1 = (n-> = 0 && n-<100);
    4.System.
    5.// result is true, the above code, the expression "flag || (n> = 0 && n <100)" means: when the flag is true or n is between 0 and 100 (n is greater than 0 and smaller than 100) is equal, the result is true, otherwise false. The values of n and the flag, the final result of the operation is true.
    3.4. Use "!" Operator
    "!" Operation is relatively simple, only a boolean variable involved in computing, calculating the value of the variable contrast, when the variable is true to false, the result is a variable is false to true. Sample code is as follows:
    1.boolean = In Flag to true;
    2.int = n-200 is;
    3.boolean In Flag || B = (n-> = 0 && n-<100);!
    4.System.out.println (B) ;
    5.// result is false, the above code, the expression "! flag || (n> = 0 && n <100)" means: when the flag is false or between 0 and 100 n ( when n is greater than or equal to 0 and less than 100), the result is true, otherwise false. The values of n and the flag, the final operation result is false.
    3.5. On "short circuit Logic"
    Java logical operators && and || have the characteristics of a short circuit, when the first relational expression can be judged that the whole expression, will not go behind the determination of two expressions.
    1. For the "&&", when the first operand is false, the second operand is not determined, because the second operand matter what the final result will be to false;
    2.
    Sample code is as follows:
    1.int I = 100, J = 200 is;
    2.boolean B1 = (I> J) && (I ++> 100);
    3.System.out.println (B1); // results: to false
    4.System.out.println (I); // results: 100, short circuit, i ++ is not executed
    5.boolean = I B2> 0 || J ++> 200 is;
    6.System.out.println (B2 ); // results: to true
    7.System.out.println (J); // result: 200, short circuit, j ++ will not be executed

Want more interview questions and learning materials can whisper to contact me, we can talk about!

Guess you like

Origin blog.51cto.com/14623707/2459432