Operators
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 :
( )
* / %
+ -
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
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);
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
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);
}
}
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 : *=, += ,-= ,/= , %=
//augumented assignment
val += 10 # val = val + 10
val -= 4 # val = val -4
val /= 2 # val = val / 2
val %= 2 # val = val %2
print(val)
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);
}
}