Java operators, arithmetic operators

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Introduced

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.
Residual-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)

Instance
 Public classArithmeticoperator { Public Static voidMain (string[] args) {intA = 10; intb = 20; intc = 25; intD = 25; System.out.println ("A + b =" + (A + b));//addition OperationSystem.out.println ("A-B =" + (a));//Subtraction OperationsSYSTEM.OUT.PRINTLN ("A * b =" + (A * b));//Multiplication OperationsSystem.out.println ("b/a =" + (b/a));//Division OperationSystem.out.println ("b% A =" + (b% a));//take the remainder operationSystem.out.println ("c% A =" + (c% a));//System.out.println ("a++ =" + (a++));//self-increment, first output + + +System.out.println ("a--=" + (a--));//self-subtraction, first output A and then a--。 Since the previous step a outputs since the increment, so at this time a=11,System.out.println ("C + + =" + (c + +));//first output, then self-incrementSystem.out.println ("++d =" + (++d));//first self-increment, then output    }}

Operation Result:

A + b = ten-B = -10* b =/a =2 A = 0% A = 5a+ +   =a
    --   = onec+ +   = 25++d   = 26

Attention:

1. BYTE/SHORT/CHAR automatically promoted to int when calculating

2. The integer type remains unchanged at the time of operation

3. Small type and large type operation, the result must be large type (for example: Byte is a small type, int is a large type)

4. Arbitrary integer/0-arithmeticexception-arithmetic anomalies

Any non-0 digit/0.0 any non 0 decimal/0 = Infinity-Infinity

0/0.0

0.0/0.0 =NaN = not a number + = non-numeric

0.0/0

Java operators, arithmetic 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.