Java Operators - The Coding Shala
Home >> Learn Java >> Java Operators
Java Operators
In Java, operators are used for the evaluation of expressions. There are many types of operators available in Java-like arithmetic, bitwise, relational, logical, assignment operators, etc.
Arithmetic Operators
To calculate mathematical expression we use arithmetic operators. The following image shows the Java arithmetic operators:
These operators are used on numeric operands and char types in Java. We cannot use them on boolean.
The modulus operator, % returns the remainder of a division operation. In Java, Increment (++) and Decrement (--) operators can be used as prefix and postfix. In the Prefix form, the operand is incremented or decremented before the value is obtained for the use in the expression. In the postfix form, the previous value is used in the expression, and then the operand is modified. For example:
a = 11; b = ++a; //is similar to a = a+1; b = a; //and a = 11; b=a++; //that means b=a; a=a+1;
The following Java Program explains the Arithmetic Operators:
class ArithmeticOp{ public static void main(String[] args){ int a = 10; int b = 20; double aa = 12.04; double bb = 23.97; int c = a + b; int d = a-b; System.out.println("Addition of "+a+" and "+b+" is: "+c); System.out.println("Substraction of "+a+" and "+b+" is: "+ d); System.out.println("Multiplication of "+a+" and "+b+" is: "+a*b); System.out.println("Divison of "+b+" and "+a+" is: "+b/a); System.out.println("Modulus of "+a+" and "+b+" is: "+a%b); System.out.println("Modulus of "+aa+" and "+a+" is: "+aa%a); a = a + 1; System.out.println("Value of a is "+ a); a += 2; System.out.println("Value of a is "+ a); b = ++a; System.out.println("Value of a and b is "+ a+ " " + b); b = a++; System.out.println("Value of a and b is "+ a+ " " + b); } } <<<<<The Output of this Program is >>>>>>>>> Addition of 10 and 20 is: 30 Substraction of 10 and 20 is: -10 Multiplication of 10 and 20 is: 200 Divison of 20 and 10 is: 2 Modulus of 10 and 20 is: 10 Modulus of 12.04 and 10 is: 2.039999999999999 Value of a is 11 Value of a is 13 Value of a and b is 14 14 Value of a and b is 15 14
Note: Here, '=' is an assignment operator.
Java Bitwise Operators
These bitwise operators work on the individual bits of their operands and can be applied to the integer types, int, long, short, byte, and char. The Java bitwise operators are shown below:
Note: All these operators can be used with assignment operators.
The following image shows the outcome of these bitwise logical operators:
The left shift operator(<<) shifts all of the bits of the value to the left by a specified number of times. Every time we shift to left, the high order bit(left bit) is shifted out(lost) and a zero(0) is added to the right.
The Right shift(>>), shifts all of the bits of the value to the right by a specified number of times. for every time we shift a bit to right, the top bit(leftmost) is filled in with the previous contents of the top bit. This is called sign extension and the rightmost bit is lost.
Note: The unsigned shift right operator (>>>) always shifts zeros into the high-order bit.
Java Relational Operators
The relational operators are used to determine the relationship between the operands. The relational operators are shown below:
The outcome of these operations is a boolean value. These operators are often used with if and loop statements.
Java Boolean Logical Operators
These boolean logical operators are used with only boolean operands. The boolean logical operators are shown below:
These operators work on boolean values in the same way that they work on the bits of an integer.
Other Posts You May Like
Comments
Post a Comment