In this post, we will learn about available operators in java and their example usages. We will also try to understand when to use them and what to expect in result.
Table of Contents What Is an Operator? Assignment Operator (=) Arithmetic Operators Unary Arithmetic Operators Binary Arithmetic Operators String Concatenation Operator (+) Relational Operators Boolean Logical Operators Bitwise Operators Ternary Operator (? :)
What Is an Operator?
An operator is a symbol that performs a specific kind of operation on one, two, or three operands, and produces a result. The type of the operator and its operands determines the kind of operation performed on the operands and the type of the result produced.
Operators in Java can be categorized based on two criteria:
- The number of operands they operate on : There are three types of operators based on the number of operands. An operator is called a unary, binary, or ternary operator based on the number of operands. If an operator takes one operand, it called a unary operator; if it takes two operands, it called a binary operator; if it takes three operands, it called a ternary operator.
- The type of operation they perform on the operands : An operator is called an arithmetic operator, a relational operator, a logical operator, or a bitwise operator, depending on the kind of operation it performs on its operands.
Assignment Operator (=)
An assignment operator (=) is used to assign a value to a variable. It is a binary operator. It takes two operands. The value of the right-hand operand is assigned to the left-hand operand. The left-hand operand must be a variable. For example,
//26 is the right-hand operand. //num is the left-hand operand, which is a variable of type int. int num = 26;
Java ensures that the value of the right-hand operand of the assignment operator is assignment compatible to the data type of the left-hand operand. Otherwise, a compile-time error occurs. In case of reference variables, you may be able to compile the source code and get a runtime error if the object represented by the right-hand operand is not assignment compatible to the reference variable as the left-hand operand.
Operators like (+, -, *, /) are called arithmetic operators in Java and can only be used with numeric type operands. It means, both operands to arithmetic operators must be one of types byte, short, char, int, long, float, and double. These operators cannot have operands of boolean primitive type and reference type.
A) Binary Arithmetic Operators
|+||Addition – Adds values on either side of the operator|
|–||Subtraction – Subtracts right hand operand from left hand operand|
|*||Multiplication – Multiplies values on either side of the operator|
|/||Division – Divides left hand operand by right hand operand|
|%||Modulus – Divides left hand operand by right hand operand and returns remainder|
B) Unary Arithmetic Operators
|+||Unary plus operator; indicates positive value (numbers are positive without this, however)|
|–||Unary minus operator; negates an expression value|
|++||Increment operator; increments a value by 1|
|—||Decrement operator; decrements a value by 1|
|!||Logical complement operator; inverts the value of a boolean|
String Concatenation Operator (+)
The + operator is overloaded. An operator is said to be overloaded if it is used to perform more than one function. So far, you have seen its use as an arithmetic addition operator to add two numbers. It can also be used to concatenate two strings. Two strings, such as “abc” and “xyz”, can be concatenated using the + operator as “abc” + “xyz” to produce new string “abcxyz”.
String str1 = "Hello"; String str2 = " World"; String str3 = str1 + str2; // Assigns "Hello World" to str3
The string concatenation operator is also used to concatenate a primitive and a reference data type value to a string.
Consider the following snippet of code:
int num = 26; String str1 = "Alphabets"; String str2 = num + str1; // Assigns "26Alphabets" to str2
If a String variable contains the null reference, the concatenation operator uses a string “null”.
All relational operators are binary operators. That is, they take two operands. The result produced by a relational operator is always a Boolean value true or false.
Let’s see below all available relational operators in java.
|==||Checks if the values of two operands are equal or not, if yes then condition becomes true.|
|!=||Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.|
|>||Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.|
|<||Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.|
|>=||Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.|
|<=||Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.|
Boolean Logical Operators
All Boolean logical operators can be used only with boolean operand(s). See below table for clear picture.
|!||returns true if the operand is false, and false if the operand is true.|
|&&||returns true if both operands are true. If either operand is false, it returns false.|
|&||returns true if both operands are true. If either operand is false, it returns false.|
|||||returns true if either operand is true. If both operands are false, it returns false.|
||||returns true if either operand is true. If both operands are false, it returns false.|
|^||it returns true if one of the operands is true, but not both. If both operands are the same, it returns false.|
|&=||if both operands evaluate to true, &= returns true. Otherwise, it returns false.|
||=||if either operand evaluates to true, != returns true. Otherwise, it returns false.|
|^=||if both operands evaluate to different values, that is, one of the operands is true but not both, ^= returns true. Otherwise, it returns false.|
Please note that :
1) The logical AND operator (&) works the same way as the logical short-circuit AND operator (&&), except for one difference. The logical AND operator (&) evaluates its right-hand operand even if its left-hand operand evaluates to false.
2) The logical OR operator works the same way as the logical short-circuit OR operator, except for one difference. The logical OR operator evaluates its right-hand operand even if its left-hand operand evaluates to true.
A bitwise operator manipulates individual bits of its operands. Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
|&||Binary AND Operator copies a bit to the result if it exists in both operands.|
||||Binary OR Operator copies a bit if it exists in either operand.|
|^||Binary XOR Operator copies the bit if it is set in one operand but not both.|
|~||Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits.|
|<<||Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.|
|>>||Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.|
|>>>||Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.|
Ternary Operator (? :)
Java has one conditional operator. It is called a ternary operator as it takes three operands. It is used in the form
boolean-expression ? true-expression : false-expression
The two symbols of “?” and “:” make the ternary operator. If the boolean-expression evaluates to true, it evaluates the true-expression; otherwise, it evaluates false-expression.
That’s all for the operators in java. Drop me your questions and comments in comment section.
Happy Learning !!