OPERATORS IN JAVA
INTRODUCTION
Operator is a special symbol which tells to the programming language's compiler to perform some specific operations.
Java provides a rich set of operators, help to perform different types of operations.
Like when we need to perform basic arithmetic operations then we need to use plus sign (+) operator for addition, minus sign (-) operator for subtraction, etc.
In Java programming language, operators can be classify into the following categories.
1. Arithmetic operators
2. Relation operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Conditional operators
7. Misc operators
ARITHMETIC OPERATORS
Arithmetic operators are used to perform different types of arithmetic operations like: addition, subtraction, multiplication, division and these are very helpful to solve the mathematical problems.
In Java programming language, operators can be classify into the following categories.
1. Arithmetic operators
2. Relation operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Conditional operators
7. Misc operators
ARITHMETIC OPERATORS
Arithmetic operators are used to perform different types of arithmetic operations like: addition, subtraction, multiplication, division and these are very helpful to solve the mathematical problems.
OPERATOR SIGN
|
OPERATOR FUNCTION
|
+
|
To adds two operands.
|
-
|
To subtract second operands from first.
|
*
|
To multiply two numerical values.
|
/
|
To divide numerator by de-numerator.
|
%
|
To get remainder of division.
|
++
|
Increment operator, increases integer value by one.
|
--
|
Decrement operator, decreases integer value by one.
|
ARITHMETICAL OPERATORS EXAMPLE
class A_Operators
{
public static void main (String as[])
{
int a,b;
a=6;
b=4;
System.out.println("Addition is: "+(a+b));
System.out.println("Subtraction is: "+(a-b));
System.out.println("Multiplication is: "+(a*b));
System.out.println("Division is: "+(a/b));
}
}
- - - PROGRAM OUTPUT - - -
RELATIONAL OPERATORS
Relational operators are used to test the comparison between selected values and.
These operators can be used to test whether two values are equal or not equal or less than or greater than and other testing conditions.
OPERATOR SIGN
|
OPERATOR FUNCTION
|
==
|
To check, two operand are equal
|
!=
|
To check, two operand are not equal.
|
>
|
To check value on the left is greater than from the right side.
|
<
|
To check value on the left side is smaller than from right side.
|
>=
|
To check left side value is greater than or equal to from the right side value.
|
<=
|
To check left side value is smaller than or equal to from the right side value.
|
INCREMENT & DECREMENT OPERATORS EXAMPLE
class A_Operators
{
public static void main (String as[])
{
int c=10;
System.out.println("Increment operator example: "+(c++));
System.out.println("Decrement operator example: "+(c--));
}
}
- - - PROGRAM OUTPUT - - -
LOGICAL OPERATORS
Logical Operators are used to check the different types of conditional expressions.
For example, we can use logical operators, in if we require a statement to evaluate conditional based expression.
Java supports mainly three types of logical operators which are described below.
OPERATOR SIGN
|
OPERATOR MEANING
|
OPERATOR EXAMPLE
|
&&
|
Logical AND
|
(a && b) is false
|
||
|
Logical OR
|
(a || b) is true
|
!
|
Logical NOT
|
(!a) is false
|
AND LOGICAL OPERATOR EXAMPLE
class L_Operator
{
public static void main(String as[])
{
int a=5;
int b=3;
int c=4;
if(a>b && a>c)
System.out.println("Largest value is: "+a);
else if(b>a && a>c)
System.out.println("Largest value is: "+b);
else
System.out.println("Largest value is: "+c);
}
}
- - - PROGRAM OUTPUT - - -
OR LOGICAL OPERATOR EXAMPLE
class L_Operator
{
public static void main(String as[])
{
int marks=45;
if(marks>33 || marks==33)
System.out.println("Congrats, you are pass");
else
System.out.println("Sorry, you are failed");
}
}
- - - PROGRAM OUTPUT - - -
NOT LOGICAL OPERATOR EXAMPLE
class L_Operator
{
public static void main(String as[])
{
int number=13;
if(number != 0)
System.out.println("Odd number");
else
System.out.println("Even Number");
}
}
- - - PROGRAM OUTPUT - - -
CONDITIONAL OPERATOR
The conditional operator is also known as ternary operator because it works with three operands (variables).
Actually it is short alternative of if-else statement therefore it can be used to evaluate Boolean expression and return either true or false value.
CONDITIONAL OPERATOR EXAMPLE
class C_Operator
{
public static void main(String as[])
{
int a=12;
int b=15;
int big;
big=(a > b) ? a:b;
System.out.println("Largest value is: "+big);
}
}
- - - PROGRAM OUTPUT - - -
0 Comments