Java Basic Syntax Operators

Source: Internet
Author: User
Tags arithmetic integer division logical operators

1.1 Use of arithmetic operator + + 、--

In general, the arithmetic operator does not change the value of the variable that participates in the calculation. Instead, the new value is calculated when the value of the original variable is not changed. But some operators change the values of variables that participate in the calculation, such as + +,--。

Let's look at a piece of code:

int a = 3;int B = 3;a++;b--; System.out.println (a); System.out.println (b);

The output of the above code is a value of 4,b value of 2;

This shows that the original value of a has changed, on the basis of the original value of the original value of the 1;B has also changed, on the basis of the original value of self-minus 1;

The L + + operator will increment by 1 on the basis of the original value;

The L-operator will subtract 1 from the original value.

Let's look at another piece of code:

What happens when the ++,--operator participates in the operation, let's look at a piece of code:

int a = 3;int b;b = a++ + 10; System.out.println (a); System.out.println (b);

The output of the above code is a value of 4,b value of 13;

Summarize:

+ +,--operator, the value of variable A is increased by 1 or self-minus 1 when the original value of variable A is used to participate in the operation, after the operation is completed.

When we introduce the next ++,--operator, we take a look at the changes in the operation of operations:

int a = 3;int b;b = ++a + 10; System.out.println (a); System.out.println (b);

The output of the above code is a value of 4,b value of 14;

Here I emphasize the role of the predecessor ++,--when the ++,--operator participates in the operation of an operation:

The ++,--operator precedes it by increasing the value of variable a by 1 or by 1, and then using the updated new value to participate in the operation.

1.2 Assignment operators

Operator

Arithmetic rules

Example

Results

=

Assign value

int a=2

2

+=

Add after Assignment

int a=2,a+=2

4

-=

Reduced-value Assignment

int a=2,a-=2

0

*=

Multiply post-Assign value

int a=2,a*=2

4

/=

Assign value after integer division

int a=2,a/=2

1

%=

Assign value after modulo

int a=2,a%=2

0

First: Let's look at the following error code

public class operatordemo3{public    static void main (string[] args) {          byte x=10;          byte y=x+20;          System.out.printIn (y)    }    }               

Because the above is byte+20 the result should be of type int, but it is written directly as a byte type, without casting.

If the assignment operator is used, the result is automatically coerced into the data type, as follows:

Note: The assignment operator, such as + =, automatically turns the result to the data type on the left side of the equals sign.

To write a code, let's take a look at the use of the assignment operator

/* * Assignment operator * + =,-=, *=,/=,%=  : */  Public class OperatorDemo2 {    publicstaticvoid  main (string[] args) {         byte x = ten;         + = 20; // equivalent to x = (byte) (x+20);         System.out.println (x);    }}

The run result is 30.

1.3 Logical operators

The logical operator, which is used for Boolean operations, and the final result of the operation is a Boolean value of TRUE or false.

Operator

Arithmetic rules

Example

Results

&

And

False&true

False

|

Or

False|true

True

^

XOR or

True^flase

True

!

Non -

!true

Flase

&&

Short Circuit and

False&&true

False

||

Short Circuit or

false| | True

True

After reading the graph, let's look at the general use of logical operators:

^: The two sides are not the same as true, otherwise false

L short Circuit and &&: The data on both sides of the operation, false, the result of the operation is false;

L short-circuit or | |: The data on both sides of the operation, with true, the result of the operation is true;

L Logic not! : The data that participates in the operation, which is true, turns false, and the original is false.

The function of short circuit is one side and false, the other side does not execute. Increase efficiency

1.4 Ternary operators

L Format:

(conditional expression)? Expression 1: Expression 2;

With the code demonstration, let's learn the use of the following ternary operators:

Way One:     3>2?  The result of the //  ternary operator operation is true, the value of the expression 1 is "correct", then the result is "correct" and the output in the console prints in two:    int A = 3;     int b = 4;     = (a==b)? " equal": "Unequal";   // The result of the ternary operator operation is false, the result of the operation is "unequal" to the value of expression 2, and the result is assigned to the variable result Way Three:     int n = (3>2 && 4>6)? 100:200;     // The result of the ternary operator operation is false, the result of the operation is the value of expression 2 of 200, and the result 200 is assigned to the variable n

Let's look at the complex code:

int A = 5int b = 3int c = 1; int n2 = (a>b && b>c)? (c + +): (++C);

The execution order of this Code operation we also write:

L 1. The parentheses have a high priority, so let's first calculate the code in the first set of parentheses

L 1.1. comparison operator ' > ' precedence greater than logical operator ' && '

N Execute a>b First to get the result true;

N Executes the b>c again, obtaining the result true;

n the result of the last execution of A>b && b>c, that is true && true, the result is true

L 2. Condition in ternary operator evaluates to true, returns the result of expression 1 C + +

N First assigns the original value of the variable C to the variable N2, that is, the N2 value is 1;

n then increment the value of variable C by 1 and update to 2.

The result is n2=1;c=2;

Java Basic Syntax 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.