• 淘宝、京东、拼多多购物助手上线,希望大家多多支持!

  • 增加付费可看,结合购物助手

  • 马上要过年了

  • 下载资源可以使用了! :mrgreen:

  • 今天天气一般般!

Operators in java

JAVA 空手师 4年前 (2019-11-15) 414次浏览 已收录 0个评论

Operators in java

Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in java which are given below:

  • Unary Operator,
  • Arithmetic Operator,
  • Shift Operator,
  • Relational Operator,
  • Bitwise Operator,
  • Logical Operator,
  • Ternary Operator and
  • Assignment Operator.

Java Operator Precedence

Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative * / %
additive + -
Shift shift > >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ? :
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

Java Unary Operator

The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:

  • incrementing/decrementing a value by one
  • negating an expression
  • inverting the value of a boolean

Java Unary Operator Example: ++ and —

Output:

10
12
12
10

Java Unary Operator Example 2: ++ and —

Output:

22
21

Java Unary Operator Example: ~ and !

Output:

-11
9
false
true

Java Arithmetic Operators

Java arithmatic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.

Java Arithmetic Operator Example

Output:

15
5
50
2

Java Arithmetic Operator Example: Expression

Output:

21

Java Left Shift Operator

The Java left shift operator

Java Left Shift Operator Example

Output:

40
80
80
240

Java Right Shift Operator

The Java right shift operator >> is used to move left operands value to right by the number of bits specified by the right operand.

Java Right Shift Operator Example

Output:

2
5
2

Java Shift Operator Example: >> vs >>>

Output:

5
5
-5
1073741819

Java AND Operator Example: Logical && and Bitwise &

The logical && operator doesn’t check second condition if first condition is false. It checks second condition only if first one is true.

The bitwise & operator always checks both conditions whether first condition is true or false.

Output:

false
false

Java AND Operator Example: Logical && vs Bitwise &

Output:

false
10
false
11

Java OR Operator Example: Logical || and Bitwise |

The logical || operator doesn’t check second condition if first condition is true. It checks second condition only if first one is false.

The bitwise | operator always checks both conditions whether first condition is true or false.

Output:

true
true
true
10
true
11

Java Ternary Operator

Java Ternary operator is used as one liner replacement for if-then-else statement and used a lot in java programming. it is the only conditional operator which takes three operands.

Java Ternary Operator Example

Output:

2

Another Example:

Output:

5

Java Assignment Operator

Java assignment operator is one of the most common operator. It is used to
assign the value on its right to the operand on its left.

Java Assignment Operator Example

Output:

14
16

Java Assignment Operator Example

Output:

13
9
18
9

Java Assignment Operator Example: Adding short

Output:

Compile time error

After type cast:

Output:

20

You may also like

Operator Shifting in Java


空手之道 , 版权所有丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Operators in java丨部分内容为转载,如有侵权,请联系,删除处理!丨
喜欢 (0)

您必须 登录 才能发表评论!