You are on page 1of 16

1

MAILAM ENGINEERING COLLEGE


MAILAM 604 304
(Approved by AICTE New Delhi !""ili!#ed #o A$$! %$iver&i#y Che$$!i' !((redi#ed by
N!#io$ bo!rd o" !((redi#!#io$ New Delhi ' TC) Che$$!i*
Dep!r#+e$# o" Co+p,#er Appli(!#io$&
),b- Code ' N!+e. MC/031 ' 2eb 3ro4r!++i$4 )e+. 03
%NIT III
%NIT III 5A6A 7%NDAMENTAL)
Java features Java Platform Java Fundamentals Expressions, Operators, and Control
Structures Classes, Packages and Interfaces Exception Handling
0 M!r8& 9,e&#io$ !$d A$&wer
:- 2h!# i& 5A6A ;
Java is a pure o!"ect oriented programming language, #$ic$ $as derived C s%ntax and C&& o!"ect
oriented programming features Is a compiled and interpreted language and is platform independent and
can do grap$ics, net#orking, multit$reading It #as initiall% called as O'(
0- 2h!# !re #he "o,r (or$er&#o$e& o" OO3 ;
a '!straction ) Can manage complexit% t$roug$ a!straction *ives t$e
complete overvie# of a particular task and t$e details are $andled !% its
derived classes Ex ) Car
! Encapsulation ) +ot$ing !ut data $iding, like t$e varia!les declared
under private of a particular class are accessed onl% in t$at class and cannot
access in an% ot$er t$e class
c In$eritance ) Is t$e process in #$ic$ one o!"ect ac,uires t$e
properties of anot$er o!"ect, ie, derived o!"ect
d Pol%morp$ism ) One met$od different forms, ie, met$od overriding and interfaces are t$e
examples of pol%morp$ism
3- 2h!# i& dow$ (!&#i$4;
-oing a cast from a !ase class to a more specific class .$e cast does not convert t$e o!"ect, "ust asserts it
actuall% is a more specific extended o!"ect
eg -alamatian d / 0-almatian1 a-og2
3ost people #ill stare !lankl% at %ou if %ou use t$e #ord do#ncast Just use cast
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
2
4- 2h!# i& 5!v! I$#erpre#er;
It is Java 4irtual 3ac$ine ie, a "ava program compiles t$e 5nicode to intermediar% code called
as 6%te code #$ic$ is not an executa!le code, t$at is executed !% Java interpreter
1- 2h!# !re p,bli( &#!#i( void +!i$()#ri$4 !r4&<=* !$d )y&#e+-o,#-pri$#l$(* ;
Pu!lic ke%#ord is an access specifier
Static allo#s main01 to !e called #it$out $aving to instantiate a particular instance of class
4oid does not return an% value
main01 is t$e met$od #$ere "ava application !egins
String args78 receives an% command line arguments during runtime
S%stem is a predefined class t$at provides access to t$e s%stem out is
output stream connected to console
println displa%s t$e output
6- 2h!# !re ide$#i"ier& !$d li#er!l&;
Identifiers are t$e varia!les t$at are declared under particular datat%pe 9iterals are t$e values
assigned to t$e Identifiers
>- 2h!# i& #yped l!$4,!4e;
.$is means t$at t$e varia!les are at first must !e declared !% a particular data t%pe #$ereas t$is is
not t$e case #it$ JavaScript
?- )(ope !$d li"e#i+e o" v!ri!ble&;
Scope of varia!les is onl% to t$at particular !lock lifetime #ill !e till t$e !lock ends varia!les
declared a!ove t$e !lock #it$in t$e class are valid to t$at inner !lock also
/- C!&#i$4 I$(o+p!#ible #ype&;
Can !e done eit$er implicitl% and explicitl% ie, int ! / 0int1 c2 #$ere c is float
:0- De(l!r!#io$ o" Arr!y& ;
int a78 / ne# int7:;82 int a7878 / ne# int7<87<82
::- 2h!# doe& )#ri$4 de"i$e ;
String is an arra% of c$aracters, !ut in "ava it defines an o!"ect and t$e varia!le of t%pe string can
!e assigned to anot$er varia!le of t%pe String
:0- 2h!# !re bi#wi&e oper!#or& !$d bi#wi&e lo4i(!l oper!#or& ;
=, >, ?, @, AA, AAA, BB, >/, C/ and =, >, ?
:3- @ow do yo, de"i$e bre!8 !$d L!bel;
De can declare as !reak Bla!el name A and t$e la!el is declared as Bla!el nameA ) E F G2
.$e !reak statement can !e declared an%#$ere in t$e program ie eit$er inside t$e la!el or outside t$e
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
3
la!el
:4- De"i$e (l!&& ;
' class is a one #$ic$ defines ne# datat%pe, and is template of an o!"ect, and is a protot%pe
:1- De"i$e #hi& "i$!liAe !$d "i$!l( "or v!ri!ble& +e#hod& (l!&&e& * ;
.$is is used inside an% met$od to refer to t$e current o!"ect 'nd is mostl% avoided FinaliHe
met$od is used to perform t$e actions specified in t$is met$od "ust !efore an o!"ect is destro%ed
ie "ust !efore gar!age collector process
Final #it$ varia!les is c$anging t$e literals of t$e varia!les ie not$ing !ut const final #it$
met$od means override t$at met$od
Final #it$ class means $ave derived classes of t$at particular class
:6- 2h!# i& p!&&ed by re"ere$(e;
O!"ects are passed !% reference
In "ava #e can create an o!"ect pointing to a particular location ie +599 location !% specif%ing )
Bclass nameA Bo!"ect nameA2 and also can create o!"ect t$at allocates space for t$e varia!les declared in
t$at particular class !% specif%ing ) Bo!"ect name A / ne# Bclass nameA012
:>- EBpl!i$ !bo,# )#!#i(;
D$en a mem!er is declared as static it can !e accessed !efore an% o!"ects of its class are created
and #it$out an% reference to an% o!"ect .$ese are glo!al varia!les2 no cop% of t$ese varia!les can !e
made static can also !e declared for met$ods 'nd cannot refer to t$is or super
:?- 2h!# !re $e&#ed (l!&&e&;
.$ere are t#o t%pes ) static and nonIstatic
Static class means t$e mem!ers in its enclosing class 0class #it$in class1 can !e accessed !%
creating an o!"ect and cannot !e accessed directl% #it$out creating t$e o!"ect
+onIstatic class means inner class and can !e accessed directl% #it$ t$e o!"ect created for t$e
outer class no need to create again an o!"ect like static class
:/- Crie"ly !bo,# &,per (*;
.$is is used to initialiHe constructor of !ase class from t$e derived class and also access t$e
varia!les of !ase class like superi / :;
00- De"i$e +e#hod overlo!di$4-
In Java same met$od name #it$ different arguments
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
4
0:- De"i$e +e#hod overridi$4 -
In Java same met$od name and same num!er of arguments
00-2h!# i& Dy$!+i( Me#hod Di&p!#(h;
.$is is t$e mec$anism !% #$ic$ a call to an overridden function is resolved at runtime rat$er t$an
at compile time 'nd t$is is $o# Java implements runtime pol%morp$ism
03- 2h!# !re !b&#r!(# (l!&&e&;
.o create a super class t$at onl% defines generaliHed form t$at #ill !e s$ared !% all its su!classes,
leaving it to eac$ su!class to fill in t$e details De cannot declare a!stract constructors and a!stract static
met$ods
'n a!stract class contains at least one a!stract met$od
'nd t$is a!stract class is not directl% instantiated #it$ ne# operator
Can create a reference to a!stract class and can !e point to su!class o!"ect
04- 2h!# i& ObDe(# (l!&& !$d D!v!-l!$4 ;
O!"ect class is t$e super class of all t$e classes and means t$at reference varia!le of t%pe o!"ect
can refer to an o!"ect of an% ot$er class and also defines met$ods like finalise,#ait
"avalang contains all t$e !asic language functions and is imported in all t$e programs implicitl%
01- 2h!# !re p!(8!4e& !$d why; how #o eBe(,#e ! pro4r!+ i$ ! p!(8!4e ;
Package is a set of classes, #$ic$ can !e accessed !% t$emselves and cannot !e accessed outside t$e
package and can !e defined as package Bpkg nameA
Package name and t$e director% name must !e t$e same 'nd t$e execution of programs in package is
done !% ) "ava m%packaccount #$ere m%pack is director% name and account is program name
06-2h!# doe& ! D!v! &o,r(e "ile (!$ (o$#!i$ ;
&pe(i"yi$4 package name importing more t$an one pack
0>-De"i$e i$#er"!(e-
Interface is similar to a!stract classes .$is defines onl% met$od declarations and definitions are
specified in t$e classes #$ic$ implements t$ese interfaces Jone interface multiple met$ods J signifies t$e
pol%morp$ism concept Here an interface can extend anot$er interface and a class implements more t$an
one interface
0>-@ow EB(ep#io$ h!$dli$4 i& do$e i$ 5!v!;
Is managed via K ke%#ords )
tr% ) statements t$at %ou #ant to monitor t$e exceptions contain in tr% !lock catc$ ) t$e
exception t$ro#n !% tr% is catc$ed !% t$is
t$ro# ) to manuall% t$ro# exception #e go for t$is
t$ro#s ) Exception t$at is t$ro#n out of a met$od must !e specified !% t$ro#s after t$e met$od
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
5
declaration
finall% ) t$is !lock is executed #$et$er or not an exception is t$ro#n and also it is executed "ust
!efore t$e met$od returns and t$is is optional !lock
0>- 2h!# !re (he(8ed !$d ,$(he(8ed EB(ep#io$& ;
5nc$ecked Exceptions are t$ose #$ic$ are not included in t$ro#s list and are derived from
LuntimeException #$ic$ are automaticall% availa!le and are in "avalang
C$ecked Exceptions are t$ose #$ic$ cannot $andle !% it
30-2h!# i& Thre!d !$d +,l#i#hre!d; @ow i& #hre!d (re!#ed;
.$read is a small unit of dispatc$a!le code ie, a single program can perform more t$an one task
simultaneousl% .$is is a lig$t #eig$t process and is of lo# cost
3ulti.$reading #rites more efficient programs and make maximum use of CP5 ie, it minimiHes
its idle time
.$read is created in t#o #a%s )
6% implementing t$e runna!le interface
6% extending t$e .$read class itself

First #a% is used to update onl% run01 met$od ie #e can use onl% run01 met$od in t$is class Second
#a% is used to define several met$ods overridden !% derived class
*enerall% #e override onl% run01 met$od so #e go for runna!le interface
0?- 2h!# !re #he v!rio,& &#!#e& !$d +e#hod& o" #hre!d ;
States ) Lunning, Lead% to run, Suspended, Lesumed, !locked and terminated 3et$ods ) get+ame,
getPriorit%, is'live, "oin, run, sleep, start
.$read priorities are 3I+MPLIOLI.N, 3'OMPLIOLI.N, +OL3MPLIOLI.N
0/- 2h!# i& i$#erpro(e&& &y$(hro$iA!#io$ ; ( !l&o (!lled Mo$i#or )e+!phore *
a Consider a small !ox namel% 3O+I.OL t$at can $old onl% one t$read Once a t$read
enters a monitor, all ot$er t$reads must #ait until t$at t$read exits t$e monitor
! In t$is #a%, a monitor can !e used to protect a s$ared asset from !eing manipulated !%
more t$an one t$read at a time
c Once a t$read is inside a s%nc$roniHed met$od, no ot$er t$read can call an% ot$er
s%nc$roniHed met$od on t$e same o!"ect
30- 2h!# i& )#re!+ ;
a It is an '!straction t$at eit$er produces or consumes information
! .#o t%pes ) 6%te Stream and C$aracter Stream
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
6
3:- @ow #o e$#er d!#! i$ D!v!;
a First specif% )
6ufferedLeader !r / ne# 6ufferedLeader0ne# InputStreamLeader0S%stemin112
t$en sa% !rread01, and t$is is put in do #$ile loop to receive as man% as #e re,uire
30- 2h!# i& ! )#ri$4To8e$iAer ;
String .okeniHer provide parsing process in #$ic$ it identifies t$e delimiters provided !% t$e user , !%
default delimiters are spaces, ta!, ne#line etc and separates t$em from t$e tokens .okens are t$ose
#$ic$ are separated !% delimiters
33- 2h!# i& )eri!liA!#io$ ;
.$e process of #riting t$e state of an o!"ect to a !%te stream and can restore t$ese o!"ects !% using
deserialiHation
Is also need to implement L3I, #$ic$ allo#s a "ava o!"ect of one mac$ine to invoke "ava o!"ect of
anot$er mac$ine
ie, t$e o!"ect is passed as an argument !% serialiHing it and t$e receiving mac$ine deserialiHes it
34- I$#er"!(e& i$(l,de by D!v!-l!$4 ;
Javalang is t$e package of all t$e classes 'nd is automaticall% imported into all Java programs
31- 2h!# i& ,&er de"i$ed eB(ep#io$;
.o $andle situations specific to our applications #e go for 5ser -efined Exceptions 'nd are defined
!% 5ser using t$ro# ke%#ord
36- Li&# o,# #he "e!#,re& o" 5!v!;
Simple, 'rc$itectureIneutral, O!"ectIoriented, Porta!le, -istri!uted, Hig$Iperformance,
Interpreted, 3ultit$readed, Lo!ust, -%namic, Secure
3>- 2h!# i& by#e (ode;
Java compiler translates a Java program into an intermediate language called Java bytecodesIIt$e
platformIindependent codes interpreted !% t$e Java interpreter
3?- 2h!# !re #he v!rio,& !ppli(!#io$& o" 5!v!
'pplets, +et#orking, InternationaliHation , Securit%, O!"ect serialiHation, Java -ata!ase
Connectivit% 0J-6C1
3/- 2hy do D!v! i& (!lled pl!#"or+ i$depe$de$#;
Java is called platform independent language !ecause :;;P Pure Java programs are compiled
into mac$ineIindependent !%tecodes, t$e% run consistentl% on an% Java platform
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.

43- 2h!# i& br!$(hi$4;


D$en a program !reaks t$e se,uential flo# and "umps to anot$er part of t$e code ,it is called
!ranc$ing
44- 2h!# i& (o$di#io$!l !$d ,$(o$di#io$!l br!$(hi$4;
D$en t$e !ranc$ing is !ased on a particular condition, it is kno#n as conditional !ranc$ing If
!ranc$ing takes place #it$out an% decision, it is kno#n as unconditional !ranc$ing
41-2h!# i& #he ,&e o" &wi#(h &#!#e+e$#;
.$e s#itc$ statement tests t$e value of a given varia!le against a list of case values and #$en a
matc$ is found, a !lock of statements associated #it$ t$at case is executed
46- 2ri#e dow$ #he (o$#rol "low &#!#e+e$# !v!il!ble i$ D!v!;
D$ile
.$e general s%ntax of t$e #$ile statement is)
#$ile 0expression1
E
statement
G
doEwhile
.$e general s%ntax of t$e doI#$ile is)
do E
statement0s1
G #$ile 0expression12
For
.$e general form of t$e for statement can !e expressed like t$is)
for 0initialization2 termination2 increment1
E
statement
G
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
!
4>- 2h!# !re #he de(i&io$E+!8i$4 &#!#e+e$#& !v!il!ble i$ D!v!;
The if/else Statements
if 0response // O(1
E
QQ code to perform OK action
G
else
E
QQ code to perform Cancel action
G

