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

Java Operators Explained: Examples & Usage

The document provides examples and explanations of various operators in Java, including arithmetic, relational, bitwise, logical, ternary, and assignment operators. It includes code snippets demonstrating the usage of these operators and their outputs, as well as assignments for practice. Additionally, it discusses operator precedence and type conversions in expressions.

Uploaded by

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

Java Operators Explained: Examples & Usage

The document provides examples and explanations of various operators in Java, including arithmetic, relational, bitwise, logical, ternary, and assignment operators. It includes code snippets demonstrating the usage of these operators and their outputs, as well as assignments for practice. Additionally, it discusses operator precedence and type conversions in expressions.

Uploaded by

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

operators

ex 1: Arithemtic Operators
class Test
{ public static void main(String[] args)
{ int a=20,b=5;
[Link](a+b);
[Link](a-b);
[Link](a*b);
[Link](a/b);
[Link](a%b);
}
}

operators flow :
( )
* / %
+ -

ex: write the output.


class Test
{ public static void main(String[] args)
{ [Link]("ratan"+"anu");
[Link](10+20+"ratan"+"naresh");
[Link](10+"ratan"+20+5+"naresh");
[Link](10+"ratan"+20+5+"naresh"+3+3);
[Link](10+"ratan"+(20+5)+("naresh"+3+3));
[Link](10+"ratan"+3*10/2+"naresh");
[Link](10+"ratan"+3*10/2-1+"naresh");
}
}

ex 2: Relational Operators : return boolean


class Test
{ public static void main(String[] args)
{ int a=20,b=5;
[Link](a>b); //true
[Link](a<b); //false
[Link](a==b); //false
[Link](a!=b); //true
[Link](a>=b); //true
[Link](a<=b); //false
}
}

ex 3: bitwise operator & | ~


32 16 8 4 2 1
0 1 1 0 = 6
1 1 1 1 = 15
1 0 0 1 1 = 19
1 0 0 0 1 1 = 35
0 1 1 0 = 6
0 1 0 1 = 5
1 1 0 0 1 = 25
1 0 0 1 1 = 19
1 0 1 0 0 0 = 40
1 1 1 1 = 15

a b a&b a|b
t t t t
t f f t
f t f t
f f f f

a b a&b a|b
1 1 1 1
1 0 0 1
0 1 0 1
0 0 0 0

& : all are true then it is true


| : any one is true then it is true

[Link](10&7);
10: 1 0 1 0
7 : 0 1 1 1
-------
0 0 1 0 = 2
-------

[Link](10|7);
10: 1 0 1 0
7: 0 1 1 1
-------
1 1 1 1 = 15
-------

class Test
{ public static void main(String[] args)
{ [Link](10&7);
[Link](4&6);
[Link](2&4);

[Link](10|7);
[Link](4|6);
[Link](2|4);

[Link](~123); //add 1 chage sign //-124


[Link](~-123); //sub 1 change sign //122
}
}

ex-4: Logical operators && || !


& : this can used for both bitwise calculations & logical conditions
[Link](10&6); //valid
10>20 & 20>30 //valid

&& : this can be used only for logical calculations


[Link](10&&6); //invalid
10>20 && 20>30 //valid

cond1 & cond2


It will check the both conditions then only it will give result.

cond1 && cond2


Here the second condition execution depends on first condition
if the first condition is true then only second condition exec
if the first condition is false then second condition not exec
case 1 : Here both conditions are executed so second condition will get
ArithmeticException
class Test
{ public static void main(String[] args)
{ if(10>20 & 10/0==0)
[Link]("Good morning");
else
[Link]("Good Night");
}
}
E:\>java Test
Exception in thread "main" [Link]

case 2:
class Test
{ public static void main(String[] args)
{ if(10>20 && 10/0==0)
[Link]("Good morning");
else
[Link]("Good Night");
}
}
Here second condition execution depends on first condition
Here first condition is fail so secondition not executed it return false.

cond1 | cond2
It will check the both conditions then only it will give result.

cond1 || cond2
Here the second condition execution depends on first condition
if the first condition is false then only second condition exec
if the first condition is true then second condition not exec.

