Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What operators does JAVA have?

2025-05-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what operators does JAVA have". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what operators JAVA has.

1. Arithmetic operator 1. Unary operator

There are three arithmetic unary operations, namely -, + + and--. The table is as follows:

Examples of operator name illustration-take the reverse sign, take the reverse operation, take the value from adding one and then add one, or add the repeated value first, or subtract the value from one and then minus one, or subtract the value from one and then minus one, or subtract the value from one and then minus one, or-- a.

Code example:

Int a = 12

System.out.println (- a)

Int b = await +

System.out.println (b)

B = + + a

System.out.println (b)

two。 Binary operator

The function of arithmetic operators in Java language is to perform arithmetic operations, in addition to addition (+), subtraction (-), multiplication (*) and except (\), there are modular operations (%). The table is as follows:

The operator name illustrates the example + plus the sum of a plus b, and can also be used in the String type to perform the string concatenation operation a + b-minus the difference a-b * multiplied by the product of b a * b / divided by a divided by b's quotient a / b% take the remainder a divided by b

Note: arithmetic operators are binocular operators, that is, operators that join two operands. In priority, *, /,% have the same level of operation, and are higher than +,-(+,-have the same level).

Attention should be paid to the following two points in arithmetic operations:

The remainder (%) operation requires that the two operands involved in the operation are integers and cannot be of other types.

Two integers are divided, and the result is still an integer. If an integer is divided by a real number, the result is a real number.

Code example:

Public static void main (String [] args) {

Float F1 = 9% 4bot / result of floating-point type after saving remainder

Double da = 9 + 4.5; / / A double precision addition

Double db = 9-3.0; / / A double precision subtraction

Double dc = 9 * 2.5; / / A double precision multiplication

Double dd = 9 / 3.0; / / A double precision division

Double de = 9% 4; / / A double precision margin

System.out.println ("arithmetic operation of integers"); / / addition, subtraction, multiplication, division and remainder of integers

System.out.printf ("94th% d\ n", 94th)

System.out.printf ("9-4% d\ n", 9-4)

System.out.printf ("94th% d\ n", 94th)

System.out.printf ("9max 4% d\ n", 9 max 4)

System.out.printf ("9%% 4% d\ n", 9% 4)

System.out.println ("\ narithmetic operation of floating point numbers"); / / addition, subtraction, multiplication, division and remainder of floating point numbers

System.out.printf ("9+4.5f=%f\ n", 94.5f)

System.out.printf ("9-3.0f=%f\ n", 9-3.0f)

System.out.printf ("9*2.5f=%f\ n", 92.5f)

System.out.printf ("9/3.0f=%f\ n", 9tick 3.0f)

System.out.printf ("9%% 4% f\ n", F1)

System.out.println ("arithmetic operation of double precision numbers"); / / addition, subtraction, multiplication, division and remainder of a double precision number

System.out.printf ("94.5% 4.16f\ n", da)

System.out.printf ("9-3.0% 4.16f\ n", db)

System.out.printf ("92.5% 4.16f\ n", dc)

System.out.printf ("9thumb 3.0% 4.16f\ n", dd)

System.out.printf ("9% 4% 4% 4.16f\ n", de)

System.out.println ("arithmetic operation of\ ncharacters"); / / addition and subtraction of a pair of characters

System.out.printf ("'Aids 32% d\ n",' Aids 32)

System.out.printf ("'Aids 32% c\ n",' Aids 32)

System.out.printf ("'a'-'B'=%d\ n",' averse Mushroom B')

}

The results are as follows:

Arithmetic operation of integers 9 "4" 139-4 "59" 4 "369Universe 4" 29% 4 "1 arithmetic operation of floating-point numbers 9+4.5f=13.5000009-3.0f=6.0000009*2.5f=22.5000009/3.0f=3.0000009%4=1.000000 arithmetic operation of double precision numbers 9" 4.5 "13.500000000000009-3.0" 6.0000000000009" 2.5" 22.50000000000009Universe 3.0" 3.000000000000009% 4" 1.00000000000000 character arithmetic operation 'A'+32=97'A'+32=a'a'-'B'=31

The operation rules are summarized as follows:

Integer type results are easy to understand, floating-point and double-precision results are returned with decimals, and character types will convert characters to ASCII codes for re-operation.

The result of the operation between integers retains only the integer part, 6 decimal places in floating-point operations, and 16 decimal places in double-precision operations.

Multiple arithmetic operators in an expression have the same priority, such as "a-b+c", and the order is determined by the direction in which the operators are combined. The combination direction of arithmetic operators is "from left to right", that is, from left to right. Therefore, b is first combined with the minus sign to perform the operation of "a murb" and then the operation of adding c.

Arithmetic assignment operator

The arithmetic assignment operator is just an abbreviation and is generally used to change the variable itself. The table is as follows:

The operator means that the instance result + = adds the numeric value on the left side of the operator to the numeric value on the right, and the result is assigned to the variable itself on the left int axiom 5; the result is assigned to the numeric value on the left side of the operator minus the numeric value on the right, and the result is assigned to the variable itself on the left side, int axiom 5; the result is assigned to the variable on the left by multiplying the numeric value on the left by the numeric value on the right, and the result is assigned to the variable itself on the left. The value on the left side of the operator is divided by the value on the right, and the result is assigned to the variable itself on the left, int aplom2% = the value on the left side of the operator is divided by the number on the right, and the result is assigned to the variable itself on the left, int aq5; a% "2transfera1.

Code example:

Int a = 1ntint b = 2

A + = b; / equivalent to a = a + b

System.out.println (a)

A + = b + 3; / equivalent to a = a + b + 3

System.out.println (a)

A-= b; / equivalent to a = a-b

System.out.println (a)

A * = b; / / equivalent to a=a*b

System.out.println (a)

A / = b; / / equivalent to a=a/b

System.out.println (a)

A% = b; / / equivalent to a=a%b

System.out.println (a)

Note: although Java supports writing that assigns values to more than one variable at a time, this reduces the readability of the program, so writing is not recommended.

Logical operators

Logic operators connect the relational expressions of various operations to form a complex logical expression to determine whether the expression in the program is valid or not, and the result is true or false. The table is as follows:

The meaning of operator usage shows that when the result of the example & & a short circuit and ab are all true, the calculated result is true, otherwise it is false. 2 > 1 evaluate 34) true | a | b if the logic or ab is all false, the result is false, otherwise it is true. 1 > 2 | 3 > if both 5false&a&b logic and ab are true, the result is true, otherwise it is false. 13false > = operators greater than or equal to support only left and right operands to be numeric types. Returns true if the value of the previous variable is greater than or equal to the value of the latter variable. 4 > = 2true = 1

C

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report