.$e s#itc$ Statement
s#itc$0expression1
E
case value: ) statements2 !reak2
case value < )
statements2 !reak2
case value R )
statements2 !reak2
default )
G
4?- 2h!# i& #he ,&e o" #he ;. oper!#or;
It is used for making t#oI#a% decisions,it is a com!ination of S and ) and takes t$ree
operands .$is operator is kno#n as ternar% operator
.$e general form is given as
Conditional expression S expression: ) expression<
.$e conditional operator is evaluated firstIf t$e result is true,expression: is evaluated, ot$er#ise
t$e expression< is evaluated
4/- 2h!# i& ! (l!&&;
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
"
' class is a !lueprint, or protot%pe, t$at defines t$e varia!les and t$e met$ods common to all
o!"ects of a certain kind
10- 2h!# i& ! obDe(#;
'n o!"ect is a soft#are !undle of varia!les and related met$ods'n instance of a class depicting
t$e state and !e$avior at t$at particular time in real #orld
1:- 2h!# i& ! +e#hod;
Encapsulation of a functionalit% #$ic$ can !e called to perform specific tasks
10- 2h!# i& e$(!p&,l!#io$; EBpl!i$ wi#h !$ eB!+ple-
Encapsulation is t$e term given to the process of hiding the implementation details of the object
Once an o!"ect is encapsulated, its implementation details are not immediatel% accessi!le an%
more Instead t$e% are packaged and are onl% indirectl% accessi!le via t$e interface of t$e o!"ect
13- 2h!# i& i$heri#!$(e; EBpl!i$ wi#h !$ eB!+ple-
In$eritance in o!"ect oriented programming means t$at a class of o!"ects can in$erit properties
and met$ods from anot$er class of o!"ects
14- 2h!# i& poly+orphi&+; EBpl!i$ wi#h !$ eB!+ple-
In o!"ectIoriented programming, pol%morp$ism refers to a programming languageTUs ability to
process objects differently depending on their data type or class 3ore specificall%, it is t$e
a!ilit% to redefine met$ods for derived classes
For example, given a !ase class s$ape, pol%morp$ism ena!les t$e programmer to define different
area met$ods for an% num!er of derived classes, suc$ as circles, rectangles and triangles
+o matter #$at s$ape an o!"ect is, appl%ing t$e area met$od to it #ill return t$e correct results
Pol%morp$ism is considered to !e a re,uirement of an% true o!"ectIoriented programming
language
11- I& +,l#iple i$heri#!$(e !llowed i$ 5!v!;
+o, multiple in$eritance is not allo#ed in Java
16-2h!# i& 56M;
.$e Java interpreter along #it$ t$e runtime environment re,uired to run t$e Java application in
called as Java virtual mac$ine0J431
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
1#
1>- 2h!# !re #he di""ere$# #ype& o" +odi"ier&;
.$ere are access modifiers and t$ere are ot$er identifiers 'ccess modifiers are p,bli( pro#e(#ed
and priv!#e Ot$er are "i$!l and &#!#ic
1?- 2h!# !re #he !((e&& +odi"ier& i$ 5!v!;
.$ere are R access modifiers 3,bli( pro#e(#ed and priv!#e, and t$e default one if no identifier
is specified is called friendl%, !ut programmer cannot specif% t$e friendl% identifier explicitl%
1/- 2h!# i& ! wr!pper (l!&&;
.$e% are classes t$at #rap a primitive data t%pe so it can !e used as a o!"ect
60- 2h!# i& ! &#!#i( v!ri!ble !$d &#!#i( +e#hod; 2h!#FG& #he di""ere$(e be#wee$ #wo;
.$e modifier static can !e used #it$ a varia!le and met$od D$en declared as static varia!le,
t$ere is onl% one varia!le no matter $o# instances are created, t$is varia!le is initialiHed #$en
t$e class is loaded Static met$od do not need a class to !e instantiated to !e called, also a nonI
static met$od cannot !e called from static met$od
6:- 2h!# i& 4!rb!4e (olle(#io$;
*ar!age Collection is a t$read t$at runs to reclaim t$e memor% !% destro%ing t$e o!"ects t$at
cannot !e referenced an%more
60- 2h!# i& !b&#r!(# (l!&&;
'!stract class is a class t$at needs to !e extended and its met$ods implemented, a class $as to !e
declared a!stract if it $as one or more a!stract met$ods
63- 2h!# i& +e!$# by "i$!l (l!&& +e#hod& !$d v!ri!ble&;
.$is modifier can !e applied to class, met$od and varia!le D$en declared as final class t$e class
cannot !e extended D$en declared as final varia!le, its value cannot !e c$anged if is primitive
value, if it is a reference to t$e o!"ect it #ill al#a%s refer to t$e same o!"ect, internal attri!utes of
t$e o!"ect can !e c$anged
64- 2h!# i& i$#er"!(e;
Interface is a contact t$at can !e implemented !% a class , it $as met$od t$at need
implementation
61- 2h!# i& +e#hod overlo!di$4;
Overloading is declaring multiple met$ods #it$ t$e same name, !ut #it$ different argument list
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
11
66- 2h!# i& &i$4le#o$ (l!&&;
Singleton class means t$at an% given time onl% one instance of t$e class is present, in one J43
6>- 2h!# i& #he di""ere$(e be#wee$ !$ !rr!y !$d ! ve(#or;
+um!er of elements in an arra% are fixed at t$e construction time, #$ereas t$e num!er of
elements in vector can gro# d%namicall%
6?-2h!# i& ! (o$&#r,(#or;
In Java, t$e class designer can guarantee initialiHation of ever% o!"ect !% providing a special
met$od called a constructor If a class $as a constructor, Java automaticall% calls t$at constructor
#$en an o!"ect is created, !efore users can even get t$eir $ands on it So initialiHation is
guaranteed
6/-2h!# i& (!&#i$4;
Conversion of one t%pe of data to anot$er #$en appropriate Casting makes explicitl% converting
of data
>0-2h!# i& +e!$# by !b&#r!(#io$;
'!straction defines t$e essential c$aracteristics of an o!"ect t$at distinguis$ it from all ot$er
kinds of o!"ects '!straction provides crispl%Idefined conceptual !oundaries relative to t$e
perspective of t$e vie#er Its t$e process of focussing on t$e essential c$aracteristics of an
o!"ect '!straction is one of t$e fundamental elements of t$e o!"ect model
>:-2h!# i& +e!$# by E$(!p&,l!#io$;
Encapsulation is t$e process of compartmentalising t$e elements of an a!traction t$at defines
t$e structure and !e$aviour Encapsulation $elps to separate t$e contractual interface of an
a!straction and implementation
>0-2h!# i& +e!$# by I$heri#!$(e;
In$eritance is a relations$ip among classes, #$erein one class s$ares t$e structure or
!e$aviour defined in anot$er class .$is is called Single In$eritance If a class s$ares t$e
structure or !e$aviour from multiple classes, t$en it is called 3ultiple In$eritance In$eritance
defines TVisIaTV $ierarc$% among classes in #$ic$ one su!class in$erits from one or more
generalised superclasses
>3-2h!# i& +e!$# by 3oly+orphi&+;
Pol%morp$ism literall% means taking more t$an one form Pol%morp$ism is a c$aracteristic of
!eing a!le to assign a different !e$avior or value in a su!class, to somet$ing t$at #as declared in
a parent class
>4-2h!# i& !$ Ab&#r!(# Cl!&&;
'!stract class is a class t$at $as no instances 'n a!stract class is #ritten #it$ t$e expectation
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
12
t$at its concrete su!classes #ill add to its structure and !e$aviour, t%picall% !% implementing its
a!stract operations
>1- 2h!# i& !$ I$#er"!(e;
Interface is an outside vie# of a class or o!"ect #$ic$ emp$aiHes its a!straction #$ile $iding
its structure and secrets of its !e$aviour
:6 M!r8 9,e&#io$&
:-2ri#e dow$ #he "e!#,re& o" D!v!;
Compiled and interpreted
PlatformIindependent and porta!le
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
13
O!"ectIOriented
Lo!ust and secure
-istri!uted
Simple, small and familar
3ultit$readed and interactive
Hig$ performance
-%namic and extensi!le
0- EBpl!i$ #he &#r,(#,re o" ! #ypi(!l D!v! pro4r!+;
-ocumentation section
Package statement
Import statement
Interface Statements
Class definitions
3ain 3et$od Class
E
3ain 3et$od -efinition
G
3- 2ri#e !$ eB!+ple D!v! pro4r!+ #o ill,&#r!#e +e#hod overlo!di$4;
class met$od Evoid
area0int r1 Eint c2
c/R:WXrXr2
S%stemoutprintln 0V.$e area of circle is V&c12G void
area0float r1
E
float c2 c/R:WXrXr2
S%stemoutprintln 0V.$e area of circle is V&c12G
G
class met$odoverloading
Epu!lic static void main0String args781 Emet$od
m/ne# met$od012
int a2 float x2
S%stemoutprintln0VEnter t$e radius of t$e circleV12
a/IntegerparseInt0args7;812
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
14
x/FloatvalueOf0args7R8float4alue012
marea0a12 marea0x12G
G
4- 2h!# do yo, +e!$ by 4!rb!4e (olle(#io$;
-eallocation of memor% allocated d%namicall% for an o!"ect is done automaticall% in
"ava .$e tec$ni,ue t$at accomplis$es t$is is called gar!age collection D$en no reference to an
o!"ect exists, t$at o!"ect is assumed to !e no longer needed, and t$e memor% occupied !% t$e
o!"ect can !e reclaimed .$ere is no explicit need to destro% o!"ects in "ava .$e gar!age
collection occurs sporadicall% during t$e execution of %our program
1- 2hy do we $eed "i$!liAe(* +e#hod;
FinaliHation is a mec$anism provided !% "ava to allo# an o!"ect to perform some action
#$en it is destro%ed
For eg if an o!"ect is $olding some nonI"ava resource suc$ as a file $andle or #indo#
c$aracter font, t$en %ou mig$t #ant to make sure t$ese resources are freed !efore a o!"ect is
destro%ed 6% using finaliHation, #e can define specific actions t$at #ill occur #$en an o!"ect is
"ust a!out to !e reclaimed !% t$e gar!age collector
.o add a finaliHer to a class, simpl% define t$e finaliHe01 met$od .$e general form is)
Protected void finaliHe01
E
QQfinaliHation code $ere
G
6-De"i$e +,l#i#hre!ded pro4r!++i$4; @ow +,l#i#hre!di$4 i& i+ple+e$#ed i$ D!v!;
JavaYs multit$reading s%stem is !uilt upon t$e .$read class, its met$ods, and its
companion interface, Lunna!le .$read encapsulates a t$read of execution Since %ou
canYt directl% refer to t$e et$ereal state of a running t$read, %ou #ill deal #it$ it
t$roug$ its prox% .o create a ne# t$read, %our program #ill eit$er extend .$read or
implement t$e Lunna!le interface
' multit$readed programming contains t#o or more parts t$at can run concurrentl% Eac$ part of
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
15
suc$ a program is called a t$read, and eac$ t$read defines a separate pat$ of execution
3ultit$reading is a specialiHed form of multitasking 3ultit$reading ena!les us to #rite ver%
efficient programs t$at make maximum use of t$e CP5, !ecause idle time can !e kept to a
minimum
3ain t$read is t$e one t$at is executed #$en %our program !egins .$e main t$read is important
for t#o reasons)
It is t$e t$read from #$ic$ ot$er Jc$ildZ t$reads #ill !e spa#ned
Often it must !e t$e last t$read to finis$ execution !ecause it performs various s$utdo#n
actions
.$read can !e created !% instantiating an o!"ect of t%pe .$read Java defines t#o #a%s in #$ic$
t$is can !e accomplis$ed)
I+ple+e$# #he R,$$!ble i$#er"!(e
.o implement Lunna!le, a class need onl% to implement a single met$od called run01
EB#e$d #he Thre!d (l!&&-
.$e extending class must override t$e run01 met$od, #$ic$ is t$e entr% point for t$e ne# t$read
It must also call start01 to !egin execution of t$e ne# t$read
>- De"i$e )y$(hro$iA!#io$- @ow i# i& i+ple+e$#ed i$ D!v!;
S%nc$roniHation is t$e process used to ensure t$at t$e resource #ill !e used !%
onl% one t$read at a time, #$en t#o or more t$reads need access to a s$ared
resource
S%nc$roniHation is implemented in "ava !% using t$e concept of monitor also called as a
semap$ore ' monitor is an o!"ect t$at is used as a mutuall% exclusive lock, or mutex Onl% one
t$read can o#n a monitor at a given time
D$en a t$read ac,uires a lock, it is said to $ave entered t$e monitorall t$e ot$er t$reads
attempting to enter t$e locked monitor #ill !e suspended until t$e first t$read exists t$e monitor
.$ese ot$er t$reads are said to !e #aiting for t$e monitor
?- 2h!# i& #he di""ere$(e be#wee$ "i$!l "i$!lly !$d "i$!liAe;
.$e modifier final is used on class varia!le and met$ods to specif% certain !e$aviors
explained a!ove
'nd finall% is used as one of t$e loop in t$e tr% catc$ !locks, It is used to $old code t$at
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.
16
needs to !e executed #$et$er or not t$e exception occurs in t$e tr% catc$ !lock
Java provides a met$od called finaliHe0 1 t$at can !e defined in t$e class D$en t$e
gar!age collector is read% to release t$e storage ed for %our o!"ect, it #ill first call finaliHe0 1, and
onl% on t$e next gar!ageIcollection pass #ill it reclaim t$e o!"ects memor%
So finaliHe0 1, gives %ou t$e a!ilit% to perform some important cleanup at t$e time of
gar!age collection
Prepared By
Ms. A.Subathra Devi
MCA / Asst. Prof.

You might also like