Java Common operator collation

Source: Internet
Author: User
Tags bitwise operators

This article is in the study summary, welcome reprint but please specify Source: http://blog.csdn.net/pistolove/article/details/44724267


This article focuses on the operators that are common in Java, with emphasis on logical and bitwise operators. I hope to be of some help to you.


Arithmetic Operators

(1) Unary operators:
Positive (+), negative (-), plus 1 (+ +) and minus 1 (--) 4.
The plus 1, minus 1 operators allow only variables that are used for numeric types and are not allowed in an expression. The addition of 1, minus 1 operators can be placed before the variable (such as ++i), but also placed after the variable (such as i++), the difference is: if placed before the variable (such as ++i), the value of the variable is added 1 or minus 1, and then other appropriate operation (mainly assignment), if placed after the variable (such as i++) Then perform the other appropriate actions before you add 1 or minus 1 to the value of the variable.
For example:
int i=6,j,k,m,n;
j = +i; Take the original value, i.e. j=6
K =-I.; Take a negative value, i.e. k=-6
m = i++; First M=i, then i=i+1, that is m=6,i=7
m = ++i; First i=i+1, then M=i, that is i=7,m=7
n = j--; First N=j, then J=j-1, that is n=6,j=5
n =--j; First J=j-1, then N=j, that is j=5,n=5
It is also important to note that there are no spaces allowed between the unary operators and the operands before or after the unary operator, or the compile error occurs. Binary operators are not discussed with less.

Relational OperatorsThe relational operator is used to compare the size between two numeric values, which results in a numeric value of a logical type. There are six relational operators: equal to (= =), not equal to (! =), greater than (>), greater than or equal (>=), less than (<), less than or equal (<=). Because it is relatively simple, it is not discussed here.

logical OperatorsA logical operator requires that the data type of the operand be a logical type, and that the result of the operation is also a logical value. Logical operators are: Logic and (&&), logic, or (| | ), logical non (!), logical XOR (^), Logic and (&), logic, or (|).
/** * * @author liqq */@Testpublic void Testlogic () {//&& logic and (short circuit) for A&&b when a is false, it is not necessary to determine whether B is Falsesystem . ERR.PRINTLN (False && true); FalseSystem.err.println (False && false); FalseSystem.err.println (True && true); TrueSystem.err.println ();//| | Logic or (short circuit) for a| | b When B is true, it is not necessary to determine whether B is trueSystem.err.println (false | | true); TrueSystem.err.println (True | | true); TrueSystem.err.println (False | | false); FalseSystem.err.println ();//! Logical Non-SYSTEM.ERR.PRINTLN (!true); FalseSystem.err.println (!false); TrueSystem.err.println ();//^ Logical XOR (the same as false, different is true) System.err.println (false ^ true); TrueSystem.err.println (true ^ true); FalseSystem.err.println (false ^ false); FalseSystem.err.println ();//& Logic and (no short circuit) for A&b when B is false, you still need to determine whether B is FalseSystem.err.println (False & True) ; FalseSystem.err.println (True & True); TrueSystem.err.println (False & False); FalseSystem.err.println ();//| Logical OR (no short circuit) for A|b when A is true, you still need to determine whether BIs TrueSystem.err.println (false | true); TrueSystem.err.println (True & True); TrueSystem.err.println (False & False); FalseSystem.err.println ();}

Bitwise Operatorsbitwise operations are operations performed in bits, with operands and results of integer values.
There are 7 bitwise operators, namely: Bit and (&), bit or (|), bit non (~), bit xor (^), right Shift (>>), left shift (<<), 0 padding right shift (>>>).
/** * @author liqq */@Testpublic void Testbit () {//Bit with (&) and logic similar to SYSTEM.ERR.PRINTLN (1 & 0);//0system.err.println ( 1 & 1);//1system.err.println (0 & 0);//0system.err.println ();//bit or (|) and logic or similar System.err.println (1 | 0);//1System . ERR.PRINTLN (1 | 1);//1system.err.println (0 | 0);//0system.err.println ();//bit non (~) System.err.println (~0); -1system.err.println (to),/// -2SYSTEM.ERR.PRINTLN (),///or (^) the same as 0, different from 1system.err.println (1 ^ 0);// 1system.err.println (1 ^ 1);//0system.err.println (0 ^ 1);//1system.err.println ();//Right Shift (>>) Right shift value becomes smaller, Halve the number of SYSTEM.ERR.PRINTLN (2 >> 1) per move,//1system.err.println (2 >> 2);//0system.err.println (8 >> 1);// 4SYSTEM.ERR.PRINTLN (8 >> 2);//2system.err.println ();//Left Shift (<<) The left shift value is larger, one value per shift is multiplied System.err.println (2 < < 1);//4system.err.println (2 << 2);//8system.err.println (8 << 1);//16system.err.println (8 << 2) ;//32system.err.println ();//0 fill the right shift (>>>) unsigned, the right vacated bits are filled with 0 System.err.println (8>>&GT;2);//2 System.err.println ( -14 >>> 2); 1073741820/* (i.e., binary 11111111 11111111 11111111 11110010), shift two bits to the right after 1073741820 (i.e. binary 00111111 11111111 11111111 11111100) */system.err.println ();}

Java Common operator collation

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.