Basic Java Operators

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators logical operators

The following is referenced from http://wiki.jikexueyuan.com/project/java/basic-operators.html:

Java provides a rich set of operators for manipulating variables. You can divide all the Java operators into the following groups:

    • Arithmetic operators
    • Relational operators
    • Bitwise operators
    • logical operators
    • Assignment operators
    • Other operators

First, arithmetic operators

The use of arithmetic operators in mathematical expressions is the same as their use in algebra. The following table lists the arithmetic operators:

Assuming that the global variable A has 10 and the variable B has 20, then:

Example:

operator Description Example
+ Addition – increases at the other end of the operator A+b is 30
- Subtraction – Subtracts the operand on the right from the operand on the left A-B is-10
* Multiplication – Multiplies the values at both ends of the operator A*b is 200
/ Division – with the right operand in addition to the left operand B/A is 2
% Remainder-the left operand is removed with the right operand and the remainder is returned B%a is 0
++ Increment – Adds 1 to the value of the operand b++ is 21
-- Decrement – The value of the operand minus 1 b--is 19

Second, relational operators

The following are the relational operators that the Java language can support.
Assuming that the variable a has 10 and the variable B has 20, then:

Example:

operator Description Example
== Checks whether the values of the two operands are equal, and if so the condition is true (A==B) is not true.
!= Check that the values of both operands are equal, and if not equal then the condition is true (a!= B) is true.
> Checks if the left operand is greater than the operand on the right, and if it is greater then the condition is true (A>B) is not true.
< Checks if the left operand is less than the right operand, if less than the condition is true (A<b) is true.
>= Check that the left operand is greater than or equal to the right of the operand, and if so the condition is true (A>=B) is not true.
<= Checks if the left operand is less than or equal to the right operand, and if so the condition is true (A<=b) is true.

Third, bitwise operators

Java defines several operators that can be used for integer types, Long,int,short,char, and Byte.

Bitwise Operators Act on the transfer criteria between binary systems and perform bitwise operations. Suppose that if a equals 60;b equals 13, now in binary type they are as follows:

a = 0011 1100b = 0000 1101-----------------a&b = 0000 1100a|b = 0011 1101a^b = 0011 0001~a  = 1100 0011  

The following table lists the bitwise operators:

Assume that the integer variable A has 60, B has 13, then:

Example:

(~a) is-61, because it is a signed binary number, the 2 complement form is 1100 0011 The
operator Description Example
& binary and operator copy one on result if the two operands exist at the same time (a&b) is 12, that is 0000 1100
| binary or operator copies one on the result if one exists on any of the operands (a| B) for 61, i.e. 0011 1101
^ Binary xor operator to copy bits if it is set on an operand instead of two. (a^b) is 49, which is 0011 0001
~ binary complement operator is unary, B and has the effect of "flip" bit
<< binary left shift operator. The value of the left operand is moved to the left based on the number of bits specified by the right-hand operand. a<<2 is 240, which is 1111 0000
>> binary right-shift operator. The value of the left operand is moved to the right based on the number of bits specified by the right-hand operand. a>>2 is 15, which is 1111
>>> Right-shift 0 operator. The value of the left operand is moved to the right based on the number of bits specified by the right operand, and the transferred value is filled with zeros. a>>>2 is 15, which is 0000 1111

Four, logical operators

The following table lists the logical operators:

Assuming that the Boolean mathematical system variable A is true and B is false, then:

Example:

operator Description Example
&& Called Logic and operators. If neither of the operands is zero, then the condition is true. (A&&B) is false.
|| is called a logical OR operator. If neither of the operands is zero, then the condition is true. (a| | B) is true.
! is called a logical non-operator. The logical state used as the number of rollover operands. If a condition is true, then the logical non-operator is false. ! (A&&b) is true.

Five, assignment operators

The following are the assignment operators supported by the Java language:

Example:

operator Description Example
= Simple and operator to assign the value of the right operand to the left operand C=a+b will assign the value of A+b to C
+= Increment and assignment operators, which increase the operand on the right to the left operand and the result to the left operand C+=a equal to C=c+a
-= Subtract and assign operator, which subtracts the right operand from the left operand and assigns the result to the left operand C-=a equal to C=c-a
*= Multiply and assign operators, which multiply the right operand to the left and assign the result to the left operand C*=a equal to C=c*a
/= In addition to the assignment operator, it adds the right operand to the left operand and assigns the result to the left operand C/=a equal to C=C/A
%= Coefficients and assignment operators require a factor that employs two operands and assigns the result to the left operand C%=a equal to C=c%a
<<= Left Shift and assignment operators c<<=2 equal to C=c<<2
>>= Right Shift and assignment operators c>>=2 equal to C=c>>2
&= Bitwise AND Assignment operators c&=2 equal to C=c&2
^= Bitwise XOR and Assignment operators c^=2 equal to C=c^2
|= bitwise can concurrently OR and assignment operators c|=2 equal to C=c|2

Vi. Other operators

The following are some of the other operators supported by the Java language:

The conditional operator/three-mesh operator (? :)

The conditional operator is also called the ternary operator. This operator consists of three operands and is used to evaluate the representation of a Boolean mathematical system. The purpose of this operator is to determine which values should be assigned to the variable. This operator is written as follows:

if true if false

The following is an example:

 Public class Test {   publicstaticvoid  main (String args[]) {      int A, B;       = ten;       = (A = = 1)? 20:30;      " Value of B is: "+  b);       = (A = = 10)? 20:30;      " Value of B is: "+ B);   }} // This will result in the following: Value of B is:3020

instanceof characters

This operator is used only for object reference variables. This operator checks whether an object is a unique type (type or interface type). The instanceof operator is written as:

instanceof  (class/interface type)

The result is true if the type or interface type to the right of the object to which the variable is referred to on the left side of the operator is checked through is-a. The following is an example:

 Public class Test {   publicstaticvoid  main (String args[]) {      = "James";       //  following would return true since name is type of String      boolean  instanceof  String;        SYSTEM.OUT.PRINTLN (result);}   } // this will produce the following results: true

If the object being compared is an assignment that is compatible with the right type, the operator will still return to true. Here is another example:

class Vehicle {}  Public class extends Vehicle {   publicstaticvoid  main (String args[]) {      new  Car ();       Boolean result =  instanceof  Car;      SYSTEM.OUT.PRINTLN (result);}   } // this will produce the following results: true

Vii. Precedence of Java operators

Operator Precedence determines the grouping of terms in an expression. It affects how an expression evaluates. A certain operator has a higher precedence than the other operators, for example: the multiplication operator has a higher precedence than the addition operator:

For example, x=7+3*2; Here x is assigned a value of 13, not 20, because the operator * is higher priority than the operator +, so it first operations multiply 3*2, and then add 7.

Here, the operator with the highest precedence is at the top level of the table, and the lowest priority appears at the bottom. In an expression, the higher-ranking operator of precedence is evaluated first.

class operator Relevance of
Suffix > () []. (dot operator) From left to right
One dollar ++ - - ! ~ From right to left
Multiplication of * / % From left to right
Addition of + - From left to right
Shift >> >>> << From left to right
Relationship of > >= < <= From left to right
Equal == != From left to right
Bit and & From left to right
Bit XOR or ^ From left to right
Bit or | From left to right
Logic and && From left to right
Logical OR || From left to right
Conditional. ?: From right to left
Assign value = + = = *=/=%= >>= <<= &= ^== From right to left

Test Project: Https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test4

Basic Java Operators

Related Article

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.