0% found this document useful (0 votes)
8 views3 pages

CH 2 Operatores

The document covers various Java operators including increment/decrement, arithmetic, string concatenation, and equality operators, explaining their behaviors and differences. It also discusses type casting, the use of the 'new' operator versus 'newInstance()', and the distinctions between ClassNotFoundException and NoClassDefFoundError. Additionally, it compares 'instanceof' and 'isInstance()' for type checking in Java.

Uploaded by

Bl Gocher
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)
8 views3 pages

CH 2 Operatores

The document covers various Java operators including increment/decrement, arithmetic, string concatenation, and equality operators, explaining their behaviors and differences. It also discusses type casting, the use of the 'new' operator versus 'newInstance()', and the distinctions between ClassNotFoundException and NoClassDefFoundError. Additionally, it compares 'instanceof' and 'isInstance()' for type checking in Java.

Uploaded by

Bl Gocher
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/ 3

1-INCREMENT-DECREMENT:

pre inc ++a postinc a++


>we can apply for variable but
> can not apply for final and constant values

2 ARTHIMATIC OPERATORES:
> if we apply any operator bw 2 var's a and b then result type is always max
int+int=int
byte+byte=int
byte+short=int
short+short=int
int+long=long
long+float=float
float+double=double
char+char=int ex sop('a'+'b'); // 195
char + int=int ex sop('a'+'1'); // 198
char + double=double ex: sop('a'+1.1); // 98.1

Note:[NAN- NOT A NUMBER] in case of float and double if result is undefined then
its hold by NAN.
0/0.0 , 10/0.0

3 String concatenation operator(+):


-----------------------------------
> only one overloaded operator in java is + operator.
if atleast one argument is String type then + operator acts concatation and if
both args are
Number type then operator act as arthimatic addition.

4 EQUALITY OPERATOR(==, !=):


----------------------------
>we can apply equality operator for object type also.
imp note: => if we can apply equality operator for object, there should be some
parent/child rel
else we get error and if we dont want to get error then use instanceof operator.
Thread t1 = new Thread();
Thread t2 = new Thread();
t1==t2 -----> returns 'true' if both are pointing to same obj
(reference/ address comparasion).

DIFF BW == OPERATORE AND .EQUAL() METHOD:


------------------------------------------
>we use == for ref(address) comparison and .equals() for content comparison.
ex: String s1 = new String("durga");
String s2 = new String("durga");
s1==s2 -->false
sop(s1.equals(s2)); --> true bcoz content is same

INSTANCEOF: OPERATOR
---------------------
>to check the given object is perticular type or not.
r instanceof x
here r is obj ref, x is class/interface
>but there should parent child relation else its errore
Type Cast operators:
====================
1 implict [automatic/upcasting/widdening]:
compiler is resp to do this type cast, its required if we assign low dt to high
dt.

ex byte -> short -> int -> long -> float-> double
|
char

2 explicit [downcasting/narrowing]:
> developer is resp to do this cast, if we assign high dt to low dt using by cast
operator.

NEW OPERATOR:
=============
> used to create an object

NEW V/S newInstance();


----------------------
>new is operator to create an object. if we know class name at beginning then we
can create
obj by using new operator.
>newInstance() is a method present in Class-class ,
which can be used to create object if we dont know class name at beginning and its
availale dynamically runtime then should go for newInstance().
ex: Object o=Class.forName(arg[0]).newInstance()
>if we use newInstance() then class should with no-arg constructor

DIFF BW ClassNotFoundException and NoClassDefFoundError:


----------------------------------------------------------
NoClassDefFoundError:
---------------------
>if hard coded class .class file not availble at runtime then we get runtime this
exception,
> which is unchecked.
ex : Test t = new Test();
at runtime, if test.class file is not availble then we get -NoClassDefFoundError

ClassNotFoundException:
-----------------------
if dynamically provided class name is not availble at runtime then we get runtime
exception - ClassNotFoundException
ex: Object o = Class.forName("Test").newInstance();
then at runtime if this Test class file not availble so we get
ClassNotFoundException error.

instanceof Vs isInstance();
============================
>instanceof: is a operatore to check whether given object is of perticular type or
not
and type is specified at beginning.
Thread t= new Thread();
sop(t instaneof Runnable); /true

>isInstance(): is a method to check wheather the given object is of perticular type


or not
and is specified dynamically at runtime.

Thread t = new Thread();


sop(Class.forName(args[0]).isInstance(t));

java Test Runnable > true


java Test String > false

You might also like