Professional Documents
Culture Documents
- Increment Operator :
1: Pre-Increment
2: Post-Increment
- Decrements Operator
1: Pre Decrement
2: Post Decrement
Y=++x 10 11 11
Y=x++ 10 10 11
Y= - - x 10 9 9
Y=x-- 10 10 9
- It can apply increment & decrements operator only for variables. The platform constant value if
you are try to apply for constant value we will get compile time error.
Fox example :
int x =10
int y = ++(++x)
For example:
x =20
x++;
System.out.println (x)
Output : CompileTimeErrot: illegal start of expression.
- We can apply increment /decrements operator for every primitive type except Boolean
x++;
System.out.println(x)
int ch='a'
ch++
System.out.println (ch);
double d =10.5;
d++
System.out.println(d)
boolean b= true;
b++;
System.out.println(b);
Output CompileTimeError : bad operand type Boolean for un-array operator '++'
- If apply any arithmetic operator between two variable a & b, the result type is always max(int,
type of a, type of b).
byte a =10;
byte b =20;
byte c= a+b;
System.out.println (c);
Ex: 1
Result: 30
Ex : 2
byte b=10;
b=b+1;
System.out.println(b);
b = (byte)(b+1);
Output : valid.
Ex: 3
Ex :
byte b=10
b++
System.out.println(b)
Arthmetic Operator:
If you apply any arithmetic operator between tow variable a & b the result type is always max(int,
type of a, type of b).
For check
- In integral arithmetic (byte, short,int,long) there is no way to represent infinity.Hence if infinity
is result we will get arithmetic exception in integral arithmetic.
- But in floating point arithmetic (float & double) there is way to represent infinity.
-For this float and double classes contains the following the two Constance POSITVE_INFINITY &
NEGATIVE_INFINITY. Hence even though result is infinity we want get any arithmetic exception in
floating point arithmetic.
javap java.lang.Double
javap java.lang.Integer
- NaN (Not a Number) : In integer arithmetic (byte, short, int , long) there is no way to represent
undefined result. Hence the result is undefined will be get run time exception saying arithmetic
exception.
System.out.println (0/0)
- But any floating point arithmetic (Float & Double) there is a way to represent undefined result.
For this float and double classes contains NaN constant.Hence it result is undefined we would not
get any arithmetic exception in floating point arithmetic.
- For any x value including NaN the following expression return are false.
- For any x value including Nan the following expression return true.
x != NaN;
System.out.println (10 != Float.NaN) true
- Arithmetic Exception:
- The only overloaded operator in java is + operator.Sometimes it's arithmetic position operator
and some time it's string concatenation operator.
String a ="durga";
Case 1: If at least one arguments is String type then + operator as a concatenation operator and
if both arguments are number type then + operator as a arithmetic position operator.
Case 2:
Case 3:
System.out.println:
String a ="durga";
- We can apply relational operator for every primitive type except boolean.
- String of retalionl operator is not allowed otherwise to get compile time error.
- We can apply equlity operator for every primitive type including boolean type also.
- r1 == r2 return true; if one only if both reference operate to same object. Reference
compression are address compression.
Example 1:
Thread t3 = t1;
- If you apply equality operator for object type then compulsorily there should be some relation
between argument types.(whether child to parent, parent to child or same type), other while we
will get compile time error saying incomparable type.
- In general we can use == operator for reference compression (address compression) and equals
method for content compression.
For ex:
- For any object reference are r==null is always false. But null==null always true.
String s = null;
- instanseOf Operator:
- We can use instanseof operator two check whether to check object to particular or not.
Object o = l.get();
if (o.instanceof Student)
Customer c = (customer)o
Syntax : r instanceof x
- Use instanceof operator compulsory there should be some relation between argument types
(Child to parent, parent to child). Other wise compile time error saying incontrovertible types.
Note : for any class of interface x "null instanceOf x" is always false.
^ (XOR) - Return true if and only both arguments are different (true - false, false-true)
Ex: System.out.println (4 & 5); Result : true :4 100 & 101 = 100
- We can apply this operator only for integral type, but not for boolean type. if u are trying to
apply boolean type , then we will get compile time error.
- positive number will be represented directly in memory where as -ve number will be
represented in directly in memory in twos complement form.
- We can apply this operator only for boolean types, but not for integer types.
System.out.println(~ true);
- These are exactly same as bitwise operator (& ,|) except the following differences
&,I && , II
Applicable for both boolean & integer types Applicable only for boolean but not for integer
types
else { y++; }
Output
Operator X y
& 11 17
&& 11 16
I 12 16
II 12 16
- If we replace && with & then we will be get run time exception showing arthemitice exception
by zero.
- Typecasting operator
2: Whenever we are assigning smaller type of value to bigger data type variable implicit data type
casting will be performer.
- The following are various possible conversion where implicit type casting will be performer.
byte -> short -> int -> long -> float -> double
2: Whenever we are assigning bigger data type value to smaller data type variable then explicit
type casting is required.
- The following are various possibilities where explicit type casting is required.
double -> float -> long -> int -> short -> byte
int -> char
- When ever we are assigning bigger data type value to smaller data type variable by explicit type
casting. The most significant bits will be last we how to consider only least significant bits.
If you assign floating point values to the integer type, by explicit type casting the digit after
decimal point will be loss.
int x =130;
byte b =x;
int x ='a'
System.out.println (x);
int d =10;
System.out.println (d);
- Assignment Operator:
Ex:
int a,b,c,d;
a=b=c=d =20
System.out.println (a+""+b+""+c+""+d);
Ex: We can not perform changed assignment directly at the time of declaration.
int a=b=c=d =20 : Result : Compile time error (Cannot find symbol : symbol :b lactation :
Class Test)
int b,c,d;
It is valid.
- Compound Assignment Operator:
- Sometimes assignment operator mixed with some other operator such type of assignment
operator are called compound assignment operators.
Ex:
int a=10;
a + =20;
+=, -+, *=, /=, %=, &= |=, ^=, >>=, >>>=, <<=,<<<=
- In the case of compound assignment operator internal type casting will be performed
automatically.
11 11
int a,b,c, d;
a=b=c=d=20;
sopln(a+ ""+b+""+c+""+d);
Result : -160,-180,200 10
- Conditional Operator (? :)
Ex:
- New operator:
- In java we have only new keyword but not delete keyword because destruction of useless object
is the responsibility of garbage collector.
- [] operator:
- Operator Precedence :
- In java we have only operator precedence but not operand precedence.Before applying any
operator all operand will be evaluated from left to right.
class Test {
System.out.println(m1(1)+m1(2)*m1(3)/m1(4)+m1(5)*m1(6));
System.out.println(i);
return i;
- new vs newInstance :
-We can use new operator to create an object if known classes as beginning.
Test t = new Test();
-newInstance is a method present in class Class. We can use new instance method to create
object if you don’t class name of beginning and if will available dynamically at run time.
Ex:
clast Student{}
class Customer {}
class Test{
Object o =Class.forName(args[0]).newInstance();
In the case of new operator based on other requirement we can invoke any constructor.
- But new instance Method internally called no argument constructor. Hence to use new instance
method compulsory corresponding should contain no argument constructor. Other wise we will
get run time exception, saying Instantion Exception.
- Using new operator at run time is corresponding that class file is not available, then we will be
get run time exception saying NoClassGetFound error:Test.
- While using newInstance method at run time is corresponding is not available then we will get
run time exception saying, ClassNotFoundException.
At run time if Test123.class file is not available then we will get run time exception saying,
ClassNotFoundException : Test123
New newInstance
We can use new operator to create object if we We can use this method to create object if we
know class name at beginning don't know class name at the beginning and it
is available dynamically at runtime.
To use new operator class not required to To use newInstance() compulsoury class should
contain no-args constructor contain no-args constructor other wise we get
Run time Initantiation Exception
- ClassNotFoundException vs NoClassDefFoundError
- For hard coded class name , at run time it corresponding that class file is not available then we
will be get run time exception saying NoClassgetFoundError,which is unchecked.
- Get run time if Test.class is not available then we will get run time exception saying
NoClassGetFoundError:Test
- For dynamically provided class name get run time it corresponding that class file is not available
then we will get run time exception saying ClassNotFoundException, which is checked.
Get run time if Student.class file is not available then we will get run time exception saying
ClassNotFoundException:Student.
- instanceof vs isInstance() :
- instanceof is operator in Java, we can use instanceOf to check whether the given object is of
particular type or not and we know the type of beginning .
- Each instance is method present in java.lang.class We can use is instance method to check
whether object is a particular type or not and we don’t about are beginning and is available
dynamically at run time.
Ex :
class Test {
System.out.println(Class.forName(args[0]).isInstance(t);