0% found this document useful (0 votes)
4 views7 pages

Java Operator Programs Explained

Uploaded by

V.Annapeachi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java Operator Programs Explained

Uploaded by

V.Annapeachi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programs on operators

1)
class AssignmentOperators
{
public static void main(String[] args)
{
int a =10 ;
int b = 5 ;
int c =5 ;
int d =11;
int e =13;

[Link](a+=5);//15

[Link](b-=10);//-5

[Link](c*=8);//40

[Link](d/=3);//3

[Link](e%=4);//1
}
}
Output:
15
-5
40
3
1
2)
class AssignmentOperators2
{
public static void main(String[] args)
{
int a=10;

[Link](a+=5);//a=a+5;--->10+5-->15
[Link](a-=5);//a=a-5;---->15-5--->10
[Link](a*=8);//a=a*8;------>10*80--->80
[Link](a/=3);
[Link](a%=4);

}
}
Output:
15
10
80
26
2

3)

class RelationalOperators
{
public static void main(String[] args)
{
int a = 20;
int b = 10;

boolean res = a>=b;

[Link](res);//true

[Link](a==20);//true

[Link](a<b);//false

[Link](b==15);//false

[Link](a!=b);//true
}
}

Output:
true
true
false
false
true
4)
class LogicalOperators
{
public static void main(String[] args)
{
int a=20;
int b=10;

boolean res = a>=b && a==10;

[Link](res);//false

[Link](a==20 || b>=15);//true

[Link](a>=b && b==10);//true

[Link](a==15 || b>=10);//true

[Link](!true);//false

[Link](!false);//true
}
}
Output
false
true
true
true
false
true
5)
class UnaryOperators
{
public static void main(String[] args)
{
int x=1;
int y=x++;
[Link]("x value is "+x+" y value is "+y);//2,1

int a =0;
int b =++a;
[Link]("a value is "+a+" b value is "+b);//1,1

int i=2;
int j=i--;
[Link]("i value is "+i+" j value is "+j);//1,2

int p=2;
int q=--p;
[Link]("p value is "+p+" q value is "+q);//1,1
}
}
Ouput
x value is 2 y value is 1
a value is 1 b value is 1
i value is 1 j value is 2
p value is 1 q value is 1
6)
class TernaryOperator
{
public static void main(String[] args)
{
int a=100;
int b=2000;
String res = a>b? "yes" : "no";
[Link](res);
}
}
Output
no

7)
public class TernaryOperator {

public static void main(String[] args) {

int a=100;
int b =20;
int c =10;

int max = a>b?a:b;

[Link](max);

[Link]("==================");

int tempMax = a>b?a:b;

int finalMax = tempMax > c? tempMax : c;

[Link](finalMax);

[Link]("===================");

int p=10 ,q=20, r=30;

int res = p>q?(p>r?p:r) : (q>r?q:r);


[Link](res);
}
}
Output:
100
==================
100
===================
30

You might also like