Lab Core Java: Operators
Objective
In this lab, we will discuss and demonstrate the different types of operators in Java, their syntax, and how to use them with the help of examples.
Learning Objectives
By the end of this lab, learners will be able to use Operators in Java.
Introduction
The list given below informs us about the entire range of operators provided in the Java programming language.
Unary Operator.
Relational Operator.
Assignment Operator.
Java instanceof Operator.
Ternary Operator or Conditional Operators.
Arithmetic Operator.
Logical Operator.
Bitwise Operator.
Example: Assignment Operators
Java Assignment Operator Syntax: <variable> = <expression>
Operator | Example | Equivalent to |
---|---|---|
= | x=y | x=y |
+= | x+=y | x=x+y |
-= | x-=y | x=x-y |
*= | x*=y | x=x*y |
/= | x/=y | x=x/y |
%= | x%=y | x=x%y |
Java Assignment Operators are used for modifying a value, performing some mathematical calculations, and assigning values to the operands.
If the value already exists in the variable, it is overwritten by the assignment operator (=).
Create a Java class named AssignmentOperatorsDemo and write the following code in that class:
Output:
Example: Arithmetic Operators
Eight arithmetic operators are available in Java: addition, subtraction, multiplication, division, modulo (or remainder), increment (or add 1), decrement (or subtract 1), and negation. The program below demonstrates the different arithmetic operators in Java:
Operator | Name | Function | Syntax |
---|---|---|---|
+ | Addition | Addition of two values | x+y |
- | Subtraction | Subtraction of two values | x-y |
* | Multiplication | Multiplies two values | x*y |
/ | Division | Divides two values. If we divide a number by 0, it does not have any definitive meaning in arithmetic operations; hence, we will get undefined as a result. | x/y |
% | Modulus | Returns the divisor or the reminder | x%y |
Output
Create a Java class named ArithmeticOperatorsDemo and write the following code in that class:
The binary operator “+” is overloaded in the sense that the operation performed is determined by the type of the operands. When one of the operands is a String object, the other operand is implicitly converted to its string representation, and string concatenation is performed.
Example: Relational Operators
Relational operators in Java are used to compare two or more objects. Java provides six relational operators. All relational operators are binary operators, and their operands are numeric expressions.
Symbol | Name | Function |
---|---|---|
< | Less than | The value returned will be true. |
> | Greater than | The value returned will be true if the LHS is greater than the RHS. |
== | Equal To | The value returned will be true if the LHS is equal to the RHS. |
!= | Not Equal To | The value returned will be true if the LHS is not equal to the RHS. |
<= | Less than or Equal To | The value returned will be true if the LHS is less than or equal to the RHS. |
>= | Greater than or Equal To | The value returned will be true if the LHS is greater than or equal to the RHS. |
Binary numeric promotion is applied to the operands of these operators. The evaluation results in a boolean value. Relational operators have a lower precedence than arithmetic operators, but a higher precedence than the assignment operators. The program below demonstrates the different relational operators in Java.
Create a Java class named RelationalOperatorsDemo and write the following code in that class:
Output:
Example: Logical Operators
Logical operators return a true or false value based on the state of the variables. There are six logical operators. Each argument to a logical operator must be a Boolean data type, and the result is always a Boolean data type.
Name | Function |
---|---|
Logical AND | Value returned is true when both given conditions are true. |
Logical OR | Value returned is true when at least one given condition is true. |
Logical NOT | Has the power to reverse the overall result. For example, if the value returned is true, it gives out false. |
An example program is shown below that demonstrates the different Logical Operators in Java.
Create a Java class named LogicalOperatorsDemo and write the following code in that class:
Output:
Example: Bitwise Operators
Java provides Bitwise operators to manipulate the content of variables at the bit level. These variables must be of numeric data type (char, short, int, or long). Java provides seven bitwise operators, as shown in the list below:
Left Operand | Right Operand | Operator | Result |
---|---|---|---|
11001100 | 01010101 | & | 01000100 |
11001100 | 01010101 | ||
11001100 | 01010101 | ^ | 10011001 |
01010101 | ~ | NOT | 10101010 |
11001100 | 2 | << | 00110000 |
11001100 | 2 | >> | 00110011 |
The example program below demonstrates the different Bitwise operators in Java.
Create a Java class named BitwiseOperatorsDemo and write the following code in that class:
Output:
Example: Ternary Operator
Create a Java class named TernaryOperatorDemo and write the following code in that class:
Output:
Example: Unary Operators
Unary Operators only need a single operand, and they are used to increment, decrement, or eliminate a value.
Operator | Description |
---|---|
++ | Increment Operator - It will increment the value by 1. This operator can be used as a post-increment and as a pre-increment. Post increment means that we want to use the current value, and then perform the incrementation. Pre-increment means to perform the incrementation, and then use the value. |
- - | Decrement Operator - It will decrement the value by 1 (opposite of increment operator). As we have seen in the above increment operator, this operator also has two use cases. The first is post-decrement, the second is pre-increment. Post-decrement means that the value is first used, and then the computation is performed. Pre-decrement means that the computation or the value is first decremented, and then used. |
+ | Unary Plus Operator - Presents the positive value. |
- | Unary Minus Operator- Normally used for eliminating or negating values. |
! | Logical not Operator- It is used for inversion of the Boolean value. This operator reverses the value of a Boolean expression. For example, let’s say that a Boolean value is true. If we use the “!” operator with a true expression, it will give us the result as false. It inverses the Boolean value and returns the result |
Let's see how they perform in real-world situations. Create a Java class named UnaryOperator and write the code below in that class.
Output
Last updated