2.2 Data Type 2.2.1 Java data type 2.3 common operators
Java provides a set of operator-rich manipulation variables. We can put all the Java operators into the following groups:
Arithmetic operators
Relational operators
Bitwise operators
logical operators
Assignment operators
Other operators
Arithmetic operators:
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:
Assuming the integer variable a=10 and the variable b=20, then:
Arithmetic Operation Example
operator |
Description |
Example |
+ |
Addition-adds values on either side of the operator |
A + B = 30 |
- |
Subtraction-subtracts right hand operand from left hand operand |
A-B =-10 |
* |
Multiplication-multiplies values on either side of the operator |
A * B = 200 |
/ |
Division-divides left hand operand by right hand operand |
b/a = 2 |
% |
Modulus-divides left hand operand by right hand operand and returns remainder |
B% A = 0 |
++ |
Increment-increases the value of operand by 1 |
b++ =21 |
-- |
Decrement-decreases the value of operand by 1 |
b--=19 |
Relational operators:
The following relational operators are supported by the Java language
Assuming variable a=10 and variable b=20, then:
Relational operator Instances
operator |
Description |
Example |
== |
Checks if the values of the operands is equal or not, if yes and condition becomes true. |
(A = = B) is not true. |
!= |
Checks if the values of operands is equal or not, if values is not equal then condition becomes true. |
(A! = B) is true. |
> |
Checks if the value of the left operand was greater than the value of right operand, if yes then condition becomes true. |
(A > B) is not true. |
< |
Checks if the value of operand is less than the value of right operand, if yes then condition becomes true. |
(A < B) is true. |
>= |
Checks if the value of operand is greater than or equal to the value of right operand, if yes then condition becomes True. |
(A >= B) is not true. |
<= |
Checks if the value of operand is less than or equal to the value of right operand, if yes then condition becomes TRU E. |
(A <= B) is true. |
Bitwise operators:
Java defines several bitwise operators that can be applied to integer types, long, integer, short, character, and Byte.
Bitwise operators Act on BITS and perform bitwise operations. Assume when a = 60 and b= 13; Now in binary format, they will be as follows:
A = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
A^b = 0011 0001
~a = 1100 0011
The following table lists the bitwise operators:
Assuming the integer variable a=60 and the variable b=13, then:
Bit Operation example
operator |
Description |
Example |
& |
Binary and Operator copies a bit to the result if it is exists in both operands. |
(A & B) would give which is 0000 1100 |
| |
Binary OR Operator Copies a bit if it exists in either operand. |
(A | B) would give which is 0011 1101 |
^ |
Binary XOR Operator Copies the bit if it is a set in one operand and not both. |
(A ^ B) would give which is 0011 0001 |
~ |
Binary Ones complement Operator is unary and has the effect of ' flipping ' bits. |
(~a) would give-61 which is 1100 0011 in 2 ' s complement form due to A signed binary number. |
<< |
Binary left Shift Operator. The left operands value was moved left by the number of bits specified by the right operand. |
A << 2 would give which is 1111 0000 |
>> |
Binary right Shift Operator. The left operands value was moved right by the number of bits specified by the right operand. |
A >> 2 would give which is 1111 |
>>> |
Shift Right Zero fill operator. The left operands value was moved right by the number of bits specified by the right operand and shifted values are filled Up with zeros. |
A >>>2 would give which is 0000 1111 |
Logical operators:
The following table lists the logical operators:
Assuming the Boolean variable a=ture, the variable b=false, then:
Logical operator Instances
operator |
Description |
Example |
&& |
Called Logical and operator. If Both the operands is Non-zero, then the condition becomes true. |
(A && B) is false. |
|| |
Called Logical OR Operator. If any of the operands is Non-zero, then the condition becomes true. |
(A | | B) is true. |
! |
Called Logical not Operator. Use to reverses the logical state of its operand. If a condition is true then Logical not operator would make false. |
! (A && B) is true. |
Assignment operators:
The following assignment operators are supported by the Java language:
Assignment operator Instance
operator |
Description |
Example |
= |
Simple assignment operator, assigns values from right side operands to left side operand |
c = A + B would assign value of a + B into C |
+= |
Add and assignment operator, It adds right operand to the left operand and assign the result to left operand |
c + = A is equivalent to C = C + A |
-= |
Subtract and assignment operator, It subtracts right operand from the left operand and assign the result to left operand |
C-= A is equivalent to C = C-a |
*= |
Multiply and assignment operator, It multiplies right operand with the left operand and assign the result to left operand |
C *= A is equivalent to C = c * A |
/= |
Divide and assignment operator, It divides left operand with the right operand and assign the result to left operand |
C/= A is equivalent to C = C/A |
%= |
Modulus and assignment operator, It takes modulus using, operands and assign the result to left operand |
C%= A is equivalent to C = c% A |
<<= |
Left Shift and assignment operator |
C <<= 2 is same as C = C << 2 |
>>= |
Right Shift and assignment operator |
C >>= 2 is same as C = C >> 2 |
&= |
Bitwise and assignment operator |
C &= 2 is same as C = C & 2 |
^= |
Bitwise exclusive OR and assignment operator |
C ^= 2 is same as C = c ^ 2 |
|= |
Bitwise inclusive OR and assignment operator |
C |= 2 is same as C = C | 2 |
Other operators
The Java language supports some other operators.
The conditional operator (?:):
The conditional operator is also known as the ternary operator. The operator consists of three operands for evaluating the calculation of a Boolean expression. The goal of this operator is to determine which values should be assigned to the variable. Can be written as:
Variable x = (expression)? Value if True:value if False
Here is an example:
public class Test {public static void Main (String args[]) { int A, b; A = ten; b = (A = = 1)? 20:30; System.out.println ("Value of B is:" + B); b = (A = = 10)? 20:30; System.out.println ("Value of B is:" + b);} }
This will produce the following results:
Value of B is:30value of B is:20
Instanceof Operator:
This operator is used only for object reference variables. The action checks whether the object is of a specific type (class type or interface type). The instanceof operator is written as:
(Object reference variable) instanceof (class/interface type)
If the object of the variable mentioned on the left side of the operator passes the class/interface type that is-a checks to the right, the result will be true. Here is an example:
public class Test {public static void main (string args[]) { string name = "James"; Following would return true since name is type of string boolean result = Name instanceof string; SYSTEM.OUT.PRINTLN (result);} }
This will produce the following results:
True
This operator will still return true if the object being compared is compatible with the type assigned on the right. Here is an example:
Class Vehicle {}public class Car extends Vehicle {public static void Main (String args[]) { Vehicle a = new Car (); C8/>boolean result = a instanceof Car; SYSTEM.OUT.PRINTLN (result);} }
This will produce the following results:
True
Priority Java operators:
The criteria that are determined by the operator precedence are grouped in an expression. This affects how an expression is evaluated. Some operators have higher precedence than others, for example, the multiplication operator takes precedence over the addition operation:
For example x= 7+3* 2, where x is assigned a value of 13 instead of 20 because the operator * has precedence over +, so it is first multiplied by 3 * 2 and then added 7.
Here, the operation with the highest priority appears above the table, and those with the lowest appearance at the bottom. In an expression, a higher-priority operator evaluates the calculation first.
category |
operator |
Associate |
Postfix |
() [] . (dot operator) |
Left to right |
Unary |
++ - - ! ~ |
Right-left |
Multiplicative |
* / % |
Left to right |
Additive |
+ - |
Left to right |
Shift |
>> >>> << |
Left to right |
Relational |
> >= < <= |
Left to right |
Equality |
== != |
Left to right |
Bitwise and |
& |
Left to right |
Bitwise XOR |
^ |
Left to right |
Bitwise OR |
| |
Left to right |
Logical and |
&& |
Left to right |
Logical OR |
|| |
Left to right |
Conditional |
?: |
Right-left |
Assignment |
= + = = *=/=%= >>= <<= &= ^= |= |
Right-left |
Comma |
, |
Left to right |
Java Private Learning Notes--2nd. Data types and operators