Operator uses Java, monocular, binary operator, logical operators, etc.

Preface:

     Java provides abundant operators such as arithmetic operators, relational operators, logic operators, bitwise operators and the like;

     Wherein further divided unary and binary operator ;

One, unary operators: only a single operand (variable) to operate;

Wherein -------- increment, decrement operators +, - is a typical unary operator;

Increment, decrement operator using characteristics:

1, they can be placed in the use of yuan before the operation can be placed after the operand, but implied meaning is not the same;

        --- When placed before the operand , as ++ x; (--x;) prior to use on behalf of x, so that the value adding 1 to the X (minus 1); The following code demonstrates:

public static void main(String[] args) {
		int x = 1;
		++x;
		System.out.println(x);  //输出x为2
		--x;
		System.out.println(x);  //输出x为1
	        int y = 10;
		System.out.println(y);  //输出y为10
		y = ++x;                //x是先进行加1操作,再将值赋给y;
		System.out.println(y);  //输出y为2
		System.out.println(x);  //输出x为2
	}

        --- When placed after the operand , such as x ++; (x--;) x represents after use, so that the value of x and then adding 1 (minus 1); The following code demonstrates:

public static void main(String[] args) {
		int x = 1;
		x++;
		System.out.println(x);  //输出x为2
		x--;
		System.out.println(x);  //输出x为1
		int y = 10;
		System.out.println(y);  //输出y为10
		y = x++;                //x是先将值赋给y之后,才进行加1操作的;
		System.out.println(y);  //输出y为1
		System.out.println(x);  //输出x为2
	}

2, operand type must be an integer or floating point type variable, the value of the variable role is to add or subtract 1;

Second, the binary operator: while two operand (variable) to operate;

-------- binary operator include:

Subtraction operator +,-
Multiplication and division operator *,/
Remainder operator %
Relational Operators > , < , >= , <= , == , !=
Assignment Operators =

Note that wherein: logical operators  &&, ||,! 

The logical operators && and || is the binary operator;

The logical operator ! Is a unary operator;

An arithmetic operator comprising: a subtraction operator, multiplication and division operator, modulo operator;

Subtraction operators: +, -;

1, the order of binding is calculated from left to right ; for example: 5-5 + 15; 15 + 5 is to calculate, at minus 5 to 20, the final result is 15;

2, subtraction operator operand floating point or integer type data type;

3, the priority level for the subtraction operator 4;

Multiplication and division operators: *, /;

1, the order of binding is calculated from left to right ; for example: 2 * 8/4; 2 * 8 is to calculate, in addition to the 16 4, the final result is 4;

2, multiplication and division operator operand floating point or integer type data type;

3, multiplication and division operator priority level 3;

Remainder operator:%;

1, the order of binding is calculated from left to right ; for example: 8% 2 * 2; 2 * 8 is calculated first, performed modulo 2 16, the final result is 0;

2, the remainder operator operand floating point or integer type data type;

3, the remainder operator priority level 3;

Arithmetic expression means: linking formula in line with the rules of Java syntax with arithmetic operators and parentheses;

Such as: x + 2 * y + 10 + 5 * (z + 20);

Relationship operators:!>, <,> =, <=, ==, =; (greater than, less than, greater than or equal, less than equal to, equal to, not equal)

1, the result of the operation relational operator is boolean type, not true, that is false, when the establishment of the corresponding relational operators, true, or is false;

Such as: 5> 10; relationship is not established, the result is false; 5> 1; relationship is established, the result is true;

2, the relational operators, relational operators Size:>, <,> =, <= 6 priority;

3, ! In the relational operators, relational operators is equal to: ==, priority = 7;

4, the order of binding is calculated from left to right in;

Refers to a relational expression: the relational expression results formed is numeric variable (x + y) or expression (4 + 5) through a relational operator;

The x + y> 4 + 5;

Assignment operator: =;

1, for the assignment operator, the right operand must be a variable, not a constant or expression;

Such as: x = 10; y = 11; correctly, is not 10 = x; 11 = y;

2, the priority assignment operator is 14;

3, the calculated binding sequence is from right to left of;

4, the assignment operator can not be confused with the relational operator == equality relation operator; = 12, such as 12; this is incorrect;

Logical operators: &&, ||,; (logical AND, logical OR, logical NOT)!

 1, the logical AND (&&) priority 11, when the operator of the left and right sides of expressions are true, the entire value of the expression will be true;

Such as: if (top1> 0 && top2> 5) When the top1> 0 is true, top2> 5 is true, then if the value of the expression to be true;

Extended: if (top1> 10 && top2 <50) When the top1> 10 is false, the determination is not to continue top2 <50, the expression is false by default; demonstrate the following code:

int top1 = 1 , top2 = 2;
if(top1>10 && top2<50){
    System.out.println(top1);  //逻辑表达式为真则输出top1
}else{
    System.out.println(top2);  //逻辑表达式为假则输出top2
}

2, logical OR (||) priority 12 , the expression for the left and right sides of the operator, when one side of the expression is true, the overall value of the expression is also true;

Such as: if (top1> 10 || top2> 15) When the top1> 10 is false, continues to top2> 15 judges, when top2> 15 is true, then the whole expression is true;

Extended: if (top1> 10 || top2> 50) When the top1> 10 is true, is determined not to continue top1> 50, the expression is true by default; demonstrate the following code:

int top1 = 100 , top2 = 20;
if(top1>10 || top2>50){
    System.out.println(top1);  //逻辑表达式为真则输出top1
}else{
    System.out.println(top2);  //逻辑表达式为假则输出top2
}

3, logic NOT (!) Priority 2, the value of the logical expression becomes opposite value;

The following code demonstrates: top1> 10 is true, then use the logical NOT (top1> 10), false, the final result output 20;!

int top1 = 100 , top2 = 20;
if( !(top1>10) ){
    System.out.println(top1);  //逻辑表达式为真则输出top1
}else{
    System.out.println(top2);  //逻辑表达式为假则输出top2
}

 

Published 26 original articles · won praise 7 · views 3058

Guess you like

Origin blog.csdn.net/LagerSwan/article/details/104080208