Operators & Assignments by Durga Sir
Operators & Assignments by Durga Sir
2. arithmetic operators
4. Relational operators
5. Equality operators
6. instanceof operators
7. Bitwise operators
13. [] operator
Ex:
1. Increment & decrement operators we can apply only for variables but
not for constant values. Otherwise we will get compile time error.
Ex:
int x = 4;
int y = ++x;
System.out.pritnln(y); //output: 5
Ex 2:
int x = 4;
int y = ++4;
System.out.pritnln(y);
required: varialbe
found: value
int x= 4;
int y = ++(++x);
System.out.println(y);
required: varialbe
found : value
Ex:
final int x = 4;
x++; // x = x + 1
System.out.println(x);
x++;
System.out.println(x); //output:11
char ch='a';
ch++;
System.out.println(ch); //b
double d=10.5;
d++;
System.out.println(d); //11.5
boolean b=true;
b++;
System.out.println(b);
Ex 1:
byte a=10;
byte b=20;
System.out.println(c);
found: int
required: byte
Ex 2:
byte b=20;
System.out.println(c);
CE: possible loss of precession
found: int
required: byte
Ex:
byte b=10;
b++;
System.out.println(b); //output : 11
Arithmetic Operator:
1. If we apply any Arithmetic operation b/w 2 variables a & b,the
result type is always max(int, type of a , type of b).
Example:
byte + byte=int;
byte+short=int;
short+short=int;
short+long=long;
double+float=double;
int+double=double;
char+char=int;
char+int=int;
char+double=double;
Ex:
System.out.println(10/0.0); // output : infinity
System.out.println(-10/0.0); // output : - infinity
// Ex: x=10;
System.out.println(10 < Float.NaN ); // false
System.out.println(10 <= Float.NaN ); // false
System.out.println(10 > Float.NaN ); // false
System.out.println(10 >= Float.NaN ); // false
System.out.println(10 == Float.NaN ); // false
System.out.println(Float.NaN == Float.NaN ); // false
System.out.println(10 != Float.NaN ); //true
System.out.println(Float.NaN != Float.NaN ); //true
ArithmeticException:
1. It is a RuntimeException but not compile time error
3. The only operations which cause ArithmeticException are: ' / ' and
' % '.
String Concatenation operator:
1. The only overloaded operator in java is '+' operator sometimes it
access arithmetic addition operator & sometimes it access String
concatenation operator.
Ex:
String a="ashok";
Example:
Example:
String a="ashok";
a=b+c+d;
CE : incompatible type
found : int
required : java.lang.String
a=a+b+c; // valid
b=a+c+d;
found: java.lang.String
required: int
Example:
b=b+c+d; // valid
30);
Thread t3=t1 ;
System.out.println(t1==t2); //false
System.out.println(t1==t3); //true
To use the equality operators between object type compulsory
these should be some relation between argument types(child to
parent , parent to child), Otherwise we will get Compiletime
error incompatible types
System.out.println(o==s); //false
System.out.println(s==t);
String s=null ;
System.out.println(r==null); //true
System.out.println(null==null); //true
System.out.println(s1==s2); //false
System.out.println(s1.equals(s2)); //true
instanceof operator:
1. We can use the instanceof operator to check whether the given an
object is perticular type or not
Object o = l.get(0); // l is an array name
if (o instanceof Student) {
Ex:
}
To use instance of operator compulsory there should be some
relation between argument types (either child to parent Or parent
to child Or same type) Otherwise we will get compile time error
saying inconvertible types.
found : java.lang.Thread
required : java.lang.String
Example:
System.out.println(true&false);//false
System.out.println(true|false);//true
System.out.println(true^false);//true
Example:
System.out.println(4|5);//5 4-->100
System.out.println(4^5);//1 5-->101
Example:
Bitwise complement (~) (tilde symbol) operator:
1. We can apply this operator only for integral types but not for
boolean types.
Example:
boolean
System.out.println(~4); //-5
result is : 000...0101 =5
Note: The most significant bit access as sign bit 0 means +ve number,
1 means -ve number. +ve number will be represented directly in memory
whereas -ve number will be represented in 2's complement form.
Boolean complement (!) operator:
This operator is applicable only for boolean types but not for
integral types.
Example:
System.out.println(!true);//false
System.out.println(!false);//true
Summary:
&
& , | && , ||
Both arguments should be Second argument evaluation
evaluated always. is optional.
Relatively performance is Relatively performance is
low. high.
Applicable for both integral Applicable only for boolean
and boolean types. types but not for integral
types.
1. x&&y: y will be evaluated if and only if x is true.(If x is false
then y won't be evaluated i.e., If x is ture then only y will be
evaluated)
2. x||y: y will be evaluated if and only if x is false.(If x is true
then y won't be evaluated i.e., If x is false then only y will be
evaluated)
Example:
x++;
else {
y++;
System.out.println(x+"----"+y);
Example:
int x=10 ;
System.out.println("Hello");
else {
System.out.println("Hi");
output: Hi
1. implicit
2. explicit
System.out.println(x); //97
Diagram:
Example 1:
int x='a';
System.out.println(x);//97
Example 2:
double d=10;
System.out.println(d);//10.0
Diagram:
Example:
int x=130;
byte b=(byte)x;
System.out.println(b); //-126
Example 2:
int x=130;
byte b=x;
Example 3:
int x=150;
short s=(short)x;
byte b=(byte)x;
System.out.println(s); //150
System.out.println(b); //-106
Example 4:
double d=130.456 ;
int x=(int)d ;
System.out.println(x); //130
byte b=(byte)d ;
System.out.println(b); //-206
Assignment Operator:
There are 3 types of assignment operators
Simple assignment:
Example:
int x=10;
Chained assignment:
Example:
int a,b,c,d;
a=b=c=d=20;
System.out.println(a+"---"+b+"---"+c+"---"+d);//20---20---20---20
int b , c , d ;
Example 2:
int a=b=c=d=30;
symbol: variable b
Compound assignment:
Sometimes we can mixed assignment operator with some other
operator to form compound assignment operator.
Ex:
int a=10 ;
a +=20 ;
System.out.println(a); //30
The following is the list of all possible compound assignment
operators in java.
int a , b , c , d ;
a=b=c=d=20 ;
a += b-= c *= d /= 2 ;
System.out.println(a+"---"+b+"---"+c+"---"+d); // -160...-180--200--10
Ex 1:
int x=(10>20)?30:40;
System.out.println(x); //40
Ex 2:
int x=(10>20)?30:((40>50)?60:70);
System.out.println(x); //70
[ ] operator:
We can use this operator to declare under construct/create
arrays.
2. Arithmetic operators : * , / , % , + , -.
5. Equality operators: == , !=
9. Assignment operators: += , -= , *= , /= , %= . . .
Example:
package fundamental_java;
int i=1;
System.out.println(i); //13
description:
i=1+2+2+4+4;
i=13;
new Vs newInstance( ):
new is an operator to create an objects , if we know class name
at the beginning then we can create an object by using new
operator .
newInstance( ) is a method presenting class " Class " , which can
be used to create object.
If we don't know the class name at the beginning and its
available dynamically Runtime then we should go for newInstance()
method
public class Test {
Object o=Class.forName(arg[0]).newInstance( ) ;
System.out.println(o.getClass().getName( ) );
} }
}
}
java Test Test //true
java Test String //false
java Test Object //true