Operators in JavaOperator 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:
Java Operator Precedence
Java Unary OperatorThe Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:
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 OperatorsJava arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations. Java Arithmetic Operator ExampleOutput: 15 5 50 2 0 Java Arithmetic Operator Example: ExpressionOutput: 21 Java Left Shift OperatorThe Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times. Java Left Shift Operator ExampleOutput: 40 80 80 240 Java Right Shift OperatorThe Java right shift operator >> is used to move the value of the left operand to right by the number of bits specified by the right operand. Java Right Shift Operator ExampleOutput: 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 the second condition if the first condition is false. It checks the second condition only if the 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 the second condition if the first condition is true. It checks the second condition only if the 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 OperatorJava Ternary operator is used as one line 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 ExampleOutput: 2 Another Example: Output: 5 Java Assignment OperatorJava assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left. Java Assignment Operator ExampleOutput: 14 16 Java Assignment Operator ExampleOutput: 13 9 18 9 Java Assignment Operator Example: Adding shortOutput: Compile time error After type cast: Output: 20 You may also like |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263855.html