Java Operator
Java Operator
Types of Operator :-
1. Arithmetic operator
2. Relational Operator
3. Logical Operator
4. Conditional Operator
5. Assignment Operator
6. Bitwise Operator
7. Unary Operator
1. Arithmetic Operator - These are used for calculations. Addition, Subtraction, Multiply,
Division, Remainder
Program –
class Arth
{
public static void main(String args[])
{
int a=20, b=33;
System.out.println(“ addition” + (a+b));
System.out.println(“ Subtraction” + (a-b));
System.out.println(“ Multiply” + (a*b));
System.out.println(“ Division” + (a/b));
System.out.println(“ Remainder” + (a%b));
}
}
2. Relational Operator – These are used to compare two or more values or operands.
Program –
class Rela
{
public static void main(String args[])
{
int a = 20, b=67;
System.out.println(“ less than” + (a<b));
System.out.println(“ Greater than” + (a>b));
System.out.println(“ Less than equal to” + (a<=b));
System.out.println(“Greater than equal to” + (a>=b));
System.out.println(“ Equal to” + (a==b));
System.out.println(“ Not equal to “+ (a!=b));
}
}
The answer or output of this operator will be in True or False.
3. Conditional or Ternary Operator – It check the condition and decides the desired result on
the basis of both conditions.
class Conditionalop
{
public static void main(String args[])
{
int a = 20, b=67,c;
c=(a>b)?a:b;
System.out.println(“ ans is = “+c);
4. Assignment Operator -
Java is a popular programming language that software developers use to construct a wide range of
applications. It is a simple, robust, and platform-independent object-oriented language. There are
various types of assignment operators in Java, each with its own function.
In this section, we will look at Java's many types of assignment operators, how they function, and how
they are utilized.
Simple Assignment Operator (=)
To assign a value to a variable, use the basic assignment operator (=). It is the most fundamental
assignment operator in Java. It assigns the value on the right side of the operator to the variable on
the left side.
5. Unary Operator - The unary operator is an operator that can be used only with an operand.
It is used to represent the positive or negative value, increment/decrement the value by 1.
Program –
There are Five Unary Operators in Java
1. Unary Plus
2. Unary Minus
3. Unary Increment
4. Unary Decrement
5. Logical Complement Operator
class unary1
{
public static void main(String args[])
{
int a = 15;
System.out.println(++a);
System.out.println(a);
System.out.println(a++);
System.out.println(a--);
System.out.println(--a);
System.out.println(a);
6. Logical Operator - An operator can be defined as a symbol that is used for performing
different operations. In a programming language, there are various types of operators
such as arithmetic operators, relational operators, logical operators, assignment
operator, increment/decrement operators, conditional operators, bitwise operators, and
shift operators.
AND Operator ( && ) – if( a && b ) [if true execute else don’t]
class Main {
public static void main(String[] args) {
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
7. Bitwise operators in Java are used to perform operations on individual bits. For
example,
~ 00100011
________
11011100 = 220 (In decimal)
Here, ~ is a bitwise operator. It inverts the value of each bit (0 to 1 and 1 to 0).