Getting Started with Java---operators & arithmetic operators & Auto-decrement operators & relational operators & bitwise operators

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

One of the most basic uses of computers is to perform mathematical operations, and as a computer language, Java also provides a rich set of operators to manipulate variables. We can divide the operators into the following groups:

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

This article only looks at the arithmetic operators & self-increment decrement operators & relational operators & bitwise operators, which are four, followed by records in subsequent articles. First look at the arithmetic operator. Arithmetic operators are used in mathematical expressions, and they function in the same way as they do in mathematics. The following table lists all the arithmetic operators. The instance in the table assumes that the value of integer variable A is 10, and the value of variable B is 20:

operator Description Example
+ Values on both sides of the addition-add operator A + B equals 30
- Subtraction-left operand minus right operand A–b equals-10
* Multiply-multiplies the values on both sides of the operator A * b equals 200
/ Division-left operand divided by right operand b/a equals 2.
Modulo-left operand divided by the remainder of the right operand B%a equals 0.
++ Self-increment: The value of the operand is increased by 1 b++ or ++b equals 21 (see below for differences)
-- Self-Subtract: The value of the operand is reduced by 1 b--or--b equals 19 (see below for differences)

The following simple example program demonstrates the arithmetic operator. Copy and paste the following Java program and save it as a Test.java file, and then compile and run the program:

public class Test {

public static void Main (string[] args) {
int a = 10;
int B = 20;
int C = 25;
int d = 25;
System.out.println ("a + b =" + (A + b));
System.out.println ("A-B =" + (a));
SYSTEM.OUT.PRINTLN ("A * b =" + (A * b));
System.out.println ("b/a =" + (b/a));
System.out.println ("b% A =" + (b% a));
System.out.println ("c% A =" + (c% a));
System.out.println ("a++ =" + (a++));
System.out.println ("a--=" + (a--));
See the difference between d++ and ++d
System.out.println ("d++ =" + (d++));
System.out.println ("++d =" + (++d));
}
}

The result of the operation is:

A+B= 30A-B= -10A*B= 200B/A= 2b % a =  0c % a = 5a++  = 10a -- = 11++ = 25 ++d =                   

Then we look at the self-increment decrement operator. 1. The self- increment (+ +) decrement (-) operator is a special arithmetic operator that requires two operands in an arithmetic operator, while the self-increment decrement operator is an operand.

public class selfaddminus{
public static void Main (string[] args) {
int a = 3;//defines a variable;
int B = ++a;//self-increment operation
int c = 3;
int d =--c;//self-subtraction operation
System.out.println ("The value after the self-increment operation equals" +b);
System.out.println ("The value after the self-subtraction operation equals" +d);
}
}


The result of the operation is:

The value after the self-increment operation equals 4 the value after the self-subtraction operation equals 2  

Look at the parsing:

    • int b = ++a; The splitting operation process is: a=a+1=4; B=a=4, the final result is b=4,a=4

    • int d =--c; The splitting operation process is: c=c-1=2; d=c=2, the final result is d=2,c=2

2, prefix self-increment (++a,--a): the first self-increment or self-subtraction operation, and then the expression operation.

3, suffix self-subtraction (a++,a--): first expression operation, and then self-increment or self-reduction operation example:


public class selfaddminus{
public static void Main (string[] args) {
int a = 5;//defines a variable;
int b = 5;
int x = 2*++a;
int y = 2*b++;
System.out.println ("Increment operator prefix operation a=" +a+ ", x=" +x ");
System.out.println ("b=" +b+ after "increment operator suffix operation", y= "+y");
}
}

The result of the operation is:

A = 6 after the increment operator prefix operation ,andx= 6 after the increment operator suffix operation,y=ten 

The following table is a Java-supported relational operator, the value of the instance integer variable A in the table is 10, and the value of variable B is 20:

operator Description Example
== Checks if the values of the two operands are equal, and if they are equal, the condition is true. (A = = B) is False (not true).
!= Checks if the values of the two operands are equal, and if the values are not equal, the condition is true. (A! = B) is true.
> Checks if the value of the left operand is greater than the value of the right operand, and if so the condition is true. (a> B) not true.
< Checks if the value of the left operand is less than the value of the right operand, and if so the condition is true. (A <b) is true.
>= Checks if the value of the left operand is greater than or equal to the value of the right operand, and if so the condition is true. (a> = B) is false.
<= Checks if the value of the left operand is less than or equal to the value of the right operand, and if so the condition is true. (a <= B) is true.

The following simple example program demonstrates the relational operator. Copy and paste the following Java program and save it as a Test.java file, and then compile and run the program:

public class Test {

public static void Main (string[] args) {
int a = 10;
int B = 20;
System.out.println ("a = = b =" + (a = = b));
SYSTEM.OUT.PRINTLN ("A! = b =" + (A! = b));
System.out.println ("a > b =" + (A > B));
System.out.println ("a < b =" + (A < b));
System.out.println ("b >= A =" + (b >= a));
System.out.println ("b <= A =" + (b <= a));
}
}

The result of the operation is:

a == b =falsea != b =  truea > b = falsea < b Span class= "pun" >= trueb >=  a = trueb <= a = false    

Java defines a bitwise operator, which is applied to types such as Integer type (int), long Integer (length), short integer (shorter), character type (char), and byte type (byte). The bitwise operator acts on all the bits, and the bitwise operation. Suppose a = 60,b = 13; Their binary format representation would be as follows:

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

The following table lists the basic operations of the bitwise operators, assuming that the value of integer variable A is 60 and the value of variable B is 13:

if the relative position is 1, the result is 1, otherwise 0 The
operator description example
& (A&B), 12, which is 0000 1100
| if the relative position is 0, the result is 0, otherwise 1 (A | B) get 61, i.e. 0011 1101
^ If the relative value is the same, then the result is 0, otherwise 1 (A ^ B) gets 49, which is 0011 0001
? bitwise complement operator flips each bit of the operand, that is, 0 becomes 1, and 1 becomes 0. (? A) Get-61, which is 1100 0011
<<  bitwise left-shift operator. The left operand specifies the number of digits of the right-hand operand by bitwise left. A << 2 gets 240, which is 1111 0000
>>  bitwise right-shift operator. The left operand shifts the number of digits specified by the right-hand operand to the right. A >> 2 Get 15 that is 1111
>>>  Right-shift 0 operator. The value of the left operand is shifted right by the number of digits specified by the right operand, and the resulting empty space is filled with 0. A>>>2 get 15 that is 0000 1111

The following simple example program demonstrates the bitwise operator. Copy and paste the following Java program and save it as a Test.java file, and then compile and run the program:

public class Test {
public static void Main (string[] args) {
int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = A & B; /* 12 = 0000 1100 */
System.out.println ("A & B =" + c);

c = A |       b /* 61 = 0011 1101 */
System.out.println ("a | b = "+ C");

c = a ^ b; /* 49 = 0011 0001 */
System.out.println ("a ^ b =" + c);

c = ~a; /*-61 = 1100 0011 */
System.out.println ("~a =" + c);

c = a << 2; /* 240 = 1111 0000 */
System.out.println ("a << 2 =" + C);

c = a >> 2; /* 15 = 1111 */
System.out.println ("a >> 2 =" + C);

c = a >>> 2; /* 15 = 0000 1111 */
System.out.println ("a >>> 2 =" + C);
}
}

The result of the operation is:

A&B= 12A|B= 61a ^ b =49~a =  -61a << 2 = 240< Span class= "PLN" >a >> 2= 15a >>>< Span class= "PLN" > 2 = 15   
Well, it's here this time. If you feel good, please more praise support oh ...
Original link: 80109011

Getting Started with Java---operators & arithmetic operators & Auto-decrement operators & relational operators & bitwise operators

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.