In this article, you will find everything about the operator, we will discuss the below things
What is Operator, Operator in java, Type of Operator, A special double-column operator in java?
Before going to deep dive in Operator in java, let’s understand
what is the operator
Operator :
The operator in java or computer programming, the operator is a symbol that usually represents an action/process. These symbols are the adapted form of mathematics.
OR
Operators are use full to perform a set of actions based on their meanings.
OR
An operator is an instruction for the compiler to perform specific Action (mathematical or logical manipulations)
What is Operator in java
An operator in java meaning remains the same in all programming languages, so in java above definition are fit means Operator in java is an instruction for a compiler, to perform some set of action, these actions may be arithmetical, logical, conditional.
Type of Operator in java
There are various types of operators in java.
what is the different type of Operator in Java
- Arithmetic Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators
- instance of operator
- Precedence and Associativity
Let’s take some example of all above type of operator’s
Arithmetic Operators
This is a special kind of operator in java This type of operator is used to perform some math metical operation like
*: Multiplication
/ : Division
% : Modulo
+ : Addition
– : Subtraction
Arithmetic operators in java Example
public class JSR_ArithmeticOperatorDemo { public static void main(String []args) { System.out.println("Welcome to JSR Techno Talks Blog"); int Value1 = 100; int Value2 = 20; System.out.println("Value2 + Value1: " + (Value2 + Value1) ); System.out.println("Value2 / Value1: " + (Value2 / Value1) ); System.out.println("Value2 % Value1: " + (Value2 % Value1) ); System.out.println("Value2 - Value1: " + (Value2 - Value1) ); System.out.println("Value2 * Value1: " + (Value2 * Value1) ); } }
OutPut :
Welcome to JSR Techno Talks Blog Value2 + Value1: 120 Value2 / Value1: 0 Value2 % Value1: 20 Value2 - Value1: -80 Value2 * Value1: 2000
Assignment Operators
This is a special kind of operator in java. equal ‘=’ is an Assignment operator, this operator is used to assign a value to any variable. It has a right to left associativity
Assignment Operator in Java Example
Means right-hand side of operator value is assigned to the left-hand side variable
Different Assignments operators in java are: =,/=, %=,+=, -=, *=
Value2 = Value1 would assign value of variable Value1 to the variable.
Value2+=Value1 is equal to Value2 = Value2+Value1
Value2-=Value1 is equal to Value2 = Value2-Value1
Value2*=Value1 is equal to Value2 = Value2*Value1
Value2/=Value1 is equal to Value2 = Value2/Value1
Value2%=Value1 is equal to Value2 = Value2%Value1
public class JSR_AssignmentOperatorDemo { public static void main(String args[]) { System.out.println("Welcome to JSR Techno Talks Blog"); int Value1 = 100; int Value2 = 20; Value2 = Value1; System.out.println("= Operator Result : "+Value2); Value2 += Value1; System.out.println("+= Operator Result : "+Value2); Value2 -= Value1; System.out.println("-= Operator Result : "+Value2); Value2 *= Value1; System.out.println("*= Operator Result : "+Value2); Value2 /= Value1; System.out.println("/= Operator Result : "+Value2); Value2 %= Value1; System.out.println("%= Operator Result : "+Value2); } }
Output
Welcome to JSR Techno Talks Blog = Operator Result : 100 += Operator Result : 200 -= Operator Result : 100 *= Operator Result : 10000 /= Operator Result : 100 %= Operator Result : 0
Relational Operators:
This is a special kind of operator in java. This kind of operators are used to check the relation between to operands we got a Boolean result after the comparison variable relation_operator value
public class JSR_operatorsDemo { public static void main(String[] args) { int Input1 = 210, Input2 = 110; String srt1 = "JSR", str2 = "JSR"; int arr1[] = { 10, 20, 30 }; int arr2[] = { 10, 20, 30 }; boolean condition = true; System.out.println("Input1 greater than = Input2 :" + (Input1 >= Input2)); System.out.println("Your Input1 Not equal Input2 :" + (Input1 != Input2)); System.out.println("Your Input1 equal Input2 :" + (Input1 == Input2)); System.out.println("Your Input1 less than Input2 :" + (Input1 < Input2)); System.out.println("Your Input1 less tan and = Input2 :" + (Input1 <= Input2)); System.out.println("Your Input1 greater Input2 :" + (Input1 > Input2)); System.out.println("Your string srt1 == str2 : " + (srt1 == str2)); } }
Some of the relational operators are-
- ==, Equal to the operator: If the left-hand side is equal to the right-hand side then, return true.
- !=, Not Equal to: If left-hand side is not equal to the right-hand side, returns true
- <, less than if the left-hand side is less than the right-hand side, returns true
- <=, less than or equal to if the left-hand side is less than or equal to the right-hand side, returns true
- >, Greater than: returns true of the left-hand side is greater than the right-hand side.
- >=, Greater than or equal to returns true of the left-hand side is greater than or equal to the right-hand side.
OutPut
Input1 greater than = Input2 :true Your Input1 Not equal Input2 :true Your Input1 equal Input2 :false Your Input1 less than Input2 :false Your Input1 less tan and = Input2 :false Your Input1 greater Input2 :true Your string srt1 == str2 : true
Logical Operators:
This kind of operators are used to perform “AND” and “OR” operation
Conditional operators are
&&, Logical, AND:
if both conditions are true. returns true
• ||, Logical, OR
If the least one condition is true, return true
public class JSR_LogicalOperatorDemo { public static void main(String args[]) { boolean Value1 = true; boolean Value2 = false; System.out.println("Value1 && Value2: " + (Value1&&Value2)); System.out.println("Value1 || Value2: " + (Value1||Value2)); System.out.println("!(Value1 && Value2): " + !(Value1&&Value2)); } }
OutPut
Value1 && Value2: false Value1 || Value2: true !(Value1 && Value2): true
Ternary Operator
The ternary operator in java syntax
Its a shorthand condition of the if-else condition
Syntax is-
condition ? if true : if false
variable number1 = (expression) ? value if true : value if false
public class JSR_TernaryOperatorDemo { public static void main(String args[]) { int Value1, Value2; Value1 = 25; Value2 = (Value1 == 10) ? 100: 200; System.out.println( "Value2: "+Value2); Value2 = (Value1 == 25) ? 100: 200; System.out.println( "Value2: "+Value2); } }
OutPut
Value2: 200 Value2: 100
Bitwise Operators
BITWISE operator acts for bit-level 32 bit. They can be used with any of the integer types.
There is a different kind of bitwise Operators:
• &, Bitwise AND operator: return bit by bit AND of input values.
• |, Bitwise OR operator: returns bit by bit OR of input values.
• ^, Bitwise XOR operator: returns bit by bit XOR of input values.
• ~, Bitwise Complement Operator: returns the one’s complement representation of the input value
public class JSR_operatorsdemo { public static void main(String[] args) { int Value1 = 0x0015; int Value2 = 0x0017; System.out.println("Value1&Value2 = " + (Value1 & Value2)); System.out.println("Value1|Value2 = " + (Value1 | Value2)); System.out.println("Value1^Value2 = " + (Value1 ^ Value2)); System.out.println("~Value1 = " + ~Value1); Value1 &= Value2; System.out.println("Value1= " + Value1); } }
OutPut:
Value1&Value2 = 21 Value1|Value2 = 23 Value1^Value2 = 2 ~Value1 = -22 Value1= 21
double colon (::) operator
We will update this soon…
Recent Comments