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

Operators

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)
4 views7 pages

Operators

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
You are on page 1/ 7

operators

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

operators flow :
( )
* / %
+ -

ex: write the output.


class Test
{ public static void main(String[] args)
{ System.out.println("ratan"+"anu");
System.out.println(10+20+"ratan"+"naresh");
System.out.println(10+"ratan"+20+5+"naresh");
System.out.println(10+"ratan"+20+5+"naresh"+3+3);
System.out.println(10+"ratan"+(20+5)+("naresh"+3+3));
System.out.println(10+"ratan"+3*10/2+"naresh");
System.out.println(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;
System.out.println(a>b); //true
System.out.println(a<b); //false
System.out.println(a==b); //false
System.out.println(a!=b); //true
System.out.println(a>=b); //true
System.out.println(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

System.out.println(10&7);
10: 1 0 1 0
7 : 0 1 1 1
-------
0 0 1 0 = 2
-------

System.out.println(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)
{ System.out.println(10&7);
System.out.println(4&6);
System.out.println(2&4);

System.out.println(10|7);
System.out.println(4|6);
System.out.println(2|4);

System.out.println(~123); //add 1 chage sign //-124


System.out.println(~-123); //sub 1 change sign //122
}
}

ex-4: Logical operators && || !


& : this can used for both bitwise calculations & logical conditions
System.out.println(10&6); //valid
10>20 & 20>30 //valid

&& : this can be used only for logical calculations


System.out.println(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)
System.out.println("Good morning");
else
System.out.println("Good Night");
}
}
E:\>java Test
Exception in thread "main" java.lang.ArithmeticException

case 2:
class Test
{ public static void main(String[] args)
{ if(10>20 && 10/0==0)
System.out.println("Good morning");
else
System.out.println("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)
System.out.println("Good morning");
else
System.out.println("Good Night");
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero

E:\>java Test
Exception in thread "main" java.lang.NullPointerException

case 2:
class Test
{ public static void main(String[] args)
{ if(10<20 || 10/0==0)
System.out.println("Good morning");
else
System.out.println("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)
{ System.out.println(!true);
System.out.println(!false);
System.out.println(!"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)
{ System.out.println("ratan");
}
else
{ System.out.println("anu");
}

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


System.out.println(res);

int y = 10<20?100:200;
System.out.println(y);
}
}

ex:
class Test
{ public static void main(String[] args)
{ int x=10,y=20;
if (x>y)
{ System.out.println(x);
}
else
{ System.out.println(y);
}
//write the above code using ternary operator.
int s = x>y?x:y
System.out.println(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;
System.out.println(a++);
System.out.println(++a);
System.out.println(a--);
System.out.println(--a);
System.out.println(a);
}
}

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

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

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

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

// this is normal assignment


val = 10
System.out.println(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;
System.out.println(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;
System.out.println(b3);
}
}

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

double res;
res = f+d;
System.out.println(res);
}
}

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

You might also like