5.Java arithmetic operators and relational operators

Source: Internet
Author: User
Tags arithmetic operators bitwise operators

An operator is a symbol that is often used when operations are performed with variables or constants, and there are 10 commonly used methods: arithmetic operators, relational operators, logical operators, bitwise operators, shift operators, assignment operators, ternary operators, comma operators, string operators, and transformation operators. Each of these operators will be described in more detail in conjunction with the examples below.

I. Arithmetic operators

There are 7 types of arithmetic operators: "+", "-", "*", "/", "%" (remainder), "+ +" (self-added), "--" (self-subtraction).

The precision of the operator:

    • When using an operator to combine two operands, the first two operands are converted to the same type of data.
    • If one of the two operands is a double type, then the other operand must first be converted to a double type before the operation is performed.
    • If one of the two operands is a float type, then the other operand must first be converted to float, and then the operation is performed.
    • If one of the two operands is a long type, then the other operand must first be converted to long, and then the operation is performed.
    • Any of the other two basic types of data operations, two operands are automatically converted to an int type.


The following focuses on the use of the self-add and decrement operators, which can make a variable automatically add 1 and automatically minus 1, the resulting value is then assigned to the variable. The following is an example of a self-adding operation.

The self-adding operator is divided into two types:

    • One is the former self-added: First add 1 operation, and then assign value;
    • One is the post-self-add: The first assignment, in the addition of 1 operation.

public class data2{
public static void Main (string[] args) {
int a=10;
int b=10;
System.out.println ("Post-add a=" + (a++));
System.out.println ("A's value a=" +a);
SYSTEM.OUT.PRINTLN ("ex-b=" + (++b));
}
}
Operation Result:
After self-add a=10
The value of a a=11
Former self-add b=11

The above results can be seen: a first the original value output, plus 1;b first to add 1, and then the value output.

Two. Relational operators

A relational operator is a relationship between two operands that includes: ">", "<", ">=", "<=", "= =", "! =".

It is important to note that the result of an arithmetic operator is a number, whereas the result of a relational operator is a Boolean data. For example:
public class data9{
public static void Main (string[] args) {
int a=10; int b=21;
System.out.println ("Say A>b, right?") "+ (a>b));
}
}
Operation Result:
Say A>b, right? False

As you can see from the program section above, the result of the relational operator is the Boolean data.

5.Java arithmetic operators and relational 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.