before (prefix) its argument or after (postfix) its argument. A binary or ternary operator appears between its arguments.
Operators in java fall into 8 different categories:
Java operators fall into eight different categories: assignment, arithmetic, relational, logical, bitwise,
compound assignment, conditional, and type.
Relational Operators
|
Java has eight different operator types: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional, and type.
Assignment operators
The java assignment operator statement has the following syntax:
<variable> = <expression>
If the value already exists in the variable it is overwritten by the assignment operator (=).
public class AssignmentOperatorsDemo { |
Relational operators
Relational operators in Java are used to compare 2 or more objects. Java provides six relational operators:
greater than (>), less than (<), greater than or equal (>=), less than or equal (<=), equal (==), and not equal (!=).
All relational operators are binary operators, and their operands are numeric expressions.
Binary numeric promotion is applied to the operands of these operators. The evaluation results in a boolean value. Relational operators have precedence lower than arithmetic operators, but higher than that of the assignment operators. An example program is shown below that demonstrates the different relational operators in java.
|
Logical operators
Logical operators return a true or false value based on the state of the Variables. There are six logical, or boolean, operators. They are AND, conditional AND, OR, conditional OR, exclusive OR, and NOT. Each argument to a logical operator must be a boolean data type, and the result is always a boolean data type. An example program is shown below that demonstrates the different Logical operators in java.
public class LogicalOperatorsDemo { |
Given that x and y represent boolean expressions, the boolean logical operators are defined in the Table below.
x | y | !x | x & y x && y | x | y x || y | x ^ y |
true | true | false | true | true | false |
true | false | false | false | true | true |
false | true | true | false | true | true |
false | false | true | false | false | false |
Bitwise operators
Java provides Bit wise operators to manipulate the contents of variables at the bit level.
These variables must be of numeric data type ( char, short, int, or long). Java provides seven bitwise
operators. They are AND, OR, Exclusive-OR, Complement, Left-shift, Signed Right-shift, and Unsigned Right-shift. An example program is shown below that demonstrates the different Bit wise operators in java.
public class BitwiseOperatorsDemo { |
The result of applying bitwise operators between two corresponding bits in the operands is shown in the Table below.
A | B | ~A | A & B | A | B | A ^ B |
1 | 1 | 0 | 1 | 1 | 0 |
1 | 0 | 0 | 0 | 1 | 1 |
0 | 1 | 1 | 0 | 1 | 1 |
0 | 0 | 1 | 0 | 0 | 0 |
Output
3,0,3
/* |
Compound operators
The compound operators perform shortcuts in common programming operations. Java has eleven compound assignment operators.
Syntax:
argument1 operator = argument2.
The above statement is the same as, argument1 = argument1 operator argument2. An example program is shown below that demonstrates the different Compound operators in java.
public class CompoundOperatorsDemo { |
Conditional operators
The Conditional operator is the only ternary (operator takes three arguments) operator in Java. The operator evaluates the first argument and, if true, evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left: (a?b?c?d:e:f:g) evaluates as (a?(b?(c?d:e):f):g)
An example program is shown below that demonstrates the Ternary operator in java.
public class TernaryOperatorsDemo { |
/* |
Output
FFT
Type conversion allows a value to be changed from one primitive data type to another. Conversion can occur explicitly, as specified in
the program, or implicitly, by Java itself. Java allows both type widening and type narrowing conversions.
In java Conversions can occur by the following ways:
- Using a cast operator (explicit promotion)
- Using an arithmetic operator is used with arguments of different data types (arithmetic promotion)
- A value of one type is assigned to a variable of a different type (assignment promotion)
Operator Precedence
The order in which operators are applied is known as precedence. Operators with a higher precedence are applied before operators with a lower precedence. The operator precedence order of Java is shown below. Operators at the top of the table are applied before operators lower down in the table. If two operators have the same precedence, they are applied in the order they appear in a statement.
That is, from left to right. You can use parentheses to override the default precedence.
postfix | [] . () expr++ expr– |
unary | ++expr –expr +expr -expr ! ~ |
creation/caste | new (type)expr |
multiplicative | * / % |
additive | + - |
shift | >> >>> |
relational | < <= > >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | || |
ternary | ?: |
assignment | = “op=” |
Example
In an operation such as,
result = 4 + 5 * 3
First (5 * 3) is evaluated and the result is added to 4 giving the Final Result value as 19. Note that ‘*’ takes higher precedence than ‘+’ according to chart shown above. This kind of precedence of one operator over another applies to all the operators.