case 1:Here both conditions are executed so second condition will get
NullPointerException.
class Test
{ public static void main(String[] args)
{
if(10<20 | 10/0==0)
[Link]("Good morning");
else
[Link]("Good Night");
}
}
Exception in thread "main" [Link]: / by zero

E:\>java Test
Exception in thread "main" [Link]

case 2:
class Test
{ public static void main(String[] args)
{ if(10<20 || 10/0==0)
[Link]("Good morning");
else
[Link]("Good Night");
}
}
Here second condition execution depends on first condition
Here first condition is fail so secondition executed it wii give NPE.

ex: not
class Test
{ public static void main(String[] args)
{ [Link](!true);
[Link](!false);
[Link](!"ratan"); error: bad operand type String for
unary operator '!'
}
}

Assignment-1:
take the day from end user
Enter day : suday
mon,tue : Discount 2%
web , thr, fri : Discount 4%
sat, sun : Discoutn 6%
if user enter invalid data give proper error message.

Assignment-2:
take the username , password from end user
if username=="ratan" password=="anu"
Login Success
else
Login Fail

ex 5: Ternary operator : it is a short form of if-else


class Test
{ public static void main(String[] args)
{ if (10>20)
{ [Link]("ratan");
}
else
{ [Link]("anu");
}

String res = 10>20?"ratan":"anu";


[Link](res);

int y = 10<20?100:200;
[Link](y);
}
}

ex:
class Test
{ public static void main(String[] args)
{ int x=10,y=20;
if (x>y)
{ [Link](x);
}
else
{ [Link](y);
}
//write the above code using ternary operator.
int s = x>y?x:y
[Link](s);
}
}

ex 1: increament & decarement : unary operators


++a : pre increment : increment the value then print
a++ : post increment : print the value then increment
--a : pre decrement : decrease the value then print
a-- : post decrement : print the value then decrease

class Test
{ public static void main(String[] args)
{ int a=10;
[Link](a++);
[Link](++a);
[Link](a--);
[Link](--a);
[Link](a);
}
}

ex: Assignment
class Test
{ public static void main(String[] args)
{ int a=10;
[Link](a++ + ++a );
[Link](a++ - ++a );
[Link](a-- + --a );
[Link](a-- - --a );
}
}

ex: assignemnt
class Test
{ public static void main(String[] args)
{ int a=10;
[Link](a++ + ++a + --a + a--);
[Link](a++ - ++a - --a - a--);
}
}

class Test
{ public static void main(String[] args)
{ int a=10;
[Link](a++ + ++a + --a + a--);
[Link](a++ - ++a - --a - a--);
}
}

Assignment operators:
normal assignment : =
Augumented Assignment : *=, += ,-= ,/= , %=

// this is normal assignment


val = 10
[Link](val)

//augumented assignment
val += 10 # val = val + 10
val -= 4 # val = val -4
val /= 2 # val = val / 2
val %= 2 # val = val %2
print(val)

ex : write the output of the example.


class Test
{ public static void main(String[] args)
{ int a=10;
a += 1;
a -= 2;
a *= 3;
a /= 3;
[Link](a);
}
}

byte + byte = int


short + short = int
byte + short = int
int + int = int
int + long = long
long + byte = long
long + long = long

float +float = float


float + int = float
float + byte = float
float + double = double
double + double = double
double + int = double

int + char = int


float + char = float
char + char = int

"a" + "a" = aa : valid


'a'+'a' = aa : Invalid
'a'+'a' = 194 : valid
'a'+'b' = 195 : valid
'd'+ 'A' = 165 : valid

class Test
{ public static void main(String[] args)
{ byte b1 = 10;
byte b2 = 20;
int b3;
b3 = b1 + b2;
[Link](b3);
}
}

class Test
{ public static void main(String[] args)
{ float f = 10.5f;
double d = 20.3;

double res;
res = f+d;
[Link](res);
}
}

class Test
{ public static void main(String[] args)
{ int a = 10;
char ch = 'a';
int result;
result = a+ch;
[Link](result);
}
}

You might also like