Arithmetic Operators
Arithmetic operators perform the
Arithmetic operations. It takes two or more operands and returns the result of
specific mathematical calculation.
Operator
|
Description
|
+
|
add
two or more numbers and also concatenate two Strings
|
-
|
subtract
a number from another.
|
*
|
multiply
two or more numbers.
|
/
|
divide
a number by another.
|
%
|
give
remainder of a division.
|
Example
public class Airthmatic_operator
{
public static void main(String
args[])
{
int a=10,b=5;
System.out.println("a+b = "+(a+b));
System.out.println("a-b = "+(a-b));
System.out.println("a*b = "+(a*b));
System.out.println("a/b = "+(a/b));
System.out.println("a%b = "+(a%b));
}
}
Relational Operator
A relational operator tests
some kind of relation between two identifiers. Relational operator compares
between two values and determines the relationship between the same. Relational
operators determine if one operand is greater than, less than, equal to, or not
equal to another operand.
Operator
|
Description
|
operand1
> Operand2
|
Check
if Operand1 is greater than Operand2
|
Operand1
>= Operand2
|
Check
if Operand1 is greater than or equal
to Operand2
|
Operand1
< Operand2
|
Check
if Operand1 is less than to Operand2
|
Operand1
<= Operand2
|
Check
if Operand1 is less than or equal to
Operand2
|
Operand1
== Operand2
|
Check
if Operand1 and Operand2 are equal
|
Operand1
!= Operand2
|
Check
if Operand1 and Operand2 are not equal
|
Example
public class Relational_Operator
{
public static void main(String
args[])
{
int a=10,b=5;
System.out.println("Value of
a = "+a+" b= "+b);
System.out.println("a=b
"+(a==b));
System.out.println("a
"+(a
System.out.println("a<=b
"+(a<=b));
System.out.println("a>b "+(a>b));
System.out.println("a>=b
"+(a>=b));
System.out.println("a!=b
"+(a!=b));
}
}
Ternary
Operator
Conditional operator also known
as ternary operator or an inline if. The conditional operator gives a shorter
syntax for if-else statement. The first operand is a boolean expression; if the
expression is true then the value of the second operand is returned otherwise
the value of the third operand is returned. The second and third operand is
separated by ? and :.
Example-1
public class Ternary_Operator
{
public static void main(String
args[])
{
int a=10,b=20;
System.out.println("The largest between a and b
("+a+","+b+") = "+(a>b?a:b));
}
}
Example-2
public class Ternary_Operator_1
{
public static void main(String
args[])
{
int a=10,b=20,c=5;
System.out.println("The
largest between a and b ("+a+","+b+","+c+") =
"+(a>b?(a>c? a : c):(b>c?b:c)));
}
}
No comments:
Post a Comment