You are on page 1of 129

1

Java SE: -Java Standard Edition


Core java
Java SE
Adv java
It deals with developing stand alone applications & also basic programs for the
network
Java EE: - Java Enterprise Edition
It !eals with developing b"siness sol"tions on a network
Java #E: - Java #icro Edition $or% Java #obile Edition
It deals with developing embedded s&stems & wireless applications
'(#): - '&per (e*t #ark"p )ang"age
+IC: - +etwork Interface Card Card means Electronic Circ"its
A network is an interconnection of Comp"ter
A gro"p of programs is called Software
,itho"t Software- 'ardware did not work
. 'ardware
/ Software digital signals
0 1rotocol
. & 2 called bits & also called
3inar& digits
. 1ower s"ppl&
2 no power s"ppl&
1rotocol is a specification of r"les to be followed b& ever& comp"ter on the network
,hen to send 4 receive data
1rotocol contain / things
'ow to send 4 receive data
,hat is 'and Shaking mechanism5
A% (wo comp"ters establishing a connection- comm"nicating with each other &
finall& disconnecting from each other is called hand shaking mechanism
1acket: - A packet represents a gro"p of b&tes of data Envelop is called frame
,hat is I1 Address5
A% I1 Address is a "ni6"e I! n"mber given to ever& comp"ter in the network
2
I! address will have 7 n"mbers separated b& dot $% s&mbol
E8:-
.9/::.222.
b b b b 7 b&tes
(C14I1: - (ransition control protocol 4 Internet 1rotocol
Client: - A client is a machine that sends a re6"est for some service
Server: - A Server is a machine that provides services to the clients A network is
also called client server architect"re
!;!:-!epartment ;f !efense
A<1A+E(: - Advance <esearch 1roject Agenc& +etwork (his is the first network
in the network
(he& connected slowl&
,hat is the advantage of +etwork5
A% <eso"rce sharing is main advantage of network
(his means Sharing memor&
Sharing processor
Sharing data
Sharing software
Sharing hardware
!"mm& terminals means witho"t C1=
A(&(:-Advance (elephone (echnolog&
Internet: - Internet is a networking of comp"ters e*isting in the world
It is global network of networks on the Earth
Intranet: - Intranet is a network of all the comp"ters e*isting on the earth It is a
global network of networks It is a network of comp"ter with in an organi>ation
E*tranet: - It is a network o"t side the organi>ation that connects it with the
network of some other organi>ation
3
E*tranet
IS1: - Internet Service 1rovider
It is a person or compan& who provides connection in to the internet
(he first IS1 in India is ?S+)
@ !ial-"p
Connection
,eb browser
:2 A31S

,eb server
)ased line
:2 Aiga bits for second- it is a bond width !ial-"p connection is a connection
into internet thro"gh an IS1 !isadvantage dial-"p connection is slow $low speed%
(here is no sec"rit& of data on internet 3eca"se IS1 will give speed B7 kbps )ased
line is a direct connection into internet witho"t the help of IS1 Complete sec"rit&
for data for lased line connection It is a high speed connection
,eb Server: - It is software that sho"ld be installed on an internet server 1op"lar
,eb servers are $IIS% Internet Information Server- ,eb logic- ,eb Sphere- Apache-
(omcat- J boss etc
'((1 is the most avidl& "sed on protocol
,eb 3rowsers:- .% Internet E*plorer- /% +etscape +avigation- 0% Cire Co*-
7%;pera EtcD
4
Interne
t
C Compiler is translate in to machine lang"age generates e6"ivalent machine
lang"age code
;bject code: - e6"ivalence machine lang"age code of a EcF program
C 1latform: - #icro processor
Celeron processor

;bject Code
8obj contains object code & contains machine lang"age code 'eader code is
incl"ded e*e 'eader files contains f"nctions is a code of f"nction 8e*e file has a
header file code- b"t 8obj does not contain header file code
7227 is the first micro processor in the world $G22G- G2GB $./2 instr"ctions%-
G2/GB- G20GB- G27GB- penti"m.- penti"m/- pentiem0- penti"m7% 1enti"m0 processor
is containing //2 instr"ctions Ever& #icro processor "nderstandable to that own
instr"ctions #icro processor is different instr"ction of other instr"ctions
(ranslator: - (ranslator is a program (hat converts the comp"ter program in to
machine lang"age (here are 0 t&pes
.% Interpreter- /% Compiler- 0% Assembler
Interpreter converts onl& one line at a time into machine lang"age Compiler
converts all lines at a time in to machine lang"age Compiler is at least .2 times
faster than interpreter
Assembler is converts assembl& lang"age into machine lang"age
C- CHH programs are called s&stem dependent programs- beca"se the& can be
e*ec"ted onl& on that comp"ter a s&stem where the& have been developed
C- CHH- 1ascal- C;<(<A+ are not "sed on internet Internet is an open network or
a p"blic network
Sphere
Java 1latform: - 1rocessor
#icro
1rocessor
Celeron
1rocessor
J?#: - Java ?irt"al #achine It is a program It converts b&te code instr"ctions in
to machine lang"age instr"ctions It is "nderstandable to the present processor
J?# check what processor &o" are "sing
5
8c 8obj 8e*e
1
C
8java 8class
J?#
J?#
J?#
S
1
C
3&te code is a s&stem independent J?# is a s&stem dependent J?# is
developed in EcF lang"age
Java is s&stem dependent 3eca"se java programs donFt depend on the
comp"ter s&stem where the& have been developed Java writes once and r"n an&
ware It is s"itable for internet Internet doesnFt contain the sec"rit& for the data
,hat is the difference between e*e & class file5
A% e*e file contains machine lang"age instr"ctions which are e*ec"ted b& the micro
processor e*e files are s&stem depended
class files b&te code instr"ctions e*ec"ted b& the J?# class files are s&stem
independent
Sec"rit& problems for data on internet: -
. Eaves dropping: - <eading others data illegall&
/ (ampering: - +ot onl& reading others data b"t also modif&ing
0 Impersonation: - A person is acting as another person on internet
7 ?ir"s: - ?ir"s is a comp"ter program that harms the data software and
hardware of a comp"ter s&stem ?ir"s will spread thro"gh e*e- com- doc-
jpg and a"dio video files
(e*t file can not spread vir"s
,ho developed the first vir"s in the world5
A% A% America 3% Afghanistan C% India !% 1akistan
Sol"tions: -
. Eaves dropping: - Encr&ption 4 !escription
Converting data into "nreadable format
Converting back into original format
=sing some password or ke&
/ (ampering: - ---- DO ----
0 Impersonation: - !igital signat"re is a file that contains personal I! information
7 ?ir"s: - class files can not carr& vir"s
In .992 --- S"n #icro s&stems I+C- =S James Aa">ing
In .990 --- ;ak I It is completel& s&stem Independent
)ater on ;ak name has changed Its name is JA?A
(he S&mbol of java is (ea c"p
Ceat"res of java: -
. Simple: - Java is a simple programming lang"age )earning & practicing
java is eas& beca"se of its resemblance C and CHH pointers are not available
on JA?A
,h& pointers are not available on java5
A% . 1ointers lead to conf"sion for a programmer
/ 1ointers easil& crash a program
6
0 =sing pointers vir"s & hacking programs can be written
/ ;bject ;riented: - Java is a p"rel& object-oriented programming lang"age
Java programs "se objects & classes
;bject: - ;bject is an&thing e*ists reall& in the world An object contains
properties and performs actions 1roperties are represented b& variables &
actions are formed b& f"nctions C"nctions are called methods in java
Class: - A class is a gro"p of name that represents properties & actions of
objects Class does not e*ist ph&sicall& ,here as object e*ist ph&sicall& A class
is a plan or model to create objects- from the class to create the objects In ever&
java program we m"st contain at least one class
0 !istrib"ted: - Information is distrib"ted on vario"s on a network =sing
java- we can write programs which capt"re information & distrib"te it to
clients
7 <ob"st: - <ob"st means strong Java programs are strong Java programs
will not crash easil& beca"se of its e*ec"tion handling and its memor&
management feat"res In C- CHH less or more than memor& the programs are
crash In java this problem is not occ"rred- beca"se J?# is allotted the
memor&
#emor& allocation: - J?#FS class loader s"b s&stem
#emor& management: - J?#FS class garbage collection- this collector
removes the "n"sed memor& E*ception means r"n time error ,e can not
handling the e*ception in ECF In case java we can handle smoothl&
: Sec"re: - Java enables the constr"ctions of vario"s free and tamper-free
s&stems
B Architect"re +e"tral: - JavaFs b&te code isnFt machine dependent It can be
r"n an& machine with an& processor & with an& operating s&stem
J 1ortable: - Java programs give same res"lts on all machines Ever&thing is
clearl& defined in java specification & nothing is left to o 4 s If the 1rogram
is eliding give same res"lts are called portabilit&
G Interpreted: - Java programs are compiled to generate the b&te code (his
b&te code can be downloaded & interpreted b& the interpreter in J?# In
java we "se interpreter (his interpreter is slow thatFs wh& problem is
occ"rred
9 'igh performance: - Along with interpreter- there will be JI( $j"st in time%
compiler which enhance the speed of e*ec"tion
7
Interpreter
JI(
Compiler
S class
J?#
Java Compiler
.2 'igh performance: - Along with interpreter- there will be JI( $j"st in time%
compiler which enhance the speed of e*ec"tion
.. #"lti (hreaded: - A thread is a process or e*ec"tion ,e can create different
processing in java called EthreadsF (his as an essential feat"re to design
server side programs
./ !&namic: - ,e can develop programs in java which d&namicall& interact
with the "ser on internet
E*: - Applets
J?# Architect"re: - $3lack diagram of J?#%
Class files: - #eans class files It contains b&te code
Class loader s"b s&stem: - (he class loader s"b s&stem does following tasks
.% It lodes class files into memor&
/% It verifies the b&te code instr"ctions
0% (hen it will be a lot necessar& memor& for the java program
(his memor& is divided into : parts or blocks are called r"n time data areas
(he& are
1) #ethod area: - Class code & method code is stored on method area
2) 'eap: - ('eap% ;bjects are created on heap memor& (he separated memor&
is allotted in heap
3) Java stacks: - (hese are the areas where java methods are e*ec"ted Each
java stack is divided into several frames ;n each frame separate method is
e*ec"ted
4) 1C registers: - 1C means program co"nter In 1C registers store memor&
address
Class files
8
Class
)oader s"b
s&stem
+ative
#ethod
)ibraries
5) +ative method stacks: - (hese are the memor& areas where native methods
E8: - C and C11 f"nctions are e*ec"ted J?# e*ec"ted C and C11 programs
are also e*ec"ted
+ote: - (o e*ec"te native methods libraries $C- C11 header files% are re6"ired (hese
headers files are linked with J?# b& a program called native method interface
E*ec"tion engine: - It contains interpreter & JI( compiler (his converts the
b&te code instr"ctions in to machine lang"age instr"ctions- so that the processor
can e*ec"te them
,hat is 'ot Spot5
A% (he block of code let to JI( compiler for the e*ec"tion is called hot spot
wwwhotspotcom is developed b& J?#
Comments: -Comments represents description of the feat"res of a program
comments increase readabilit& of a program (here are 0 t&pes of comments in java
1) Single line comments: - (hese comments are started with 44-----
E8: - 44 this is comment line
2) #"lti line comments: - (he& start with 4K and end with K4
E8: - 4K this is comment lineK4
9
<"n time data areas
#ethod
area 'eap
1C
registers
+ative
method
areas
Java
sockets
+ative method
Interface
E*ec"tion
engine
3) Java doc"mentation comments: - (hese comments are start with 4KK and end
with K4
(hese comments are "sef"l to create a '(#) file called A1I $application
programming Interface% doc"ment
,hat is A1I doc"ment5
A% A1I doc"ment is a '(#) file that contains description of all the feat"res of
software- a prod"ct or a technolog& It works has a heap file
A1I doc"ment is like "ser doc"mentation (o create A1I doc we need java doc
compiler C"nction code is available in header file
Cirst program: -
4Kthis is m& first java program
(o displa& a message
A"thor: S=!'EE< <E!!L
?ersion: ?.2
1roject: 1;('=<AI K4
class 'ello
M
p"blic static void main$String argsNO%
M
44 prints the string P'ello S=!'EE< <E!!LP on screen
S&stemo"tprintln$P'ello S=!'EE< <E!!LP%Q
R
R
!:SpsrSCore javaTjavac 'ellojava
!:SpsrSCore javaTjava 'ello
'ello S=!'EE< <E!!L
java librar&

package

class4interface

methods
Interface is similar to a class
javalang$package%

Strings&stem$class%
import javalangStringQ
import javalangS&stemQ
Star $K% means all classes and interfaces
10
import javalangKQ
,hat is the difference between Uincl"de and import5
A% Uincl"de makes a C or CHH compiler to go to standard librar& and cop& the
entire header file code in to a C4CHH programs So the program si>e increases
"nnecessaril& wasting memor& & processor time
Import statement makes the J?# to go to the java librar&- e*ec"te the code &
s"bstit"te the res"lt in to the java program (hatFs J?# doesnFt cop& an& code So
import is more efficient than Uincl"de
,hat is J<E5
A% J<E means java r"n time environment
J<EVJ?# H java librar&
J<E is available in J!W.: It represents entire software
p"blic static void main$String argsNO%
J?# starts e*ec"tion of a java program from main method #ain method is
standing of the J?# (he main method is not- the e*ec"tion will be stopped
A method can receive data from o"tside
A method can also ret"rn some res"lt
N O A gro"p of elements ,e can store several strings in to args *
N O called arra&
?oid means no val"e #ain method does not ret"rn an& val"e
,hat are command line arg"ments5
A% (he val"es passed to main method at the time of r"nning a program or called
command line arg"ments Its stores in arra&
Calling method in java:-
. Create on object to the class
Classname objVnew Classname$ %Q
/ =sing object name method name called the method
;bjmethod$ %Q
Static: - A static method is a method that can be called can witho"t "sing an& object
1"blic: - 1"blic members are available to o"t side programs
S&stemo"t It print stream class
S&stem is a class ;"t $field% is a variable
?ariable represent the memor& location to store the data
1rint method displa&s the res"lts and then keeps the c"rsor in the same line
println method also displa& the res"lt After displa&ing the res"lt it throws the
c"rsor to the ne*t line
Comments are not e*ec"table
11
44 calc"late s"m of / noXs
class S"m
M
p"blic static void main$StringNO args%
M
44 vars
int *-&Q
44 store integer val"es
*V.2Q
&V/:Q
44 find s"m and store res"lt in >
int >V*H&Q
44 displa& the res"lt
S&stemo"tprintln$PS"m of two noXs V PH>%Q
R
R
Save: - YS"mjavaZ
!o"ble 6"otation means save as e*actl& in java file
!:SpsrSCore javaTjavac S"mjava
!:SpsrSCore javaTjava S"m
S"m of two noXs V 0:
Sn is an escape se6"ence (he change the format of the o"t p"t
3acks lace code S
Escape se6"ence #eaning
Sn ne*t line
St [tabT
Sv [enterT
Sb [back spaceT
SS S
SZ Y
SF E
Ever& java program ever& statement m"st end with a semicolon $ Q %
Java is a case sensitive lang"age
+aming conventions in java $<"les%: -
.% 1ackage names in java are written in all small letters
E8: - javaawtQ
javaioQ
java*swingQ
/% Each word of a class name and interface name start with a capital letter
E8: - String- !ataInp"tStream- Action)istener
0% #ethod names starts with a small letter- then each word starts with a capital
letter
12
E8: - read)ine$ %Q - get+"mberInstance$ %Q
7% ?ariable names also follow the above r"les
E8: - age- emp+ame- emplo&ee\+et\SalQ
:% Constants sho"ld be written "sing all capital letters
E8: - 1I- #A8-?A)=E- Cont3;)!Q
'ere Cont is a class- 3;)! is a constant
B% All ke& words sho"ld be written in all small letters
E8: - p"blic- static- void- main etcD
javalang package b& defa"lt import in to ever& java program
Important packages of core java: -
.% javalong: - (his package got primar& classes and interfaces essential for
java program It consists of wrapper classes- strings- threads- etc wrapper
classes are "sef"l to create an object (hread means process
/% java"til: - $"tilit&% in this "til data str"ct"res are available (his package
contains "sef"l classes and interfaces like stack- linked list- hash table-
arra&s etc
0% javaio: - (his package handles files and inp"t o"tp"t related tasks $io I
inp"t o"tp"t%
7% javaawt : - abstract window tool kit
(his package helps to develop A=I It consists of important s"b packages
Javaawtevent event is "sef"l to providing actions
A=I graphics "ser interface
:% java*swing: - (his package helps to develop A=I like javaawt 8 means
e*tended (his package is developed from another package
B% javanet: - Client I server programming can be done "sing this package +et
stands for network
J% javaapplet: - applets are programs which come from a server into a client
and get e*ec"ted on the client machine An applet is a program that comes
to the internet server
G% javas6l: - str"ct"red 6"er& lang"age (his package helps to connect to
database like oracle and "tili>e them in java A data base is a storage data
A variable is memor& location to store data Int represent &o" can store
integer n"mbers +"mbers are witho"t decimal points It is a data t&pe
A data t&pe represents the t&pe of data stored into variable
!ata t&pes and literals:-
A data t&pe represents the t&pe of data stored in variable
Integer data t&pes: -
!ata t&pe #emor& si>e min and ma* val"e
. b&te . 3&te - ./G to H./J
/ short / 3&tes - 0/JBG to H0/JBJ
0 int 7 3&tes - /.7J7G0B to H/.7J7G0B7J
7 long G 3&tes -9//00J/20BG:7JJ:G2G to
H 9//00J/20BG:7JJ:G2J
(hese data t&pes represent integer n"mbers
Cor e*ample 2- .:222-./:- and -.: etcD
13
E2F is the stored in Hve range So. n"mber is less
E2F comes "nder the n"mbers to add
E*: - b&te rno V.2Q means literal
long * V .:2)Q
A literal represents the val"e directl& stored in to a variable
(his ) is writing the J?# will allot of G b&tes otherwise the J?# will allot /
b&tes
Cloat data t&pes: - Cloat means floating point n"mbers Cloat data t&pe represent
n"mbers with decimal points
E*: - .:- 0.7.:9--//- .222 etcD
Cloat means single precision floating point n"mber !o"ble means do"ble
precision floating point n"mber 1recision means acc"rac&
!ata t&pe #emor& si>e min and ma* val"e
.% float 7 b&tes .7e-7: to H07e0G
/% do"ble G b&tes 7.e-0/7 toH.Ge02G
e means into .2 powers $* .2power%
E*:-
.7e-7:V.7*.2
-7:

Cloat pi V 0.7/fQ
(Data type) (Variable) (Literal))
do"ble distance V .9GeGQ
,hat is the distance between a float and do"ble5
A% Cloat represent "p to J digits acc"ratel& after decimal point
!o"ble represent "p to .: digits acc"ratel& after decimal point
Character data t&pe: -
!ata t&pe #emories si>e min and ma* valve
char / b&tes 2 to B::0:
(his data t&pe represents a single character In C or C11 lang"ages "sing onl&
one b&te in a char
E*: - char ch V E*FQ
Single character literal &o" writing p"t them inside E F$single code%
Lo" write string p"t the do"ble codes $Y Z%
ASCII: - American Standard Code for Information Interchange It is a
standard It sho"ld followed b& all ke&board man"fact"res
..7 ke&s are "sing in ke&board /02 characters are there in o"r ke&board
appro*imatel& $small & large%
,hat is "ni code5
A% =ni code is a standard to incl"de the alphabet from all h"man lang"ages in java
=ni code s&stem "ses / b&tes to represent a character
String data t&pes: - A string represent gro"p of characters
E8: -
14
String nameVYS=!'EE< <E!!LZQ
String strVnew String$YS=!'EE< <E!!LZ%Q
3oolean data t&pes: - (hese data t&pes represent onl& / val"es either a tr"e or false
E8: - boolean responseVtr"eQ
;perators: - An operator is a s&mbol that performs on an operation
E8: -
If an operator acts on a single variable- it is called "nar& operator
If an operator acts on a / variables- it is called binar& operator
If an operator acts on a 0 variables- it is called ternar& operator
;perators in java: -
1) Arithmetic operators: - H - - - K - 4 - ]
(hese operators perform basic arithmetic calc"lations
E8: - If aV.0- bV:Q
;perator
a H b

;perands
;perator #eaning E*ample <es"lt
H Addition a H b .G
- S"btraction a I b G
K #"ltiplication a K b B:
4 !ivision a 4 b /B
] #od"les a ] b 0

/% =nar& operators: - - - HH - --
(hese operators act "pon a single operand
=nar& min"s $ - %:- (his operator negates the val"e of a variable
E8:-
int *V:Q
S&stemo"tprintln$-*%Q o4p:- -:
S&stemo"tprintln$-$-*%%Q o4p:- :
Increment $HH%: - (his operator increases the val"e of a variable b& E.E
E8: - int *V.Q
HH*Q ;41:- /
*HHQ ;41:- 0
,riting HH before a variable is called pre I incrementation
,riting HH after a variable is called post I incrementation
In pre incrementation - incrementation is done and other task is done ne*t In post
incrementation an& other task is performed first- incrementation is done at ne*t
E8: - 1re incrementation
15
*V.Q
S&stemo"tprint$*%Q o4p: - .
S&stemo"tprint$HH*%Q o4p: - /
S&stemo"tprint$*HH%Q o4p: - /
aV.Q bV/Q
aVHHbQ
S&stemo"tprint$a%Q o4p: 0
S&stemo"tprint$b%Q o4p: 0
1ost incrementation
*V.Q
S&stemo"tprint$*%Q o4p: .
S&stemo"tprint$*HH%Q o4p: .
S&stemo"tprint$*%Q o4p: /
aV.Q bV/Q
aVbHHQ
S&stemo"tprint$a%Q o4p: /
S&stemo"tprint$b%Q o4p: 0
,hat is the val"e of the following e*pression5 Aiven aVJ5 HHa K aHH5
A% A% 79 3% B7 C% J/ !% none of these
!ecrement $--%: - (his operator decreases the val"e of a variable b& E.E
E8: - int *V.Q
--*Q ;41: 2
*--Q ;41: -.
,riting -- before a variable is called pre I decrementation
,riting -- after a variable is called post I decrementation
In pre decrementation - decrementation is done and other task is done ne*t In post
decrementation an& other task is performed first- decrementation is done at ne*t
E8: - 1re decrementation
*V.Q
S&stemo"tprint$*%Q o4p: .
S&stemo"tprint$--*%Q o4p: 2
S&stemo"tprint$*--%Q o4p: 2
aV.Q bV/Q
aV--bQ
S&stemo"tprint$a%Q o4p: .
S&stemo"tprint$b%Q o4p: .
1ost decrementation
*V.Q
S&stemo"tprint$*%Q o4p: .
S&stemo"tprint$*--%Q o4p: .
S&stemo"tprint$*%Q o4p: 2
aV.Q bV/Q
aVb--Q
S&stemo"tprint$a%Q o4p: /
16
S&stemo"tprint$b%Q o4p: .
0% Assignment operators: - V - HV - -V - KV - 4V - ]V
a% (his operator is "sed to store a val"e in to a variable
E8: - int *V:Q
b% It is "sed to store the val"e of a variable in to another variable
E8: - int *V&Q
c% It is "sed to store the val"e of an e*pression in to a variable
E8: - int * V & H > - 7Q
E*pressions mean combination of a variables & val"es
+ote: - ,e can not write more than . variable at the left side of assignment
E8: - * H & V .:Q 44 invalid
Compact notation: -
E8: - n"m HV.2Q
sal KV2:Q
p 4VkQ
i -V07Q
& ]V:Q
(hese are the compact notations (hese are shortc"ts of java programs
7% <elational operator: - [ - T - [V - TV - VV - ^V
(hese operators are "sef"l to compare to 6"antities (he& are "sed in
constr"ction of condition
E8: -
if$aTb%----------Q
if$*^V.%---------Q
if$salTV:22222%----Q
if$aVVb%------Q
if$a[V.2%----Q
:% )ogical operators: - && - _ _ - ^
(hese operators are "sef"l to create compo"nd condition (he compo"nd
condition is condition of several simple conditions
E8: -
If$aTb__bTc__cTd%-----Q

An& one thing is tr"e this will be e*ec"tedQ
If$*VV.2&&&^V.2%----Q

3oth are tr"e this will be e*ec"ted
If$^$*VV^%%------Q
If$^$*VV. && &VV/%%----Q
B% 3oolean operators: - & - _ - ^
3oolean operators act "pon 3oolean variables and the& ret"rn 3oolean res"lt
boolean a-bQ
17
aVtr"eQ
bVfalseQ
E8: -
a & b false
a _ b tr"e
a & a tr"e boolean t&pe res"lts
b _ b false
^ a false
^ b tr"e
J% 3itwise operators: - ` - & - _ - a - [[ - TT - TTT
(hese operators act "p on individ"al bits binar& digits- $2&.% of n"mbers
!eci means .2 $2-9%
E*: - Convert 7: into binar&5
<"le: -!ivide the decimal n"mber s"ccessivel& b& / and take the remainders from
bottom to top
/ 7:
/ // - .
/ .. - 2
V . 2 . . 2 . G bits
/ : I . V 2 2 . 2 . . 2 .
/ / - .
.
Convert 22.2..2. into decimal
<"le:-#"ltipl& each digit b& the powers of / and take the s"m of the prod"cts
2 2 . 2 . . 2 .
/
J
/
B
/
:
/
7
/
0
/
/
/
.
/
2
2 H 2 H0/H2HG H 7H 2H. V 7:
-.
-/ 0
-0 /
-7 .
2
3itwise complement $`%:-
(his operator ret"rns the complement from of a given n"mber.
18
E8: - int *V.2Q
S&stemo"tprint$`*%Q o4p: - -..
(r"th table: - (r"th table is a table giving relationship between inp"t bits & o"tp"t
bits Anding means m"ltiplication
3itwise and $&%: - (his operator performs anding operation on ind"sial bits of
n"mbers
A+! Aate
And gate is a electronic circ"it It performs anding operations
E8: -
*V.2- &V..- * & & V 5 Q
Convert binar&
8 V 2 2 2 2 . 2 . 2
L V 2 2 2 2 . 2 . .
2 2 2 2 . 2 . 2 V .2

* & & V .2
3itwise or $ _ %: -
(his operator performs oring operations on individ"al bits of n"mbers ;ring
means both adding * & &
;< Aate
E8: -
*V.2- &V..- * & & V 5 Q
Convert binar&
19
8
2
2
.
.
L
2
.
2
.
8 & L
2
2
2
.
8
2
2
.
.
L
2
.
2
.
8 _ L
2
.
.
.
8 V 2 2 2 2 . 2 . 2
L V 2 2 2 2 . 2 . .
2 2 2 2 . 2 . . V ..

* _ & V ..
3itwise *or $ a % : - 'ere * means e*cl"sive a #eans cap- carpet- circ"mfle* (his
operator performs e*cl"sive orring operation on individ"al bits of n"mbers In the
inp"t bits if odd n"mber of E.Fs is there in the o"tp"t we will get E.F
8;< Aate
E8: -
*V.2- &V..- * a & V 5 Q
Convert binar&
8 V 2 2 2 2 . 2 . 2
L V 2 2 2 2 . 2 . .
2 2 2 2 2 2 2 . V .

* a & V .
3itwise left shift $ [[ % : -
(his operator shifts the bits of a n"mber two words left a specified n"mber of
times
E*:- * V .2 Q
* [[/ V5
20
8
2
2
.
.
L
2
.
2
.
8 a L
2
.
.
2
8V.2
8[[/
/
J
/
B
/
:
/
7
/
0
/
/
/
.
/
2

2 H 2 H 0/ H 2 H G H 2 H 2 H 2 V 72

*[[/ V 72
3itwise <ight Shift $ TT %: -
(his operator shifts the bits of a n"mber two words right a specified n"mber of
times
E*: - *V.2Q
*TT/ V5
8V.2
8TT/
/
J
/
B
/
:
/
7
/
0
/
/
/
.
/
2

2 H 2 H 2 H 2 H 2 H 2 H / H 2 V /

*TT/ V /

3itwise >ero-fill right shift $TTT%: - It is the same as TT
,hat is the difference between TT & TTT5
A% TT shift the bits / words right & properties the sign bit $2 represent Hve .
represent Ive sign%
TTT also shift the bits / words right bit It does not protect sign bit It alwa&s fills the
sign bit with 2(hat reason it is called >ero-fill right shift
44 bitwise operators
class 3its
M
p"blic static void main$StringNO args%
M
b&te *-&Q
21
2 2 2 2 . 2 . 2
2 2 . 2 . 2 2 2
2 2 2 2 . 2 . 2
2 2 2 2 2 2 . 2
*V.2Q
&V..Q
S&stemo"tprintln$P`*VPH$`*%%Q
S&stemo"tprintln$P* & &VPH$*&&%%Q
S&stemo"tprintln$P* _ &VPH$*_&%%Q
S&stemo"tprintln$P* a &VPH$*a&%%Q
S&stemo"tprintln$P*[[/VPH$*[[/%%Q
S&stemo"tprintln$P*TT/VPH$*TT/%%Q
S&stemo"tprintln$P*TTT/VPH$*TTT/%%Q
R
R
!:SpsrSCore javaTjavac 3itsjava
!:SpsrSCore javaTjava 3its
`*V-..
* & &V.2
* _ &V..
* a &V.
*[[/V72
*TT/V/
*TTT/V/
S&nta*: - S&nta* means correct format of writing a statement
G% (ernar& operator or conditional operator $ 5 - : %:-
(his operator called ternar& operator beca"se it acts on three variables
(ernar& means 0 (his operator is also called conditional operator beca"se it
represents a conditional statement
S&nta*: -
varVe*p.5e*p/:e*p0Q
44 conditional operators
class Conditional
M
p"blic static void main$StringNO args%
M
int *V.2-&V..Q
int ma*V$*T&%5*:&Q
S&stemo"tprintln$P#a*im"m val"e is VPHma*%Q
R
R
!:SpsrSCore javaTjavac Conditinaljava
!:SpsrSCore javaTjava Conditinal
#a*im"m val"e isV..
9% !ot operator $ % : - $#embership operator%
a% (o refer to a class in a package
22
java"til!ate- javaio3"ffered<eader
b% (o refer to a method in a class
mathpow$ %- empget+ame$ %- brread$ %
c% (o refer to a variable in a class
Emplo&eename$ %- empeno$ %- S&stemo"t
.2% Instanceof operator: - (o test whether an object belongs to a calss
E8: -
boolean *Vemp instanceof Emplo&ee
Instants means object It is a single word- so no space
E*ec"ting the statements one b& one is called se6"ential e*ec"tion
A program can write simple programs onl& "sing se6"ential e*ec"tion If we want to
develop bigger & comple* programs random e*ec"tion we needed
E*ec"ting the statements randoml& & repeatedl& is called random e*ec"tion
It is "sef"l to write a comple* program It is achieved "sing control statements
Control statements: - Control statements are the statements which alter or changed
the flow of e*ec"tion (he& are given below
.% if-else statement
/% do-while loop
0% while loop
7% for loop
:% for each loop
B% switch statement
J% break statement
G% contin"e statement
9% ret"rn statement
Statements can e*ec"te onl& one time b"t loop can e*ec"te several times repeatedl&
.% if-else statement: - (his statement is "sef"l to e*ec"te a task (ask means . or
more statements depending "pon whether a condition is tr"e or not
S&nta*: - if$condition%
M
Statements .Q
R
else
M
Statements /Q
R
If the condition is tr"e statements . is e*ec"ted ;therwise statements / are
e*ec"ted
$or%
if$condition.%
M
23
Statements .Q
R
else
if$condition/%
M
Statements /Q
R
else
if$condition0%
M
Statements 0Q
R
else
M
Statements 7Q
R
$or%
if$comdition.%
if$condition/%
if$condition0%
statements.
else
statements/
else
statements0
else
statements7
44 test a n"mber is Hve or -ve
class IfElse(est
M
p"blic static void main$StringNO args%
M
int n"mV:Q
if$n"mVV2%
S&stemo"tprintln$PIt is beroP%Q
else if$n"mT2%
S&stemo"tprintln$PIt is HveP%Q
else
S&stemo"tprintln$PIt is -veP%Q
R
R
!:SpsrSCore javaTjavac IfElse(estjava
!:SpsrSCore javaTjava IfElse(est
It is Hve
24
/% do-while loop: - (his loop repeatedl& e*ec"te a gro"p of statements as long as a
condition is tr"e
S&nta*: -
do
M
StatementsQ
R
while $condition%Q
44 generates noXs from . to .2
class !o,hile(est
M
p"blic static void main$StringNO args%
M
int *V.Q
do
M
S&stemo"tprint$*HYStZ%Q
*HHQ
R
while $*[V.2%Q
R
R
!:SpsrSCore javaTjavac !o,hile(estjava
!:SpsrSCore javaTjava !o,hile(est
. / 0 7 : B J G 9 .2
0% while loop: -(his loop is "sef"l to repeatedl& e*ec"te a gro"p of statements as long
as a given condition is tr"e
S&nta*: -
while$condition%
M
StatementsQ
R
44 even noXs "p to .2
class ,hile(est
M
p"blic static void main$StringNO args%
M
int *V/Q
while $*[V.2%
M
S&stemo"tprintln$*HYStZ%Q
*HV/Q
25
R
R
R
!:SpsrSCore javaTjavac ,hile(estjava
!:SpsrSCore javaTjava ,hile(est
/ 7 B G .2
,hile loop is more efficient from do-while loop 3eca"se will have better
control right from beginning
7% for loop: -
(his is "sed to repeatedl& e*ec"te a gro"p of statements as long as a
condition is tr"e Cor loop is s"itable for when the "ser knows the e*act n"mber of
repetitions
S&nta*: -
for$e*p.Qe*p/Qe*p0%
M
StatementsQ
R
E*p. is called initiali>ing e*pression
E*p/ is called conditional e*pression
E*p0 is called modif&ing e*pression
+ote: - ,e can write for loop b& eliminating e*p. or e*p/ or e*p0 or an& / e*p or all
the 0 e*pressions
44 generates noXs from . to .22
class Cor(est
M
p"blic static void main$StringNO args%
M
for$int iV2Qi[V.22QiHH%
M
S&stemo"tprintln$iHYStZ%Q
R
R
R
!:SpsrSCore javaTjavac Cor(estjava
!:SpsrSCore javaTjava Cor(est
. / 0 7 : B J G 9 .2
.. ./ .0 .7 .: .B .J .G .9 /2
/. // /0 /7 /: /B /J /G /9 02
0. 0/ 00 07 0: 0B 0J 0G 09 72
7. 7/ 70 77 7: 7B 7J 7G 79 :2
:. :/ :0 :7 :: :B :J :G :9 B2
B. B/ B0 B7 B: BB BJ BG B9 J2
26
J. J/ J0 J7 J: JB JJ JG J9 G2
G. G/ G0 G7 G: GB GJ GG G9 92
9. 9/ 90 97 9: 9B 9J 9G 99 .22
Infinite loop is e*ec"ted then press C(<) H C (hat is e*ec"tes for ever
Infinite loops are draw backs in a program
$or%
44 generates noXs from . to :2
class Cor(est.
M
p"blic static void main$StringNO args%
M
int iV2Q
for$QQ%
M
S&stemo"tprintln$i%Q
iHHQ
if$iT:2%
breakQ
R
R
R
!:SpsrSCore javaTjavac Cor(est.java
!:SpsrSCore javaTjava Cor(est.
. / 0 7 : B J G 9 .2 .. ./ .0 .7 .:
.B .J .G .9 /2 /. // /0 /7 /: /B /J /G /9 02
0. 0/ 00 07 0: 0B 0J 0G 09 72 7. 7/ 70 77 7:
7B 7J 7G 79 :2
3reak is statement to come o"t from the loop
+ote: - It is possible to write a loop inside other loops- the& are called nested loops
S&nta*: -
for$e*p.Qe*p/Qe*p0%
M
for$e*p.Qe*p/Qe*p0%
M
statementsQ
R
R
:% for each loop: - (his loop is "sed to repeatedl& e*ec"tes a gro"p of statements for
each element of a collection Collection means gro"p of elements Arra& means a
gro"p of elements & also java"til classes
S&nta*: -
for$var:collection%
27
M
statementsQ
R
44 for each loop
class CorEach(est
M
p"blic static void main$StringNO args%
M
int arrNOVM.2-00-:B-/2-92RQ
for$int *:arr%
M
S&stemo"tprintln$*HZStZ%Q
R
R
R
!:SpsrSCore javaTjavac CorEach(estjava
!:SpsrSCore javaTjava CorEach(est
.2 00 :B /2 92
B% switch statement: - switch statement is "sef"l to e*ec"te a partic"lar task from
several available tasks depending "pon the val"e of a variable
S&nta*: -
switch$var%
M
case val"e.:
Statements .Q
breakQ
case val"e/:
Statements /Q
breakQ
__
case val"e n:
Statements nQ
breakQ
defa"lt :
StatementsQ
R
44 switch statement
class Switch(est
M
p"blic static void main$StringNO args%
M
char colorVXgXQ
28
switch$color%
M
case XrX:
case X<X:
S&stemo"tprintln$P<edP%Q
breakQ
case XgX:
case XAX:
S&stemo"tprintln$PAreenP%Q
breakQ
case XbX:
case X3X:
S&stemo"tprintln$P3l"eP%Q
breakQ
case XwX:
case X,X:
S&stemo"tprintln$P,hiteP%Q
breakQ
defa"lt:
S&stemo"tprintln$P+o colorP%Q
R
R
R
!:SpsrSCore javaTjavac Switch(estjava
!:SpsrSCore javaTjava Switch(est
Areen
3reak is statement to come o"t from a switch statement
Switch statement is "sef"l to handle men" driven programs #en" means a list of
items or options for the "ser select
J% break statement: -
a% it is "sef"l to come o"t of a loop
b% it is "sef"l to come o"t of a switch statement
c% it is "sed in a nested blocks to go to end of a block
S&nta*: -
break blocknameQ 44 go to end of block
go to statement is "sed in to j"mp one statement to another statement
,h& goto statement are not available in java5
A% .% goto statements lead to conf"sion for a programmer
/% goto statement from infinite loops which are draw backs in a program
0% (he doc"mentation of the program will become diffic"lt with more gotoFs are
"sed !oc"mentation means preserving a cop& of the program Algorithm means a
step of logic
7% goto statements are not part of str"ct"red programming 3eca"se of above
reasons the goto statements are eliminates of java
29
44 break test
class 3reak(est
M
p"blic static void main$String argsNO%
M
for$int iV2Qi[V.2QiHH%
M
if$iVV7%
breakQ
S&stemo"tprintln$i%Q
R
S&stemo"tprintln$Pbreak e*ec"tedP%Q
R
R
!:SpsrSCore javaTjavac 3reak(estjava
!:SpsrSCore javaTjava 3reak(est
2
.
/
0
break e*ec"ted
)abeled break statement: - It can be "sed as a form of goto statement in java
44 break to go to end of a block
class 3reak(est.
M
p"blic static void main$StringNO args%
M
boolean *Vtr"eQ
bl.:M
bl/:M
bl0:M
S&stemo"tprintln$P3lock 0P%Q
if$*Vtr"e%
break bl/Q
R
S&stemo"tprintln$P3lock /P%Q
R
S&stemo"tprintln$P3lock .P%Q
R
S&stemo"tprintln$P;"t of allP%Q
R
R
!:SpsrSCore javaTjavac 3reak(est.java
!:SpsrSCore javaTjava 3reak(est.
30
3lock 0
3lock .
;"t of all
G% contin"e statement: -
(his statement is "sef"l to contin"e the ne*t repetition of a loop ,hen the
statement will s"bse6"ent statements in the loop are not e*ec"ted
44 contin"e statement
class Contin"eStatement(est
M
p"blic static void main$StringNO args%
M
for$int iV.2QiTV.Qi--%
M
if$iT:%
contin"eQ
S&stemo"tprint$iHZStZ%Q
R
R
R
!:SpsrSCore javaTjava Contin"eStatement(est
!:SpsrSCore javaTjava Contin"eStatement(est
: 7 0 / .
9% ret"rn statements: - it is "sed in / wa&s
.% It is "sed to come o"t of a method to a calling method
main$ % displa&$ %
+ote: - If ret"rn is "sed in main the entire program is terminated
/% ret"rn statement can be "sed to ret"rn some val"e to the calling method
E8: - ret"rn *Q
44 ret"rn in main$ %
class <et"rn(est
31
displa&$ %

ret"rnQ
M
p"blic static void main$StringNO args%
M
int n"mV.Q
S&stemo"tprintln$P3efore ret"rnP%Q
if$n"mVV.%
ret"rnQ 44 S&steme*it$2%Q
S&stemo"tprintln$PAfter ret"rnP%Q
R
R
!:SpsrSCore javaTjavac <et"rn(estjava
!:SpsrSCore javaTjava <et"rn(est
3efore ret"rn
S&steme*it$2% will terminate the program from an& method
,hat is the difference between S&steme*it$2% and S&steme*it$.%5
A% S&steme*it$2% represents normal termination
S&steme*it$.% represents termination d"e to an error
44 print odd noXs "p to :2
class ;dd
M
p"blic static void main$String argsNO%
M
int nV.Q
S&stemo"tprintln$Podd noXs are:P%Q
while$n[.22%
M
S&stemo"tprint$PStPHn%Q
nHV/Q
R
R
R
!:SpsrSCore javaTjavac ;ddjava
!:SpsrSCore javaTjava ;dd
odd noXs are:
. 0 : J 9 .. .0 .: .J
.9 /. /0 /: /J /9 0. 00 0: 0J
09 7. 70 7: 7J 79 :. :0 :: :J
:9 B. B0 B: BJ B9 J. J0 J: JJ
J9 G. G0 G: GJ G9 9. 90 9: 9J
99
44 (est whether a n"mber is even or odd
class Even;<;dd
32
M
p"blic static void main$StringNO args%
M
int aV/2Q
if$a]/VV2%
S&stemo"tprintln$PIt is even n"mberP%Q
else
S&stemo"tprintln$PIt is odd n"mberP%Q
R
R
!:SpsrSCore javaTjavac Even;<;ddjava
!:SpsrSCore javaTjava Even;<;dd
It is even n"mber
44 given no is prime or not
class 1rime;r+ot
M
p"blic static void main$String argsNO%
M
int i-j-k-lQ
iV9Q
jV/Q
lV.Q
while$j[9%
M
kVi]jQ
if$kVV2%
lVkQ
jHHQ
R
if$lVV2%
M
S&stemo"tprintln$Pnot primeP%Q
R
else
M
S&stemo"tprintln$PprimeP%Q
R
R
R
!:SpsrSCore javaTjavac 1rime;r+otjava
!:SpsrSCore javaTjava 1rime;r+ot
not prime
44displa& a m"ltiplication table
class #"ltiflication(able
33
M
p"blic static void main$String argsNO%
M
int iV/-jQ
S&stemo"tprintln$P#"ltiplication table isV:P%Q
for$jV.Qj[V.2QjHH%
M
S&stemo"tprintln$iHPKPHjHPVPH$iKj%%Q
R
R
R
!:SpsrSCore javaTjavac #"ltiflication(ablejava
!:SpsrSCore javaTjava #"ltiflication(able
#"ltiplication table isV:
/K.V/
/K/V7
/K0VB
/K7VG
/K:V.2
/KBV./
/KJV.7
/KGV.B
/K9V.G
/K.2V/2
4K displa& the stars the following
K
K K
K K K
K K K K
K K K K K
K4
class Stars(est
M
p"blic static void main$String argsNO%
M
int starsV.-linesV:-spacesV:Q
int iV.-j-kQ
for$iV2Qi[linesQiHH%
M
44 print spaces first
for$kV2Qk[spacesQkHH%
S&stemo"tprint$P P%Q
for$jV2Qj[$stars%QjHH%
S&stemo"tprint$PK P%Q
spaces--Q
34
stars HV.Q
S&stemo"tprintln$ %Q
R
R
R
!:SpsrSCore javaTjavac Stars(estjava
!:SpsrSCore javaTjava Stars(est
K
K K
K K K
K K K K
K K K K K
44 print the Cibonacci series "p to .2
class Cibonacci
M
p"blic static void main$String argsNO%
M
int iV2Q
int jV.-cQ
S&stemo"tprint$i%Q
S&stemo"tprint$PStPHj%Q
for$int kV0Qk[V.2QkHH%
M
cViHjQ
S&stemo"tprint$PStPHc%Q
iVjQ
jVcQ
R
R
R
!:SpsrSCore javaTjavac Cibonaccijava
!:SpsrSCore javaTjava Cibonacci
2 . . / 0 : G .0 /. 07
44 print prime noFs
class 1rimeSeries
M
p"blic static void main$String argsNO%
M
int k-m-jQ
for$jV/Qj[.22QjHH%
M
kV2Q
for$int iV.Qi[VjQiHH%
35
M
mVj]iQ
if$mVV2%
kVkH.Q
R
if$kVV/%
S&stemo"tprint$PStPHj%Q
R
R
R
!:SpsrSCore javaTjavac 1rimeSeriesjava
!:SpsrSCore javaTjava 1rimeSeries
/ 0 : J .. .0 .J .9 /0 /9 0. 0J 7. 70 7J
:0 :9 B. BJ J. J0 J9 G0 G9 9J
Accepting data from ke&board: -
If &o" want to data read inp"t from ke&board
Stream:- A stream represent flow of data from one place to another place Stream
carries data (here are / t&pes of streams
.% In p"t streams: - in p"t streams read data or accept data
/% ;"t p"t streams: - o"t p"t streams send or write data All streams are
represented b& classes in javaio package
S&stem in: - Inp"tStream obj I ke&board 'ere in means field
S&stemo"t: - 1rintStream obj I monitor
S&stemerr: - 1rintStream obj I monitor
,hat is difference between S&stemo"t & S&temerr5
A% S&stemo"t is "sef"l to displa& general messages
S&stemerr is "sed to displa& error messages Stream is one of the io device
Inp"t4o"tp"t devices are represented b& streams Streams are "sef"l to achieve
hardware independence in a java program
Inp"tStream<eader: -It is read data from ke&board
3"ffered<eader: - ,e read the data at 3"fferd<eader
.% Connect ke&board to Inp"tStream<eader
Inp"tStream<eader isrVnew Inp"tStream<eader$S&stemin%
/% Connect Inp"tStream<eader to 3"ffered<eader
3"ffered<eader brVnew 3"ffered<eader$isr%
0% <ead data from br "sing read$ % or read)ine$ % methods

44 accepting a char from ke&board
import javaioKQ
36
class Char(est
M
p"blic static void main$StringNO args% throws I;E*ception
M
44 create 3"ffered<eader obj
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$PEnter a char : P%Q
char chV$char%brread$ %Q
S&stemo"tprintln$P&o" entered : PHch%Q
R
R
!:SpsrSCore javaTjavac Char(estjava
!:SpsrSCore javaTjava Char(est
Enter a char : 7
&o" entered : 7
(&pe casting: - Converting one data t&pe into another data t&pe is called t&pe
casting or casting ,riting the data t&pe with in the simple braces- it is called caste
operator Cast means converting
E*ception represents r"n time errors (hrows E*ception- throw o"t
e*ception witho"t handling it
44 accepting a string from ke&board
import javaioKQ
class String(est
M
p"blic static void main$StringNO args% throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$PEnter a char : P%Q
String chVbrread)ine$ %Q
S&stemo"tprintln$P&o" entered : PHch%Q
R
R
!:SpsrSCore javaTjavac String(estjava
!:SpsrSCore javaTjava String(est
Enter a char : =sha
&o" entered : =sha
44 accepting integers from ke&board
import javaioKQ
class Int(est
M
37
p"blic static void main$StringNO args% throws I;E*ception
M
44 create 3"ffered<eader obj
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$PEnter a n"mber : P%Q
int nVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprintln$P&o" entered : PHn%Q
R
R
!:SpsrSCore javaTjavac Int(estjava
!:SpsrSCore javaTjava Int(est
Enter a n"mber : BJ
&o" entered : BJ
44 accepting float n"mbers from ke&board
import javaioKQ
class Cloat(est
M
p"blic static void main$StringNO args% throws I;E*ception
M
44 create 3"ffered<eader obj
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$PEnter a n"mber : P%Q
float nVCloatparseCloat$brread)ine$ %%Q
S&stemo"tprintln$P&o" entered : PHn%Q
R
R
!:SpsrSCore javaTjavac Cloat(estjava
!:SpsrSCore javaTjava Cloat(est
Enter a n"mber : /0
&o" entered : /02
44 accepting and displa&ing emp data
import javaioKQ
class Empdata
M
p"blic static void main$StringNO args% throws I;E*ception
M
44 create 3"ffered<eader obj
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
44 accept emplo&ee data
S&stemo"tprint$PEnter id : P%Q
38
int idVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprint$PEnter se* : P%Q
char se*V$char%brread$ %Q
brskip$/%Q
S&stemo"tprint$PEnter name : P%Q
String nameVbrread)ine$ %Q
S&stemo"tprintln$PId VPHid%Q
S&stemo"tprintln$PSe* VPHse*%Q
S&stemo"tprinlnt$P+ame VPHname%Q
R
R
!:SpsrSCore javaTjavac Empdatajava
!:SpsrSCore javaTjava Empdata
Enter id : .02
Enter se* : m
Enter name : Sathish
Id V.02 Se* Vm +ame VSathish
4K accepting two noXs from ke& board & find the res"lt
;f their H - - - * - 4 K4
import javaioKQ
class Arithmetic
M
p"blic static void main$String argsNO%throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$Penter m val"e: P%Q
int mVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprint$Penter n val"e: P%Q
int nVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprintln$PAddition is:VPH$mHn%%Q
S&stemo"tprintln$PS"btraction is:VPH$m-n%%Q
S&stemo"tprintln$Pm"ltiplication is:VPH$mKn%%Q
S&stemo"tprintln$P!ivision is:VPH$m4n%%Q
R
R
!:SpsrSCore javaTjavac Arithmeticjava
!:SpsrSCore javaTjava Arithmetic
enter m val"e: :B
enter n val"e: 07
Addition is:V92
S"btraction is:V//
m"ltiplication is:V.927
!ivision is:V.
44 print the even noXs between m & n
import javaioKQ
39
class Even+os
M
p"blic static void main$String argsNO%throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$Penter m val"e: P%Q
int mVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprint$Penter n val"e: P%Q
int nVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprintln$PEven noXs are:-P%Q
do
M
int iVm]/Q
if$iVV2%
S&stemo"tprint$PStPHm%Q
mHHQ
R while$m[n%Q
R
R
!:SpsrSCore javaTjavac Even+osjava
!:SpsrSCore javaTjava Even+os
enter m val"e: /2
enter n val"e: :0
Even noXs are: -
/2 // /7 /B /G 02 0/ 07 0B 0G 72 7/
77 7B 7G :2 :/
44 (est whether a character is vowel or not
import javaioKQ
class Ch(est
M
p"blic static void main$String argsNO%throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
char chQ
S&stemo"tprint$PEnter an& character: P%Q
chV$char%brread$ %Q
if$chVVXaX__chVVXAX__chVVXeX__chVVXEX__chVVXiX__chVVXIX__chVVXoX__
chVVX;X__chVVX"X__chVVX=X%
S&stemo"tprintln$P?owelP%Q
else
S&stemo"tprintln$PConstantP%Q
R
R
40
!:SpsrSCore javaTjavac Ch(estjava
!:SpsrSCore javaTjava Ch(est
Enter an& character: s
Constant
!:SpsrSCore javaTjava Ch(est
Enter an& character: o
?owel
44 test whether a n"mber is perfect s6"ire or not
import javaioKQ
class 1erefectS6"are.
M
p"blic static void main$String argsNO%throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
int iV2-*V2Q
S&stemo"tprint$Penter an& n"mber: P%Q
iVIntegerparseInt$brread)ine$ %%Q
*V$int%#aths6rt$i%Q
if$iVV$*K*%%
S&stemo"tprintln$P1erfect s6"are P%Q
else
S&stemo"tprintln$P+ot a perfect S6"are P%Q
R
R
!:SpsrSCore javaTjavac 1erefectS6"are.java
!:SpsrSCore javaTjava 1erefectS6"are.
Enter an& n"mber: :
+ot a perfect S6"are
!:SpsrSCore javaTjava 1erefectS6"are.
Enter an& n"mber: 9
1erfect s6"are
44 given point lies inside the circle or o"t side the circle or on the circle
import javaioKQ
class 1oint)ies
M
p"blic static void main$String argsNO%throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
int *V2-&V2-rV2Q
S&stemo"tprintln$PEnter * & & co-ordinates : P%Q
*VIntegerparseInt$brread)ine$ %%Q
41
&VIntegerparseInt$brread)ine$ %%Q
S&stemo"tprintln$PEnter <adi"s :P%Q
rVIntegerparseInt$brread)ine$ %%Q
int aV$*K*%H$&K&%Q
int bV$rKr%Q
if$aVVb%
S&stemo"tprintln$P1oint lies on the CircleP%Q
else if$aTb%
S&stemo"tprintln$P1oint lies o"t side the CircleP%Q
else
S&stemo"tprintln$P1oint lies in side the CircleP%Q
R
R
!:SpsrSCore javaTjavac 1oint)iesjava
!:SpsrSCore javaTjava 1oint)ies
Enter * & & co-ordinates :
.2
/2
Enter <adi"s :
7
1oint lies o"t side the Circle
44 to find the Cactorial val"e
import javaioKQ
class Cactorial
M
p"blic static void main$String argsNO%throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
int factV.Q
S&stemo"tprint$PEnter an& +"mber : P%Q
int nVIntegerparseInt$brread)ine$ %%Q
if$nVV2%
S&stemo"tprintln$PCactorial of 2 is .P%Q
while$nT2%
M
factVfactKnQ
n-V.Q
R
S&stemo"tprintln$PCactorial VPHfact%Q
R
R
!:SpsrSCore javaTjavac Cactorialjava
!:SpsrSCore javaTjava Cactorial
42
Enter an& +"mber : :
Cactorial V./2
44 s"m of the digits
import javaioKQ
class S"m!igits
M
p"blic static void main$String argsNO%throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
int nV2-s"mV2-dV2Q
S&stemo"tprint$PEnter an& +"mber : P%Q
nVIntegerparseInt$brread)ine$ %%Q
do
M
dVn].2Q
s"mHVdQ
nVn4.2Q
R
while$nT2%Q
S&stemo"tprintln$PS"m of digits VPHs"m%Q
R
R
!:SpsrSCore javaTjava S"m!igits
Enter an& +"mber : /0B
S"m of digits V..
4K all the 7 n"mber digits S6"are of the .st / digits are perfect s6"ire & also last /
digits K4
import javaioKQ
class 1erfectS6"are
M
p"blic static void main$String argsNO%
M
int nlV2-nrV2-tempV2-*V2-&V2->V2Q
for$int iV.222Qi[V9999QiHH%
M
tempViQ
*V$int%#aths6rt$temp%Q
M
if$tempVV*K*%
M
nlVtemp4.22Q
nrVtemp].22Q
&V$int%#aths6rt$nl%Q
43
>V$int%#aths6rt$nr%Q
if$$nlVV&K&%&&$nrVV>K>%%
S&stemo"tprint$tempHZStZ%Q
R
R
R
R
R
!:SpsrSCore javaTjavac 1erfectS6"arejava
!:SpsrSCore javaTjava 1erfectS6"are
.B22 .BG. /:22 0B22 7922 B722 G.22
Arra&s: - An arra& represents a gro"p of elements of same data t&pe (he& are /
t&pes
.% Single dimensional arra& /% #"lti dimensional arra&s $/d-0d-DD%
.% Single dimensional arra&s $.d%: -
A Single dimensional arra& represents is a single row or a single col"mn of an
elements
<ow - 'ori>ontal arrangement- Col"mn - ?ertical arrangement
Cor e*ample marks obtained b& a st"dent in : s"bjects are Single dimensional
arra&
Creating a Single dimensional arra&: - (here are / wa&s
.% ,e can declare a Single dimensional arra& & directl& store the elements in to it
E8: - int marksN OVM:2-::-99-GB-J/RQ

marks N2O N.O N/O N0O N7O
marksNiO i is called inde* Inde* represents the position n"mber of the element in
the arra&
(he Single dimensional arra& is onl& one inde* (hat is i
float salN OVM9222f-./22:2f-:BJG07fRQ
char chN OVMEaF-FbF-FcF-FdFRQ
String nameN OVMYraj"Z-Zs"dheerZ-Zs"nilZ-ZlathaZRQ
/% ,e can allot for Single dimensional arra& "sing new operator and later store the
elements
int marksN OVnew intN:OQ
marksN2OV:7Q marksN.OVB:Q - - - - marksN7OV9GQ
do"ble *N OVnew do"bleN.22OQ
String nameN OVnew StringN:2OQ
+ote: - Arra& name length $is a propert& of an& arra&% gives the si>e of the arra&
arra&length
44
:2 :: 99 GB J/
44 total marks & percentage
import javaioKQ
class Single!imensionalArra&
M
p"blic static void main$StringNO args% throws I;E*ception
M
44 to accept data from ke&board
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$P'ow man& s"bjects 5 VP%Q
int nVIntegerparseInt$brread)ine$ %%Q
44 create an arra& with si>e n
int marksNOVnew intNnOQ
44 store marks into marksNO
for$int iV2Qi[nQiHH%
M
S&stemo"tprintln$PEnter marks :P%Q
marksNiOVIntegerparseInt$brread)ine$ %%Q
R
44 displa& the marks & percentage
S&stemo"tprint$P(he marks are :P%Q
int totV2Q
for$int iV2Qi[nQiHH%
M
S&stemo"tprintln$marksNiO%Q
totHVmarksNiOQ
R
S&stemo"tprintln$P(otal marks V ZHtot%Q
44 calc"late percentage
float percentV$float%tot4nQ
S&stemo"tprintln$P1ercentage V PHpercent%Q
S&stemo"tprintln$PSi>e of marks V PHmarkslength%Q
R
R
!:SpsrSCore javaTjavac Single!imensionalArra&java
!:SpsrSCore javaTjava Single!imensionalArra&
'ow man& s"bjects 5 V0
Enter marks :
0B
Enter marks :
J:
Enter marks :
9J
(he marks are : 0B J: 9J
(otal marks V /2G
45
1ercentage V B900000B
Si>e of marks V 0
/% (wo dimensional arra& $/d%: - A two dimensional arra& represents several rows &
col"mns of data
Cor e*ample obtained b& a gro"p of st"dents in : different s"bjects
Creating (wo dimensional arra&: - (here are / wa&s
.% ,e can declare initiali>e a two dimensional arra& with elements
int marksN O N OVMM:2-:.-:/-:0-:7R-
MB2-B.-B/-B0-B7R-
MJ2-J.-J/-J0-J7RRQ
r2
r.
r/
c. c/ c0 c7 c:
marksNiONjO inde*es of marks
A (wo dimensional arra& has / inde*es (he& represent row position & col"mn
position n"mbers A (wo dimensional arra& is a combination of single dimensional
arra&s
A three dimensional arra& has 0 inde*es A three dimensional arra& is a combination
of several (wo dimensional arra&
/% ,e can created two dimensional arra&s "sing new operator & stored the elements
later
int marksN ON OVnew intN0ON:OQ
float salN ON OVnew floatN:ON.2OQ
String strN ON OVnew StringN0ON.2OQ
44 (wo dimensional arra&
import javaioKQ
class (wo!imensionalArra&
M
p"blic static void main$StringNO args%
M
44 take (wo dimensional arra&
float *NONOVMM..f-./f-.0f-.7fR-
M/.f-//f-/0f-/7fR-
M0.f-0/f-00f-07fRRQ
44 read from the (wo dimensional arra& & displa&
for$int iV2Qi[0QiHH%
M
46
:2
B2
J2
:.
B.
J/
:/
B/
J0
:0
B0
J0
:7
B7
J7
for$int jV2Qj[7QjHH%
M
S&stemo"tprint$*NiONjOHPStP%Q
R
S&stemo"tprintln$ %Q
R
R
R
!:SpsrSCore javaTjavac (wo!imensionalArra&java
!:SpsrSCore javaTjava (wo!imensionalArra&
.. ./ .0 .7
/. // /0 /7
0. 0/ 00 07
A two dimensional arra& represent a matri*
#ethod parameter: - #ethod parameter is a variable that receive data from o"t side
into the method
Command line arg"ments: -
Command line arg"ments are val"es passed to main method from s&stem
prompt
Command line command line arg"ments
J?# allots memor& in r"n time
argslength Si>e of arg"ments
44 arg"ment length e*ample
class Arg"ment
M
p"blic static void main$String argsNO%
M
int nVargslengthQ
S&stemo"tprintln$Pno of arg"ments:VPHn%Q
for$int iV2Qi[nQiHH%
M
S&stemo"tprintln$argsNiO%Q
R
R
R
!:SpsrSCore javaTjavac Arg"mentjava
!:SpsrSCore javaTjava Arg"ment ./ 07 :7 S"nil W"mar <edd&
47
java Arr .. // S"dheer <edd&
no of arg"ments:VB
./
07
:7
S"nil
W"mar
<edd&
44 to find s"m of two matrices
import javaioKQ
class S"m#atri*
M
p"blic static void main$StringNO args% throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$Penter r val"e : P%Q
int rowVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprint$Penter c val"e : P%Q
int colVIntegerparseInt$brread)ine$ %%Q
int aNONOVnew intNrowONcolOQ
S&stemo"tprintln$PEnter first matri* elements :SnP%Q
for$int iV2Qi[rowQiHH%
M
for$int jV2Qj[colQjHH%
M
aNiONjOVIntegerparseInt$brread)ine$ %%Q
R
R
S&stemo"tprintln$PCirst matri* elements are:SnP%Q
for$int iV2Qi[rowQiHH%
M
for$int jV2Qj[colQjHH%
M
S&stemo"tprint$aNiONjOHPStP%Q
R
S&stemo"tprintln$ %Q
R
int bNONOVnew intNrowONcolOQ
S&stemo"tprintln$PEnter second matri* elements :SnP%Q
for$int iV2Qi[rowQiHH%
M
for$int jV2Qj[colQjHH%
M
bNiONjOVIntegerparseInt$brread)ine$ %%Q
R
R
48
S&stemo"tprintln$PSecond matri* elements are:SnP%Q
for$int iV2Qi[rowQiHH%
M
for$int jV2Qj[colQjHH%
M
S&stemo"tprint$bNiONjOHPStP%Q
R
S&stemo"tprintln$ %Q
R
S&stemo"tprintln$PS"m matri* is : P%Q
int cNONOVnew intNrowONcolOQ
for$int iV2Qi[rowQiHH%
M
for$int jV2Qj[colQjHH%
M
cNiONjOVaNiONjOHbNiONjOQ
S&stemo"tprint$cNiONjOHPStP%Q
R
S&stemo"tprintln$ %Q
R
R
R
!:SpsrSCore javaTjavac S"m#atri*java
!:SpsrSCore javaTjava S"m#atri*
enter r val"e : /
enter c val"e : 0
Enter first matri* elements :
.
/
0
7
:
B
Cirst matri* elements are:
. / 0
7 : B
Enter second matri* elements :
J
G
9
.2
..
./
Second matri* elements are:
49
J G 9
.2 .. ./
S"m matri* is :
G .2 ./
.7 .B .G
44 to find prod"ct of two matrices
import javaioKQ
class 1rodmatri*
M
p"blic static void main$StringNO args% throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$Penter r val"e : P%Q
int row.VIntegerparseInt$brread)ine$ %%Q
S&stemo"tprint$Penter c val"e : P%Q
int col.VIntegerparseInt$brread)ine$ %%Q
int aNONOVnew intNrow.ONcol.OQ
S&stemo"tprintln$PEnter first matri* elements :SnP%Q
for$int iV2Qi[row.QiHH%
M
for$int jV2Qj[col.QjHH%
M
aNiONjOVIntegerparseInt$brread)ine$ %%Q
R
R
S&stemo"tprintln$PCirst matri* elements are:SnP%Q
for$int iV2Qi[row.QiHH%
M
for$int jV2Qj[col.QjHH%
M
S&stemo"tprint$aNiONjOHPStP%Q
R
S&stemo"tprintln$ %Q
R
S&stemo"tprintln$Penter r val"e :P%Q
int row/VIntegerparseInt$brread)ine$ %%Q
S&stemo"tprintln$Penter c val"e : P%Q
int col/VIntegerparseInt$brread)ine$ %%Q
int bNONOVnew intNrow/ONcol/OQ
S&stemo"tprintln$PEnter second matri* elements :SnP%Q
for$int iV2Qi[row/QiHH%
M
for$int jV2Qj[col/QjHH%
M
50
bNiONjOVIntegerparseInt$brread)ine$ %%Q
R
R
S&stemo"tprintln$PSecond matri* elements are:SnP%Q
for$int iV2Qi[row/QiHH%
M
for$int jV2Qj[col/QjHH%
M
S&stemo"tprint$bNiONjOHPStP%Q
R
S&stemo"tprintln$ %Q
R
if$row/VVcol.%
M
S&stemo"tprintln$P1rod matri* is : P%Q
int cNONOVnew intNrow.ONcol/OQ
for$int iV2Qi[row.QiHH%
M
for$int jV2Qj[col/QjHH%
M
for$int kV2Qk[row/QkHH%
M
cNiONjOHVaNiONkOKbNkONjOQ
R
R
R
for$int iV2Qi[row.QiHH%
M
for$int jV2Qj[col/QjHH%
M
S&stemo"tprint$cNiONjOHPStP%Q
R
S&stemo"tprintln$ %Q
R
R
else
S&stemo"tprintln$Pcan not prod"ct the matri*P%Q
R
R
!:SpsrSCore javaTjavac 1rodmatri*java
!:SpsrSCore javaTjava 1rodmatri*
enter r val"e : 0
enter c val"e : /
Enter first matri* elements :
.
51
/
0
7
:
B
Cirst matri* elements are:
. /
0 7
: B
enter r val"e : /
enter c val"e : 0
Enter second matri* elements:
.
/
0
7
:
B
Second matri* elements are:
. / 0
7 : B
1rod matri* is :
9 ./ .:
.9 /B 00
/9 72 :.
44 to transpose a matrices
import javaioKQ
class (ranspose#atri*
M
p"blic static void main$StringNO args% throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$Penter r val"e :P%Q
int rowVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprint$Penter c val"e : P%Q
int colVIntegerparseInt$brread)ine$ %%Q
int aNONOVnew intNrowONcolOQ
S&stemo"tprintln$PEnter matri* elements :SnP%Q
for$int iV2Qi[rowQiHH%
M
for$int jV2Qj[colQjHH%
M
aNiONjOVIntegerparseInt$brread)ine$ %%Q
R
R
52
S&stemo"tprintln$Pmatri* elements are:SnP%Q
for$int iV2Qi[rowQiHH%
M
for$int jV2Qj[colQjHH%
M
S&stemo"tprint$aNiONjOHPStP%Q
R
S&stemo"tprintln$ %Q
R
S&stemo"tprintln$P(ranspose matri* is : P%Q
for$int iV2Qi[colQiHH%
M
for$int jV2Qj[rowQjHH%
M
S&stemo"tprint$aNjONiOHPStP%Q
R
S&stemo"tprintln$ %Q
R
R
R
!:SpsrSCore javaTjavac (ranspose#atri*java
!:SpsrSCore javaTjava (ranspose#atri*
enter r val"e : /
enter c val"e : 0
Enter matri* elements :
.
/
0
7
:
B
matri* elements are:
. / 0
7 : B
(ranspose matri* is :
. 7
/ :
0 B
Strings: -strings represent a gro"p of characters In C or CHH strings are character
arra&s S 2 is n"ll character It is end of string
In java strings are not character arra&s Strings are string class object in
object A string is an object of string class An& string is an object of string class in
java
Creating strings: -
.% ,e can declare a string t&pe variable & store directl& in to it
53
String strVZhelloZQ
/% ,e can create a string class object "sing new operator and pass the string to it
String s. Vnew String $Y'aiZ%Q
0% ,e can create a string b& converting a character arra& in to a string object
Char arrN O V MEaF-EbF-FcF-FdF-FeF-EfFRQ
String s/ V new String $arr%Q
String s/Vnew String $arr-.-0%Q
String methods: -
Javalangstring: -
.% String concat $String str%: - Concat means method A method can for form a
task Concatenates the caning string with str
+ote: - Hwill also do the same
Concat is a method it joins two strings ,hen &o" call concat pass a string object to
it
E*: - String s. V Y'&deraZQ
String s/ V YbadZQ
String * V s. concat $s/%Q
!ispla& '&derabad
H is called concatenation operator
Concat: - joining string at the end another string
/% int length $ %:- ret"rns the length of a string
E*:- String s. V Y'&deraZQ
Int n V s. length$ %Q
0% char charAt$int inde*%:-
<et"rns the character at the specified location $from 2%
7% int compare to $string Str%: - <et"rns a negative val"e- if the calling Sting comes
before str in dictionar& order- a Hve val"e- if the String comes after str- or 2- if the
String are e6"al
E8: -
int nVs.compare(o$s/%Q
if s.VVs/- nV2
if s.Ts/- nT2
if s.[s/- n[2
s.VZbo&Z- s/VZbo*ZQ
It is a Hve n"mber 8 is first- first is less val"e
:% boolean e6"als$String str%: - <et"rns tr"e if the calling string e6"als str E6"als is
case sensitive method
B% boolean e6"alsIgnoreCase$String str%: - Sane as above (his is case insensitive
method
J% boolean starts,ith$String prefi*%: - <et"rns tr"e if the calling string starts with
prefi* 1refi* means the string starting with beginning
G% boolean ends,ith$String s"ffi*%: - <et"rns tr"e if the invoking string ends with
s"ffi* S"ffi* means the string ending with beginning
54
+ote: - Above / methods "se case sensitive comparison
9% int inde*;f$String str%: - <et"rns the first occ"rrence of str in the string
.2% int lastInde*;f$String str%: - <et"rns the last occ"rrence of str in the string
+ote: - 3oth the above methods ret"rn Ive val"e- if str not fo"nd in the calling
string Co"nting starts from 2
E*: - String strVZ(his is a bookZQ
/ :
int nVstrinde*;f$YisZ%Q 44 It gives / onl& beca"se first position
int nVstrlastInde*;f$YisZ%Q 44 it gives : onl&
..% String replace$char oldChar-char newChar%: - <et"rns a new string (hat is
obtained b& replacing characters old char in the string with new char
./% String s"bstring$int beginInde*%: - <et"rn a new string consisting of all
characters from begin inde* "ntil the end of the string
.0% String s"bstring$int beginInde*-int endInde*%: - <et"rns a new string consisting
of all characters from begin inde* "ntil end inde*$e*cl"sive% )ast character is
e*cl"sive
.7% String to)owerCase$ %: -Converts all characters into lower case
.:% String to=pperCase$ %: - Converts all characters into "pper case
.B% String trim$ %: - Eliminates all leading & trailing spaces
(rim means deleting or c"tting (rim method doesnFt remove in the middle spaces
ItFs onl& remove first spaces & last spaces )ast space is removing- it is trail method
44 "nderstanding the strings
class Strings(est
M
p"blic static void main$StringNO args%
M
44 create 0 strings
String s.VP(his is javaPQ
String s/Vnew String$P I like itP%Q
char chNOVMX1X-XoX-XtX-XhX-X"X-XrX-XaX-XiXRQ
String s0V new String$ch%Q
44 displa& the strings
S&stemo"tprintln$PS. V PHs.%Q
S&stemo"tprintln$PS/ V PHs/%Q
S&stemo"tprintln$PS0 V PHs0%Q
44 find no of charecters in s.
S&stemo"tprintln$Plength of S. V PHs.length$ %%Q
44 join / strings
S&stemo"tprintln$PS. joined with s/ V PHs.concat$s/%%Q
44 join 0 strings
S&stemo"tprintln$s.HP at PHs0%Q
55
44 check the starting of s.
boolean *Vs.starts,ith $P(hisP%Q
if$*VVtr"e%
S&stemo"tprintln$Ps. starts with (his P%Q
else
S&stemo"tprintln$PS. does not start with (his P%Q
4K e*tract s"bstring from s. & s0
String pVs/s"bString$2-B%Q
String 6Vs0s"bString$2%Q
S&stemo"tprintln$pH6%Q K4
44change the case of s0
S&stemo"tprintln$P=pper case of s0 V PHs0to=pperCase$ %%Q
S&stemo"tprintln$P)ower case of S0 V PHs0to)owerCase$ %%Q
R
R
!:SpsrSCore javaTjavac Strings(estjava
!:SpsrSCore javaTjava Strings(est
S. V (his is java
S/ V I like it
S0 V 1oth"rai
length of S. V ./
S. joined with s/ V (his is java I like it
(his is java at 1oth"rai
s. starts with (his
=pper case of s0 V 1;('=<AI
)ower case of S0 V poth"rai
44 e6"alit& of strings
class StringE6"al
M
p"blic static void main$String argsNO%
M
String aVP'elloPQ
String bVnew String$P'elloP%Q
if$aVVb%
S&stemo"tprintln$PsameP%Q
else
S&stemo"tprintln$P+ot sameP%Q
R
R
!:SpsrSCore javaTjavac StringE6"aljava
!:SpsrSCore javaTjava StringE6"al
+ot same
,hat is hash code5
56
A% It is a "ni6"e identification n"mber that is allotted b& J?# to the objects
(his n"mber is also called reference n"mber
S.
S.VVS/
<eference n"mbers
S/
./:^V.02
So the o"t p"t is not same VV is onl& compare reference n"mbers
In the above program correct format is if$s.e6"als$s/%%
// ,hat is the difference between VV to and e6"als method while comparing the
strings5
A% VV operator compares onl& the reference of string objects
E6"als method compares the contents of the string objects 'ence the e6"alsF
method gives reliable res"lts
/0 ,hat is string constant pool5
A% It is separate block of memor& where the string objects are stored
Sting s.VZhelloZ
String s/VZhelloZ
./: is cop& to another
Same reference noFs so
./:VV./: String constant pool
If$s.VVs/%
o4p: - e6"al- beca"se J?# does not waste memor&
/7 ,hat is the difference between the following statements5
a% String sVZ'elloZQ
b% String sVnew String$Y'elloZ%Q
A% ,hen the first statement is e*ec"ted J?# searches for the same object in the
string constant pool If the same object is fo"nd there then J?# will create another
reference to the same object- if the same object not fo"nd J?# creates another
object & stores it into string constant pool
,hen the second statement is e*ec"ted J?# alwa&s create a new object
witho"t searching in the string constant pool
(&pes of objects: - .% #"table /% Imm"table
57
./:
.02
'ello
'ello
'ello ./:
.02
.% #"table: -
#"table objects are the objects whose contents can be modified
/% Imm"table: -
Imm"table objects are those objects whose contents can not be modified
44 String class object are imm"table
class Imm"table
M
p"blic static void main$StringNO args%
M
String s.VPhelloPQ
String s/VPhaiPQ
s.Vs.Hs/Q
S&stemo"tprintln$s.%Q
R
R
!:SpsrSCore javaT javac Imm"tablejava
!:SpsrSCore javaT java Imm"table
'ellohai
String b"ffer: - String b"ffer is a m"table- where as string is imm"table
Creating a string b"ffer: -
.% String3"ffer sbVnew String3"ffer$Y'elloZ%Q
/% String3"ffer sbVnew String3"ffer
javalang String3"ffer: -
.% String3"ffer append$*%: - * ma& be int- float- do"ble- char- string or
String3"ffer It will be append to the calling String3"ffer
/% String3"ffer insert$int offset-*%: - * ma& be int- float- do"ble- char- string or
String3"ffer It will be inserted into the String3"ffer at offset Insert method
is insert a val"e
0% String3"ffer delete$int start-int end%: - removes the characters from start to
end
7% String3"ffer reverse$ %: - <everse the character se6"ence in the String3"ffer
:% String toString$ %: -Converting String3"ffer into a string
B% int length$ %: - <et"rns the length of the String3"ffer
,hat is the difference between a string & String3"ffer5
A% String objects are imm"table String3"ffer objects are m"table
(he methods which directl& manip"late the data or not available in string class
S"ch methods are available in String3"ffer class
44 displa& the f"ll name
import javaioKQ
class #"table
M
58
p"blic static void main$StringNO args% throws I;E*ception
M
44 to accept data from ke&board
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$PEnter s"r name : P%Q
String s"rVbrread)ine$ %Q
S&stemo"tprint$PEnter mid name : P%Q
String midVbrread)ine$ %Q
S&stemo"tprint$PEnter last name : P%Q
String lastVbrread)ine$ %Q
44 create String 3"ffer object
String3"ffer sbVnew String3"ffer$ %Q
44 append s"r-last to sb
sbappend$s"r%Q
sbappend$last%Q
44 insert mid after s"r
int nVs"rlength$ %Q
sbinsert$n-mid%Q
44 displa& f"ll name
S&stemo"tprintln$PC"ll name V PHsb%Q
S&stemo"tprintln$PIn reverse VPHsbreverse$ %%Q
R
R
!:SpsrSCore javaTjava #"table
Enter s"r name : S"nil
Enter mid name : W"mar
Enter last name : <edd&
C"ll name V S"nilW"mar<edd&
In reverse V&dde<ram"Wlin"S
,hat is software re6"irement specification $S<S%5
A% S<S is a doc"ment that contains client re6"irements and strategies to f"lfill those
re6"irements S<S is prepared b& project leader
,hat is software design specification$S!S%5
A% S!S is a doc"ment that contains I4; screens- 1S=!; code- !C!Fs dataflow
diagrams- 3lack end data bases etc- it is prepared b& mod"le leader
C- 1ASCA)- C;<(<A+- C;3A) these lang"ages are called proced"ral
lang"ages 3eca"se the& follow proced"re oriented approach
(he lang"ages cHH & java are called object oriented lang"ages 3eca"se the&
follow object oriented approach
.% In proced"re oriented approach a programmer thinks abo"t the problem and
its sol"tion ,e need another approach where the behavior of entire s&stem is
st"died before developing the software
59
$So we can create f"ll fle*ed proced"re s&stems%
/% In proced"re oriented approach when the code si>e e*ceeds .2-222 lines &
before reaching .-22-222 lines- at a partic"lar level the programs are s"ddenl&
loosing control on the code So we need another approach where we can develop
bigger & comple* projects
0% 1rogramming in proced"re oriented approach is completel& "n nat"ral ,e
need another approach where programming refle* h"man being life
3eca"se of the above approaches the& new approach called object orient
approach is developed b& comp"ter scientists It is also called ;;1S $;bject
oriented programming lang"age%
Ceat"res of ;;1S: -
.% ;bject4class: - An object is an&thing b"t e*ists in the real world An object
contains properties & actions 1roperties are represented b& variables- actions are
represented b& methods
A class is a gro"p name which specifies properties & actions of an object A
class also contains variables & methods
/% Encaps"lation: - (aking the data & methods as a single "nit is called
encaps"lation Class is e*ample of encaps"lation It is a protective mechanism (his
main advantage is one class method will mi*ed in another class
0% Abstraction: -'iding "nnecessar& data from the "ser called abstraction
E*: - Car
7% Inheritance: - 1rod"cing new class from e*isting class is called inheritance <e
"sing the code witho"t re writing it is the advantage of inheritance
:% 1ol&morphism: - 1ol& I man&- morphs I forms
(he abilit& to e*ist in different form is called pol&morphism- if same method is
called performing several tasks
B% #essage passing: - Calling a method & passing data to it is called message
passing
,hat is the difference between object oriented programming lang"age & object
based programming lang"age5
A% ;bject oriented programming lang"ages follow all the feat"res of ;;1Fs
E8: - CHH- Java- Small (alk- Sim"la BJ
;bject based programming lang"age follow all the feat"res of ;;1Fs- e*pect
inheritance
E8: - Java script- ?3 script
Class is a model for creating the object based on the class objects is created
Instance of a class: - Instance means ph&sicall& e*ists
;bjects are created b& J?# on heap at r"n time d&namicall& In java e*ec"ting is
created onl& in d&namic memor&- no static memor& in java
60
44 a class & object
class 1erson
M
44 properties instance vars
String nameQ
int ageQ
44 action methods
p"blic void talk$ %
M
S&stemo"tprintln$P'ello I am PHname%Q
S&stemo"tprintln$P#& age is PHage%Q
R
R
class Instance(est
M
p"blic static void main$StringNO args%
M
44 create an obj to person
1erson p.Q
p.Vnew 1erson$ %Q
S&stemo"tprintln$p.hashCode$ %%Q
p.nameVP S"dheer <edd& PQ
p.ageV/0Q
p.talk$ %Q
R
R
!:SpsrSCore javaTjavac Instance(estjava
!:SpsrSCore javaTjava Instance(est
G:BJ0B.
'ello I am S"dheer <edd&
#& age is /0
Instance variables are variables whose cop& is available in the object
,hen the instance variables are not initiali>ed b& the programmer- java compiler
adds some code to the class where it initiali>es (he instance variables with defa"lt
val"es are shown below
Class is also called a data t&pe It is called the "ser data t&pe
!ata (&pe defa"lt val"es
int 2
float 22
do"ble 22
char a space$single space store%
String n"ll
An& class n"ll
61
boolean false
Initiali>ing the instance variables: -Initiali>ing means storing starting val"e
Initiali>ing the instance variables are vario"s t&pes
(&pe.: - ,e can initiali>e one class instance variables in another class
1rivate: - 1rivate is called access specifiers Access specifier is a ke& word that
specifies how to access or read the member of a class or the class it self
(here are 7 access specifiers
1) 1rivate: - private members are not available o"t side the class
2) 1"blic: - p"blic members are available an& where o"t side the class
3) !efa"lt: -!efa"lt members are available o"t side the class
+ote: - If the "ser does not "se an& access specifier then java compiler "ses defa"lt
specifier
4) 1rotected: - protected members are available o"t side the class
1rivate can not be "sed before the class name In t&pe. initiali>ation- if the
instance variables are declared as private- the& are not available to other classes
4K this e*ample demonstrate how to pass an objects to a f"nction & how to ret"rn
object K4
import javaioKQ
class !istance
M
private int feetQ
private float inchesQ
void show!istance$ %
M
S&stemo"tprintln$feetHP-PHinches%Q
R
void set!istance$int ft-float in%
M
fectVftQ
inchesVinQ
R
void get!istance$ % throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$Penter feet : P%Q
feetVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprint$Penter inches : P%Q
inchesVCloatparseCloat$brread)ine$ %%Q
R
!istance add!istance$!istance d/%
M
!istance tempVnew !istance$ %Q
62
tempfeetV2Q
tempinchesVinchesHd/inchesQ
while$tempinchesTV./%
M
tempinchesVtempinches-./Q
tempfeetHHQ
R
tempfeetVtempfeetHfeetHd/feetQ
ret"rn tempQ
R
R
class !istanceAddition
M
p"blic static void main$StringNO args% throws I;E*ception
M
!istance d.Vnew !istance$ %Q
!istance d/Vnew !istance$ %Q
!istance d0Vnew !istance$ %Q
d.get!istance$ %Q
d.show!istance$ %Q
d/get!istance$ %Q
d/show!istance$ %Q
d0Vd.add!istance$d/%Q
S&stemo"tprintln$Pres"lt !istance VP%Q
d0show!istance$ %Q
R
R
!:SpsrSCore javaTjavac !istanceAddition java
!:SpsrSCore javaTjava !istanceAddition
enter feet : .2
enter inches : /2
.2-/22
enter feet : /2
enter inches : .2
/2-.22
res"lt !istance V
0/-B2
(&pe/: - ,e can initiali>e the variables within the class
In t&pe/ initiali>ation ever& object is initiali>ed with same data (his t&pe of
initiali>ation is "sef"l to declare constants
final do"ble 1IV0.7.:9Q
(o declare constants variables we "se final ke& word
(&pe0: - In this t&pe we "se constr"ctors
Constr"ctors: - A constr"ctor is similar to a method ,hich is "sed to initiali>e the
instance variables5
63
.% Constr"ctors name & class name m"st be same
E8: - 1erson$ %
/% A constr"ctor witho"t parameters is called defa"lt constr"ctors A
constr"ctor with one or more parameters is called parameteri>ed
E8: -
44 defa"lt constr"ctor
1erson$ %
M
nameVPS"dheerPQ
ageV/0Q
R
A constr"ctor does not ret"rn an& val"e A constr"ctor is called & e*ec"ted at the
time of creating the object a"tomaticall& A constr"ctor is onl& once for object
E8: -
44 parametari>ed constr"ctor
1erson$String s-int i%
M
nameVsQ
ageViQ
R
1erson p/Vnew 1erson$PS"dheerP-/0%Q
1erson p0Vnew 1erson$PS"nilP-/7%Q
44"se of constr"ctor
class Co"nter
M
int co"ntQ
Co"nter$ %
M
co"ntV2Q
S&stemo"tprintln$PConstr"ctor calledP%Q
R
void showCo"nt$ %
M
S&stemo"tprintln$Pco"nt : PHco"nt%Q
R
void incrementCo"nt$ %
M
co"ntHHQ
R
void decrementCo"nt$%
M
co"nt--Q
R
R
class Constr"ctor!emo
M
64
p"blic static void main$StringNO args%
M
Co"nter c.Vnew Co"nter$ %Q
Co"nter c/Vnew Co"nter$ %Q
c.incrementCo"nt$ %Q
c/incrementCo"nt$ %Q
c/incrementCo"nt$ %Q
c.decrementCo"nt$ %Q
c/decrementCo"nt$ %Q
c.showCo"nt$ %Q
c/showCo"nt$ %Q
R
R
!:SpsrSCore javaTjavac Constr"ctor!emojava
!:SpsrSCore javaTjava Constr"ctor!emo
Constr"ctor called
Constr"ctor called
co"nt : 2
co"nt : .
,hat is the defa"lt constr"ctor & parameteri>ed constr"ctor5
A% !efa"lt constr"ctor is "sed to initiali>e ever& object with same data
1arameteri>ed constr"ctor is "sed to initiali>e each object with different data
,hat is constr"ctor overloading5
A% ,riting / or more constr"ctors with same name with difference in the parameter
is called constr"ctor overloading
#ethods: -
A method represent a gro"p of stats with performs a task A method will have
/ parts
.% #ethod header $or% protot&pe: - It contain name of the method & method
parameters $p.-p/-p0-% & methods ret"rn t&pe
ret"rnt&pe methodname$para.-para/-DD%Q
method parameters are variables which receive data from o"tside
E8: -
ret"rn t&pe data t&pe
void s"m$ %
do"ble s"m$do"ble a-do"ble b%
do"ble s6rt$do"ble *%
do"ble power$ %
long fact$int n"m%
void cal"clate(s*$float sal%
/% #ethod bod&: - It represents a gro"p of statements which perform the task
E8: -method bod&
M
65
StatementsQ
R
+ote: - If a method ret"rns some val"e we sho"ld write ret"rn statement in the
bod&
E8: -
ret"rn *Q
ret"rn .Q
ret"rn *H&Q
ret"rn objQ
ret"rn arrQ
+ote: - ret"rn statement can ret"rn onl& one entit& or . val"e it ret"rns
A method can not ret"rn more than one val"e
44 "nderstanding the methods
class Sample
M
44 instance vars
do"ble d.-d/Q
44 1ara constr"ctor
Sample$do"ble a- do"ble b%
M
d.VaQ
d/VbQ
R
44 a method
void s"m$ %
M
do"ble d0Vd.Hd/Q
S&stemo"tprintln$PS"m V PHd0%Q
R
R
class #ethod(est
M
p"blic static void main$StringNO args%
M
44 sample object
Sample sVnew Sample$/2-02:%Q
44 call the method
ss"m$ %Q
R
R
!:SpsrSCore javaTjavac #ethod(estjava
!:SpsrSCore javaTjava #ethod(est
S"m V :2:
66
)ocal variable: - It is a variable that is declared locall& within a method or
constr"ctor (he scope of that variable is limited with in method onl& or onl& to that
constr"ctor where it is declared
#ethods in java: -
.% Static methods
/% Instance methods
a% Accessor methods
b% #"tator methods
/% Cactor& methods
.% Static methods: -
(hese are the methods which donFt act "pon the instance variables
(hese are declared as static
satic do"ble s"m$do"ble d.-do"ble d/%
Static methods are called "sing object namemethod name
Static methods the& can not read instance variables wh& beca"se the reason
is J?# first e*ec"te static methods & after that it creates a object ne*t% So at the
time of calling static method instance variables are not available Static methods can
acts as static variables It sho"ld be declared as static
,hat is different between instance variables and static variables5
A% .% Instance variables are a variable whose separate cop& is available to each
object of the class So an& modification to the instance variable in one object will not
modif& the other objects
Static variables are variables whose single cop& is shared b& all the objects of
the class If the static variable val"e is modified- it affects the val"e in all the objects
/% ,here instance variables & static variables are stored
Instance variables are created on heap
Static variables are stored on method area
+ote: -
.% Static blocks- static methods & static variables are stored in method area & onl&
one cop& is shared b& all objects
/% A static block represents a block of statements declared as static
0% J?# e*ec"te in the following order
a% static block
b% static method
c% instance method
a% static block: -
44 static test
class Static(est
67
M
static
M
S&stemo"tprintln$Pstatic blockP%Q
S&steme*it$2%Q
R
R
$or%
44 static test
class Static(est.
M
static
M
S&stemo"tprintln$Pstatic blockP%Q
44 S&steme*it$2%Q
R
p"blic static void main$StringNO args%
M
S&stemo"tprintln$Pstatic methodP%Q
R
R
!:SpsrSCore javaTjavac Static(est.java
!:SpsrSCore javaTjava Static(est.
static block
static method
44 static block
class Static3lock
M
static int aV0Q
static int bQ
static void show$int *%
M
S&stemo"tprintln$P* V PH*%Q
S&stemo"tprintln$Pa V PHa%Q
S&stemo"tprintln$Pb V PHb%Q
R
static
M
S&stemo"tprintln$Pstatic block initiali>edP%Q
bVaK7Q
R
R
class Static.!emo
M
p"blic static void main$StringNO args%
M
68
Static. obVnew Static.$ %Q
obshow$.22%Q
R
R
!:SpsrSCore javaTjavac Static.!emojava
!:SpsrSCore javaTjava Static.!emo
static block initiali>ed
* V .22
a V 0
b V ./
b% static method: -
44 static method
class Static
M
int *Q
static int &Q
static void show&$ %
M
S&stemo"tprintln$&%Q
R
void show*$ %
M
S&stemo"tprintln$*%Q
R
R
class Static#ethod
M
p"blic static void main$StringNO args%
M
Static obVnew Static$ %Q
ob&V/22Q
ob*V.22Q
obshow*$ %Q
obshow&$ %Q
R
R
!:SpsrSCore javaTjavac Static#ethodjava
!:SpsrSCore javaTjava Static#ethod
.22
/22
c% Instance methods: - (hese are those methods which act "pon instance variables of
a class Instance methods are called "sing object name
method+ame$ %
69
E8: - ss"m$ %
A method will ret"rn the res"lt from where it is called
E8: -do"ble s"m$ %
M
do"ble d0Vd.Hd/Q
ret"rn d0Q
R
calling
do"ble resVss"m$ %Q
S&stemo"tprintln$Pres"lt V PHres%Q
(hese Instance methods are two t&pes
a% Accessor methods: - Accessor methods read the instance variables
(he& donFt modif& the instance variables
b% #"tator methods: - (hese methods not onl& read the instance
variables b"t also modif& them
44 accessor & m"tator methods
class 1erson
M
44 instance variables
String nameQ
int ageQ
char se*Q
44 constr"ctor to initiali>e
1erson$String name-int age-char se*%
M
thisnameVnameQ
thisageVageQ
thisse*Vse*Q
R
44 accessor method
void displa&$ %
M
S&stemo"tprintln$PnameV PHname%Q
S&stemo"tprintln$Page VPHage%Q
S&stemo"tprintln$Pse* VPHse*%Q
R
44 m"tator method
1erson modif&$1erson obj%
M
objnameVP)athaPQ
--objageQ
objse*VXfXQ
70
ret"rn objQ
R
R
class #"tate
M
p"blic static void main$String argsNO%
M
1erson p.Vnew 1erson$Ps"dheerP-/0-XmX%Q
p.displa&$ %Q
1erson p/Vp.modif&$p.%Q
p/displa&$ %Q
1erson p0Vp.modif&$new 1erson$Ps"nilP-02-XmX%%Q
p0displa&$ %Q
R
R
!:SpsrSCore javaTjavac #"tatejava
!:SpsrSCore javaTjava #"tate
nameV s"dheer
age V/0
se* Vm
nameV )atha
age V//
se* Vf
nameV )atha
age V/9
se* Vf
(his: - this is a ke& word that refers to present class object
It refers to present class instance variables
present class methods
present class constr"ctor
44this demo
class Sample
M
int nQ
Sample$ %
M
this$::%Q 44 present class constr"ctor
thisdispla&$ %Q 44 present class method
R
Sample$int n%
M
thisnVnQ 44 present class variable
R
void displa&$ %
71
M
S&stemo"tprintln$PnVPHn%Q
R
R
class (his!emo
M
p"blic static void main$String argsNO%
M
Sample sVnew Sample$ %Q
sdispla&$ %Q
R
R
!:SpsrSCore javaTjavac (his!emojava
!:SpsrSCore javaTjava (his!emo
nV::
nV::
0% Cactor& methods: - A factor& method is a method that ret"rns an object of the
class to which to belong
E8: -
+"mberCormat objVnew +"mberCormatgetInstance$ %Q
All factor& methods are static methods 3"t all static methods are not factor&
methods
In how man& wa&s can &o" create on object in java5
A% ,a&s of creating on object: -
.% =sing new operator
Emplo&ee objVnew Emplo&ee$ %Q
/% "sing factor& method
+"mberCormat objVnew +"mberCormatgetInstance$ %Q
0% "sing newInstance$ % method
Class cVClassfor+ame$YEmplo&eeZ%Q
Cor name method will store emplo&ee that class name in an object
(his is represented b& c
7% =sing cloning$ %
Cloning means creating bitwise e*act cop& of an e*isting object
44 find the area of circle
import javate*tKQ
class Circle
M
p"blic static void main$String argsNO%
M
do"ble rV:J:Q
final do"ble 1IV$do"ble%//4JQ
do"ble areaV1IKrKrQ
72
S&stemo"tprintln$Parea is VPHarea%Q
44 create +"mberCormat obj
+"mberCormat objV +"mberCormatget+"mberInstance$%Q
44 store format in to obj
objset#a*im"mCraction!igits$/%Q
objset#inim"mInteger!igits$7%Q
44 appl& this format into area
String strVobjformat$area%Q
44 displa& formatted area
S&stemo"tprintln$Pformatted area V PHstr%Q
R
R
!:SpsrSCore javaTjavac Circlejava
!:SpsrSCore javaTjava Circle
area is V.209.2J.7/G:J.70
formatted area V .2-09.2J
,e can pass to arra&: -
int N O m&#ethod$int arrN O%
int N O N O m&#ethod$int arrN O N O%
ret"rn arrQ
Arra& works like a reference similar to that of an object
ANiONjO H bNiONjO tempNiONjO
.% <ead matri*. elements from ke&board
=sing getArra&$ %
/% <ead matri*/ elements from ke&board
=sing getArra&$ %
0% Add both matrices
=sing findS"m$ %
7% !ispla& the s"m matri*
=sing p"tArra&$ %
44 find s"m of / matrices
import javaioKQ
class Arr
M
44 instance variables
int r-cQ
int arrNONOQ
44 constr"ctor
Arr$int r-int c%
M
thisrVrQ
thiscVcQ
arrVnew intNrONcOQ
R
73
44 to receive matri* elements from ke& board
int NONO getArra&$ % throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
for$int iV2Qi[rQiHH%
M
for$int jV2Qj[cQjHH%
M
S&stemo"tprint$PEnter element : P%Q
arrNiONjOVIntegerparseInt$brread)ine$ %%Q
R
R
ret"rn arrQ
R
44 to find s"m of / matrices
int NONO findS"m$int aNONO-int bNONO%
M
int tempNONOVnew intNrONcOQ
for$int iV2Qi[rQiHH%
M
for$int jV2Qj[cQjHH%
M
tempNiONjOVaNiONjOHbNiONjOQ
R
R
ret"rn tempQ
R
44 displa& the s"m matri*
void p"tArra&$int resNONO%
M
for$int iV2Qi[rQiHH%
M
for$int jV2Qj[cQjHH%
M
S&stemo"tprint$resNiONjOHPStP%Q
R
S&stemo"tprintln$PSnP%Q
R
R
R
class #atri*S"m
M
p"blic static void main$StringNO args% throws I;E*ception
M
44 create / objects to arr
Arr obj.Vnew Arr$0-0%Q
74
Arr obj/Vnew Arr$0-0%Q
44 take 0 arra& referances
int *NONO-&NONO->NONOQ
S&stemo"tprintln$Penter matri* . :P%Q
*Vobj.getArra&$ %Q
S&stemo"tprintln$Penter matri* / :P%Q
&Vobj/getArra&$ %Q
>Vobj.findS"m$*-&%Q
S&stemo"tprintln$PSn (he s"m matri* is : P%Q
obj/p"tArra&$>%Q
R
R
!:SpsrSCore javaTjavac #atri*S"mjava
!:SpsrSCore javaTjava #atri*S"m
enter matri* . :
Enter element : .
Enter element : /
Enter element : 0
Enter element : 7
Enter element : :
Enter element : B
Enter element : J
Enter element : G
Enter element : 9
enter matri* / :
Enter element : .
Enter element : /
Enter element : 0
Enter element : 7
Enter element : :
Enter element : B
Enter element : J
Enter element : G
Enter element : 9
(he s"m matri* is :
/ 7 B
G .2 ./
.7 .B .G
44 find prod"ct of / matrices
import javaioKQ
class Arr.
M
44 instance variables
int r.-c.Q
int arr.NONOQ
75
44 constr"ctor
Arr.$int r.-int c.%
M
thisr.Vr.Q
thisc.Vc.Q
arr.Vnew intNr.ONc.OQ
R
44 to receive matri* elements from ke& board
int NONO getArra&$ % throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
for$int iV2Qi[r.QiHH%
M
for$int jV2Qj[c.QjHH%
M
S&stemo"tprint$PEnter element :P%Q
arr.NiONjOVIntegerparseInt$brread)ine$ %%Q
R
R
ret"rn arr.Q
R
R
class Arr/
M
44 instance variables
int r/-c/Q
int arr/NONOQ
44 constr"ctor
Arr/$int r/-int c/%
M
thisr/Vr/Q
thisc/Vc/Q
arr/Vnew intNr/ONc/OQ
R
44 to receive matri* elements from ke& board
int NONO getArra&$ % throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
for$int iV2Qi[r/QiHH%
M
for$int jV2Qj[c/QjHH%
M
S&stemo"tprintln$PEnter element :P%Q
arr/NiONjOVIntegerparseInt$brread)ine$ %%Q
R
76
R
ret"rn arr/Q
R
R
class 1rod
M
int r.-c/-c.Q
1rod$int r.-int c/-int c.%
M
thisr.Vr.Q
thisc/Vc/Q
thisc.Vc.Q
R
44 to find 1rod of / matrices
int NONO find1rod$int aNONO-int bNONO%
M
int tempNONOVnew intNr.ONc/OQ
for$int iV2Qi[r.QiHH%
M
for$int jV2Qj[c/QjHH%
M
for$int kV2Qk[c.QkHH%
M
tempNiONjOHVaNiONkOKbNkONjOQ
R
R
R
ret"rn tempQ
R
44 displa& the 1rod matri*
void p"tArra&$int resNONO%
M
for$int iV2Qi[r.QiHH%
M
for$int jV2Qj[c/QjHH%
M
S&stemo"tprint$resNiONjOHPStP%Q
R
S&stemo"tprintln$PSnP%Q
R
R
R
class #atri*1rod
M
p"blic static void main$StringNO args% throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
77
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprintln$PEnter first matri* r.-c.P%Q
int r.VIntegerparseInt$brread)ine$ %%Q
int c.VIntegerparseInt$brread)ine$ %%Q
S&stemo"tprintln$PEnter second matri* r/-c/P%Q
int r/VIntegerparseInt$brread)ine$ %%Q
int c/VIntegerparseInt$brread)ine$ %%Q
if$c.VVr/%
M
44 create 0 objects to arr
Arr. obj.Vnew Arr.$r.-c.%Q
Arr/ obj/Vnew Arr/$r/-c/%Q
1rod pVnew 1rod$r.-c/-c.%Q
44 take 0 arra& referances
int *NONO-&NONO->NONOQ
S&stemo"tprintln$PSn enter matri* . :P%Q
*Vobj.getArra&$ %Q
S&stemo"tprintln$PSn enter matri* / :P%Q
&Vobj/getArra&$ %Q
>Vpfind1rod$*-&%Q
S&stemo"tprintln$PSn (he prod matri* is : P%Q
pp"tArra&$>%Q
R
else
S&stemo"tprintln$Pcan not prod"ct of matri*P%Q
R
R
!:SpsrSCore javaTjavac #atri*1rodjava
!:SpsrSCore javaTjava #atri*1rod
Enter first matri* r.-c.
0
/
Enter second matri* r/-c/
/
0
enter matri* . :
Enter element : .
Enter element : /
Enter element : 0
Enter element : 7
Enter element : :
Enter element : B
enter matri* / :
Enter element : .
Enter element : /
Enter element : 0
78
Enter element : 7
Enter element : :
Enter element : B
(he prod matri* is :
9 ./ .:
.9 /B 00
/9 72 :.
44 write a method to swap to given val"es
import javaioKQ
class Swap
M
int a-b-cQ
Swap$int *-int &%
M
thisaV*Q
thisbV&Q
cVaQ
aVbQ
bVcQ
S&stemo"tprintln$PaVPHaHPSt bVPHb%Q
R
R
class Swap#ethod
M
p"blic static void main$StringNO args% throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$Penter a val"e :P%Q
int aVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprint$Penter b val"e : P%Q
int bVIntegerparseInt$brread)ine$ %%Q
S&stemo"tprintln$Pbefore swappingP%Q
S&stemo"tprintln$PaVPHaHPStbVPHb%Q
S&stemo"tprintln$PAfter swappingP%Q
Swap sVnew Swap$a-b%Q
R
R
!:SpsrSCore javaTjavac Swap#ethodjava
!:SpsrSCore javaTjava Swap#ethod
enter a val"e : /:
enter b val"e : :B
before swapping
aV/: bV:B
After swapping
79
aV:B bV/:
,hat are class variables5 : -
A% Static variables are also called class variables
44 relation between two classes & class one obj
class ;ne
M
int *Q
(wo tQ
;ne$(wo t%
M
thistVtQ
*V.2Q
R
void show$ %
M
S&stemo"tprintln$P*V PH*%Q
tshow$ %Q
S&stemo"tprintln$P&V PHt&%Q
R
R
class (wo
M
int &Q
(wo$ %
M
&V//Q
R
void show$ %
M
S&stemo"tprintln$P&V PH&%Q
R
R
class <elate
M
p"blic static void main$String argsNO%
M
(wo obj/Vnew (wo$ %Q
;ne obj.Vnew ;ne$obj/%Q
obj.show$ %Q
R
R
!:SpsrSCore javaTjavac <elatejava
!:SpsrSCore javaTjava <elate
*V .2
80
&V //
&V //
,hat is object graph5
A% A graph connecting several objects is called object graph
8
L
,e can create class /Fs references as an instance variable in class one ,hen the
object is created from obj. we can refer to all the members of obj/ (his is
advantage of giving relationship
Inner class: -
Inner class is a class written in another class It is a sec"rit& mechanism
,e can "se private in inner class 1rivate ke&word can be "sed before inner class
onl& Cire wall means optical that checks the a"thori>ation of "ser
;"t side programmer
Cire wall
44 inner class demo
class 3ankAcct
M
private do"ble balQ
3ankAcct$do"ble b%
M
balVbQ
R
void start$do"ble r%
M
Interest inVnew Interest$r%Q
incalc"lateInterest$ %Q
81
inner class
R
private class Interest
M
private do"ble rateQ
Interest$do"ble r%
M
rateVrQ
R
void calc"lateInterest$ %
M
S&stemo"tprintln$P3alance V PHbal%Q
do"ble interestVbalKrate4.22Q
S&stemo"tprintln$Pinterest V PHinterest%Q
balHVinterestQ
S&stemo"tprintln$P+ew 3alance V PHbal%Q
R
R
R
class Inner!emo
M
p"blic static void main$String argsNO%
M
3ankAcct acco"ntVnew 3ankAcct$/2222%Q
acco"ntstart$J:%Q
R
R
!:SpsrSCore javaTjavac Inner!emojava
!:SpsrSCore javaTjava Inner!emo
3alance V /22222
interest V .:222
+ew 3alance V /.:222
.% Inner class is a written within another class
/% It is a sec"rit& mechanism
0% Inner class in hidden in o"ter class from other classes
7% ;nl& inner class can be EprivateF
:% An object to inner class can not be created in an& other class
B% An object to inner class can be created onl& in its o"ter class
J% ;"ter class object and inner class objects are created separatel& in memor&
G% ;"ter class members are available to inner class
9% Inner class object contains an additional invisible field Ethis c2F that contains
o"ter class reference
82
bal
rate
thisc2
3ank acct
Interest
.2% if same names are "sed for members -then o"ter class members can be
referenced in inner class as: o"ter classthismemberQ
E*: -bankAcctthisbal
Inner class members are referenced as: thismember
E*: - thisbal
Inheritance: - Creating new class from e*isting classes s"ch that all the feat"res of
e*isting classes are available to the new classes is called inheritance Alread& e*isting
class is called s"per class & prod"ced class is called s"b class
S&nta*: -
class s"bclass e*tends s"per class
44 inheritance e*ample of (eacher & st"dent class
class (eacher
M
int idQ
String nameQ
String addressQ
float salQ
void setId$int id%
M
thisidVidQ
R
int getId$ %
M
ret"rn idQ
R
void set+ame$String name%
M
thisnameVnameQ
R
String get+ame$ %
M
ret"rn nameQ
R
void setAddress$String address%
M
thisaddressVaddressQ
R
String getAddress$ %
M
ret"rn addressQ
R
83
void setSal$float sal%
M
thissalVsalQ
R
float getSal$ %
M
ret"rn salQ
R
R
!:SpsrSCore javaTjavac (eacherjava
44 inheritance e*ample of (eacher & st"dent class
class St"dent e*tends (eacher
M
int marksQ
void set#arks$int marks%
M
thismarksVmarksQ
R
int get#arks$ %
M
ret"rn marksQ
R
R
!:SpsrSCore javaTjavac St"dentjava
44 inheritance e*ample of (eacher & st"dent class
class St"dent!emo
M
p"blic static void main$String argsNO%
M
44 create st"dent class object
St"dent sVnew St"dent$ %Q
44 store data into s
ssetId$.22%Q
sset+ame$PS"dheerP%Q
ssetAddress$P+aghiredd& 1alli- A1- A(1P%Q
sset#arks$BB/%Q
44 get data forms
S&stemo"tprintln$PI! V PHsgetId$ %%Q
S&stemo"tprintln$P+ame V PHsget+ame$ %%Q
S&stemo"tprintln$PAddress V PHsgetAddress$ %%Q
S&stemo"tprintln$P#arks V PHsget#arks$ %%Q
R
R
84
!:SpsrSCore javaTjavac St"dent!emojava
!:SpsrSCore javaTjava St"dent!emo
I! V .22
+ame V S"dheer
Address V +aghiredd& 1alli- A1- A(1
#arks V BB/
<ed"ndanc& of the code is a draw back of programming
E*tends is ke& word "sed in inheritance (he s"bclass object contains object
contain a cop& of s"per class object in it ,e cal create on object to onl& s"b class
,hat is the advantage of inheritance5
A% In inheritance a programmer can re "se alread& developed code 3eca"se of these
developing new programs will become eas& (his increases prod"ctivit& of
programmer & hence the over all prod"ctivit& of the compan& or organi>ation will
also increase
44 if s"per has a defa"lt constr"cter
class ;ne
M
;ne$ %
M
S&stemo"tprintln$Ps"perP%Q
R
R
class (wo e*tends ;ne
M
(wo$ %
M
S&stemo"tprintln$Ps"bP%Q
R
R
class Const
M
p"blic static void main$String argsNO%
M
(wo tVnew (wo$ %Q
R
R
!:SpsrSCore javaTjavac Constjava
!:SpsrSCore javaTjava Const
s"per
s"b
S"per class defa"lt constr"ctor is available to s"b class b& defa"lt
S"per class parameteri>ed constr"ctor is not available to s"b class b& defa"lt S"per
is a ke& word that refers to s"per class Cor e*ample
85
44 s"per "sed s"per vars- methods & constr"ctors
class ;ne
M
int *Q
;ne$int *%
M
this*V*Q
R
void show$ %
M
S&stemo"tprintln$P* V PH*%Q
R
R
class (wo e*tends ;ne
M
int &Q
(wo$int a-int b%
M
s"per$a%Q 44 $or% *VaQ
&VbQ
R
void show$ %
M
S&stemo"tprintln$P* V PHs"per*%Q
S&stemo"tprintln$P& V PH&%Q
R
R
class Const.
M
p"blic static void main$String argsNO%
M
(wo tVnew (wo$.2-/7%Q
tshow$ %Q
R
R
!:SpsrSCore javaTjavac Const.java
!:SpsrSCore javaTjava Const.
* V .2
& V /7
S"per ke& word is "sed in s"b class onl& Calling constr"ctor first declare the
s"per
1ol&morphism: - It represents man& forms If a method performs several tasks- it is
called pol&morphism (he& are / t&pes
.% !&namic pol&morphism /% Static pol&morphism
86
.% !&namic pol&morphism: -
(he pol&morphism e*hibited at r"n time is called d&namic pol&morphism In
this d&namic pol&morphism a method call is linked with method bod& at the time of
r"nning the program b& J?# (his is also known as d&namic binding or r"n time
pol&morphism
E8: -
#ethod over loading: - ,riting / or more methods with the same name- b"t with a
difference in the method signat"res is called method over loading
In method over loading J?# "nderstand in the method is called depending
"pon the difference in the method signat"re (he difference ma& be d"e to the
following
.% (here is a difference in the no of parameters
void add$int a-int b%
void add$int a-int b-int c%
/% (here is a difference in the data t&pes of parameters
void add$int a-float b%
void add$do"ble a-do"ble b%
0% (here is a difference in the se6"ence of parameters
void swap$int a-char b%
void swap$char a-int b%
An& one of the above differences will make method signat"re to be different- hence
J?# can identifies methods "ni6"el&
#ethod over loading is an e*ample for d&namic pol&morphism
44 s"m of / n"mbers & s"m of 0 n"mbers
class Sample
M
void add$int a-int b%
M
S&stemo"tprintln$Ps"m of twoVPH$aHb%%Q
R
void add$int a-int b-int c%
M
S&stemo"tprintln$Ps"m of threeVPH$aHbHc%%Q
R
R
class 1ol&
M
p"blic static void main$StringNO args%
M
Sample sVnew Sample$ %Q
sadd$/2-/:%Q
87
sadd$/2-/:-02%Q
R
R
!:SpsrSCore javaTjavac 1ol&java
!:SpsrSCore javaTjava 1ol&
s"m of twoV7:
s"m of threeVJ:
,hat is method signat"re5
A% #ethod name & its parameters is called method signat"re
#ethod overriding: - writing / or more methods in s"per & s"b classes with same
name & same signat"res is called method overriding In method overriding J?#
e*ec"tes a method depending on the data t&pe of the object
#ethod overriding is an e*ample for d&namic pol&morphism
44d&namic pol&morphism
class ;ne
M
void calc"late$do"ble *%
M
S&stemo"tprintln$PS6"are val"e VPH$*K*%%Q
R
R
class (wo e*tends ;ne
M
void calc"late$do"ble *%
M
S&stemo"tprintln$PS6"are root VPH#aths6rt$*%%Q
R
R
class 1ol&.
M
p"blic static void main$String argsNO%
M
(wo tVnew (wo$ %Q
tcalc"late$.B%Q
R
R
!:SpsrSCore javaTjavac 1ol&.java
!:SpsrSCore javaTjava 1ol&.
S6"are root V72
,hat is the difference between method over loading & method overriding5
88
A% #ethod over loading: - ,riting / or more methods with the same name- b"t with
a difference in the method signat"res is called method over loading In method over
loading J?# "nderstand in the method is called depending "pon the difference in
the method signat"re
#ethod overriding: - writing / or more methods in s"per & s"b classes with same
name & same signat"res is called method overriding In method overriding J?#
e*ec"tes a method depending on the data t&pe of the object
Achieving method overloading & method overriding "sing instance methods is an
e*ample of d&namic pol&morphism
Achieving method overloading & method overriding "sing instance methods
is an e*ample of d&namic pol&morphism
/% Static pol&morphism: - (he pol&morphism e*hibited at compile time is called
Static pol&morphism 'ere the compiler knows which method is called at the
compilation (his is also called compile time pol&morphism or static binding
Achieving method overloading & method overriding "sing
.% private /% static 0% final methods- is an e*ample of Static pol&morphism
+ote: - A final method is a method written in a final class A class is declared as a
final is called final class
E*: - final class A
+ote: - final ke& word before class name prevents inheritance ,e can create s"b
classes to a final class
E*: - final class A
class 3 e*tends A 44 invalid
,e can not override final methods- onl& final methods overloading
Converting . data t&pe into another data t&pe is called casting
,hat is final5
A% .% final is "sed to declare Constance
E*: - final do"ble 1I 0.7.:9
/% final ke& word is "sed to prevent inheritance
E*: - final class A
class 3 e*tends A 44 invalid
!ata t&pe: - A data t&pe represents t&pes of data stored into a variable (hese are /
t&pes
.% 1rimitive data t&pe: - $f"ndamental data t&pes%
(hese are represents single val"esQ methods are not available to handle them
E*: - char- b&te- short- int- long- float- do"ble- 3oolean
/% Advanced data t&pes: - $<eferenced data t&pes%
(hese are represents several val"e methods also available to handle them
E*: - int arra&- an& classes $e*: emplo&ee- stringDD%
,hat is difference between primitive & advanced data t&pes5
89
A% .% Casting can be "sed to convert one primitive data t&pe in to another primitive
data t&pe
/% Casting can be "sed to convert one advanced data t&pe into another advanced
data t&pe
0% Casting can not be "sed to convert a primitive data t&pe into an advanced data
t&pe and vice versa $means reverse also%
Cor this p"rpose we can "se wrapper classes
Casting primitive data t&pes: -
a% ,idening: - Casting a lower data t&pe into a higher data t&pe is called widening
E8: - char- b&te- short- int- long- float- do"ble
)ower ------------higher
i% char chVFAFQ
int nV$int%chQ
ii% int n"mV.:Q
float fV$float%n"mQ
In widening no digits or precision are lost
b% +arrowing: - Converting a higher data t&pe into lower t&pe is called narrowing
E8: - i% int nVBBQ
Char chV$char%nQ
ii% do"ble dV././07Q
int nV$int%dQ
In narrowing precision or digits are lost (his is called e*plicit casting
Casting advanced data t&pes:
,e can cast advanced data t&pes provide there is relationship between the
classes b& the wa& of inheritance

44Casting advanced data t&pes:
class ;ne
M
void show.$ %
M
S&stemo"tprintln$PS"per classP%Q
R
R
class (wo e*tends ;ne
M
void show/$ %
M
S&stemo"tprintln$PS"b classP%Q
R
R
class Cast
M
p"blic static void main$String argsNO%
90
M
44 s"per class reference to refer to s"per class object
;ne oQ
oVnew ;ne$ %Q
oshow.$ %Q
R
R
!:SpsrSCore javaTjavac Castjava
!:SpsrSCore javaTjava Cast
S"per class
In s"per reference is "sed to refer to s"per class object (he programmer can refer
to onl& s"per class members b"t not the s"b class members

class Cast
M
p"blic static void main$String argsNO%
M
44 s"b class reference to refer to s"b class object
;ne oVnew (wo$ %Q
(wo tV$(wo%oQ
tshow.$ %Q
tshow/$ %Q
R
R
!:SpsrSCore javaTjavac Castjava
!:SpsrSCore javaTjava Cast
S"per class
S"b class
In s"b class reference is "sed s"bclass objectQ the programmer can access all the
members of both the s"per class& s"b class

class Cast
M
p"blic static void main$String argsNO%
M
44 s"per class reference to refer to s"b class object
;ne oQ
oVnew (wo$ %Q
oshow.$ %Q
R
R
!:SpsrSCore javaTjavac Castjava
91
!:SpsrSCore javaTjava Cast
S"per class
,hat is generali>ation & speciali>ation5
A% #oving back from s"b class to s"per class is called generali>ation Casting a s"b
class t&pe into a s"per class is called a "p casting or widening
Coming down from s"per class to s"b class is called speciali>ation Casting a
s"per class t&pe into s"b class t&pe is called narrowing or down casting
In widening we can access s"per class methods In widening we can access
s"b class methods In widening- we can not access s"b class methods "nless the
override s"per class methods

class Cast
M
p"blic static void main$String argsNO%
M
44 s"b class reference to refer to s"per class object
;ne oVnew (wo$ %Q
(wo tV$(wo%oQ
tshow.$ %Q
tshow/$ %Q
R
R
!:SpsrSCore javaTjavac Castjava
!:SpsrSCore javaTjava Cast
S"per class
S"b class
+arrowing "sing s"per class object can not access either s"per class methods
or s"b class methods
+arrowing "sing s"b class object will make the programmer to access are the
members of s"per class as well as s"b class
,hat is widening & narrowing5
A% ,idening: - Casting a lower data t&pe into a higher data t&pe is called widening
In widening no digits or precision are lost Casting a s"b class t&pe into a s"per class
is called an "p casting or widening In widening we can access s"per class methods
In widening we can access s"b class methods In widening "s can not access s"b
class methods "nless the override s"per class methods
+arrowing: - Converting a higher data t&pe into lower t&pe is called narrowing
In narrowing precision or digits are lost (his is called e*plicit casting Casting a
s"per class t&pe into s"b class t&pe is called narrowing or down casting +arrowing
"sing s"per class object can not access either s"per class methods or s"b class
methods +arrowing "sing s"b class object will make the programmer to access are
the members of s"per class as well as s"b class
92
Abstract methods: - ,hen different objects need different implementation of same
method then we can not write bod& for the method S"ch a method is called abstract
method It is a method witho"t bod& Abstract method sho"ld be declared with
abstract ke& word A class that contains abstract methods is called abstract class
Abstract class sho"ld also be declared "sing the abstract ke& word ,e can not
create object to abstract class

44 abstract class
abstract class Car
M
44 ever& car will have a regno
int regnoQ
44 to store regno
Car$int r%
M
regnoVrQ
R
44 ever& car will have f"el tank
44 mechanism to fill the f"el is same for all cars
void fill(ank$ %
M
S&stemo"tprintln$P=se car We&P%Q
S&stemo"tprintln$PAnd fill the tankP%Q
R
4K ever& car will have steering 3"t different cars will have different steering
mechanisms K4
abstract void steering$int direction%Q
4K ever& cars will have breaks 3"t different cars will have different breaking
mechanisms K4
abstract void braking$int force%Q
R
!:SpsrSCore javaTjavac Carjava
44 this is a concrete s"b class of car class
class #ar"thi e*tends Car
M
#ar"thi$int regno%
M
s"per$regno%Q
R
void steering$int direction%
M
S&stemo"tprintln$P#ar"thi "ses man"al steeringP%Q
S&stemo"tprintln$Pplease drive the carP%Q
R
void braking$int force%
M
93
S&stemo"tprintln$P#ar"thi "ses h&dra"lic breaksP%Q
S&stemo"tprintln$Pappl& breaks & stop the carP%Q
R
R
!:SpsrSCore javaTjavac #ar"thijava
44 this is a concrete s"b class of car class
class Santro e*tends Car
M
Santro$int regno%
M
s"per$regno%Q
R
void steering$int direction%
M
S&stemo"tprintln$PSantro "ses power steeringP%Q
S&stemo"tprintln$PStart itP%Q
R
void braking$int force%
M
S&stemo"tprintln$PSantro "ses gas breaksP%Q
S&stemo"tprintln$P stop itP%Q
R
R
!:SpsrSCore javaTjavac Santrojava
44 "sing the car
class =seCar
M
p"blic static void main$StringNO args%
M
44 create a mar"thi & Santro obj
#ar"thi mVnew #ar"thi$BBBB%Q
Santro sVnew Santro$////%Q
44 create a reference of car class
Car refQ
44 "se ref to refer to mar"thi
refVmQ
reffill(ank$ %Q
refsteering$/%Q
refbraking$/22%Q
44 "se ref to refer to Santro
refVsQ
reffill(ank$ %Q
refsteering$7%Q
refbraking$722%Q
R
94
R
!:SpsrSCore javaTjavac =seCarjava
!:SpsrSCore javaTjava =seCar
=se car We&
And fill the tank
#ar"thi "ses man"al steering
please drive the car
#ar"thi "ses h&dra"lic breaks
appl& breaks & stop the car
=se car We&
And fill the tank
Santro "ses power steering
Start it
Santro "ses gas breaks
stop it
<eference statement is "sed to element the representation of statements
.% An abstract class is class with >ero or more abstract methods
/% An abstract class contains instance variables & concrete methods in addition to
abstract methods
0% It is not possible to create objects to abstract class
7% 3"t we can create a reference of abstract class t&pe
:% All the abstract methods of the abstract class sho"ld be implemented in its s"b
classes
B% If an& method is not implemented- then that s"b class sho"ld be declared as
EabstractF
J% Abstract class reference can be "sed to refer to the objects of its s"b classes
G% Abstract class references cannot refer to the individ"al methods of s"b classes
9% A class can not be both EabstractF & EfinalF
E8: - final abstract class A 44 invalid
'ow can enforce discipline in the programmers to implement onl& &o"r class
feat"res5
A% 3& writing an abstract class or an interface I can enforce
Interfaces: - An interface is a specification of method properties ,e can not create
on object in interface ,e create reference variable for interface
Interface class
Implementation class implementation class
95
!river: - A driver represents implementation classes of an interface

A1I
44 interface e*ample
interface #&Inter
M
void connect$ %Q
R
class ;racle!3 implements #&Inter
M
p"blic void connect$ %
M
S&stemo"tprintln$PConnecting to oracle data base P%Q
R
R
class S&base!3 implements #&Inter
M
p"blic void connect$ %
M
S&stemo"tprintln$PConnecting to S&base data base P%Q
R
R
class !ata3ase
M
p"blic static void main$String argsNO% throws E*ception
M
44 accapt "ser dats base name thro"gh command line arg"ments store in an
object c
Class cVClassfor+ame$argsN2O%Q
44 create another object to the class whose name is in c
#&Inter miV$#&Inter%cnewInstance$ %Q
44 call convert method of the object
miconnect$ %Q
96
;racle
S&3ase
#& S6l
void connect$ %Q
M
--------
R
R
R
!:SpsrSCore javaTjavac !ata3asejava
!:SpsrSCore javaTjava !ata3ase S&base!3
Connecting to s&base data base
!:SpsrSCore javaTjava !ata3ase ;racle!3
Connecting to oracle data base
I
II
.% An interface is a specification of method protot&pe
/% An interface contains 2 or more abstract methods
0% All the methods as the interface are p"blic and abstract b& defa"lt
7% An interface can also contain variables which are p"blic- static and final b&
defa"lt
:% ,e can not create an object to an interface
B% 3"t we can create a reference variable of interface t&pe
J% All the methods of the interface sho"ld be implemented in its implementation
classes
G% If an& method is not implemented- then that class sho"ld be declared as
EabstractF
9% Interface reference can be "sed to refer to all the objects of its implementation
classes
.2% ;nce an interface is written- an& third part& vendor can provide implementation
classes
..% An interface can not implement another implement
./% An interface can e*tend another interface
.0% ,e can create classes in an interface
.7% A class can implement m"ltiple interfaces
Class A implements 3-CD
,hat is the difference between abstract & interface5 : -
A% Interface contains onl& abstract methods Abstract class contains instance
variables Interface contains constant (he abstract method sho"ld be implemented
in its s"b classes All the methods of interface sho"ld be implemented in
implemented classes An abstract class is declared "sing the ke& word EabstractF
Interface is declared "sing EinterfaceF
97
C
mi
;racle!3
connect$ %
K A programmer writes an abstract class when there are some common
feat"res shared b& all the objects
K A programmer writes an interface when all the feat"res sho"ld be
implemented differentl& in different objects
,hen abstract class is written the programmer sho"ld provide the s"b class
,hen interface is written an& third part& vendor can provide
implementation class
'ow m"ch memor& is taken b& class twoFs object5
.% 7 b /% G b 0% TG b 7% [G b
A% Ans is TG b beca"se it is also taken memor& of class two
E*: -
class ;ne
M
int *Q
R
class (wo e*tends ;ne
M 7 H 7
int *Q
R G
(&pes of inheritance: -
.% Single inheritance: - A prod"cing s"b class from a single s"per class is called
single inheritance
class A
class 3 e*tends A
class A
class 3 e*tends A
class C e*tends A
class ! e*tends A
/% #"ltiple inheritances: - 1rod"cing s"b class from m"ltiple s"per classes is called
m"ltiple inheritances #"ltiple means more than one
#"ltiple inheritances are not s"pported in java
E8: -
98
A
A
3 C !
3
int *
int *
class A
class 3
class C e*tends A-3
44
invalid
Statements
class A in java
class 3
class C
class ! e*tends A-3-C
class E e*tends A-3-C
,h& m"ltiple inheritances are not s"pported in java5
A% .% #"ltiple inheritance leads to conf"sion for a programmer ;perator over
loading- m"ltiple inheritances are not available in java
/% If the programmer wants we can achieve m"ltiple inheritance "sing
interfaces
class ! e*tends A-3-C 44 invalid
class ! implements A-3-C44 valid
'ere A-3-C are not classes onl& interfaces So it is not m"ltiple
inheritances (his is wa& of m"ltiple inheritances indirectl&
0% A programmer can achieve m"ltiple inheritances b& "sing single
inheritance repeatedl&
3eca"se the above reasons the direct m"ltiple inheritances is not available in java
44 m"ltiple inheritances =sing interfaces
interface Cather
M
int prop.V:22222Q
float ht.VB/fQ
R
99
A 3
C
3 C A
! E
A
*
3
*
C
*
interface #other
M
int prop/V722222Q
float ht/V:/fQ
R
class Child implements Cather-#other
M
void propert&$ %
M
S&stemo"tprintln$Pchild propert&VPH$prop.Hprop/%%Q
R
void height$ %
M
S&stemo"tprintln$Pchild heightVPH$ht.Hht/%4/%Q
R
R
class #"lti
M
p"blic static void main$String argsNO%
M
Child chVnew Child$ %Q
chpropert&$ %Q
chheight$ %Q
R
R
!:SpsrSCore javaTjavac #"ltijava
!:SpsrSCore javaTjava #"lti
child propert&V922222
child heightV:J
#"lti 'ierarchical '&brid
Inheritance Inheritance Inheritance
1ackages: - A package represents a s"b director& that contains a gro"p of elements
E8: - importjavaioKQ
Compile: - javac Id class namejava
100
Advantages of packages: -
.% 1ackages hide classes & interfaces (h"s the& provide protection for them
/% (he classes of one 1ackage are isolated from the classes of another 1ackage
So it is possible to "se same names for the classes into different packages
0% =sing package concept we can create o"r own 1ackages & also we can e*tend
alread& available 1ackages
7% 1ackages provide re "sabilit& of code
44 creating o"r own 1ackage: pack
package packQ
p"blic class Addition
M
private do"ble d.-d/Q
p"blic Addition$do"ble a-do"ble b%
M
d.VaQ
d/VbQ
R
p"blic void s"m$ %
M
S&stemo"tprintln$PS"mVPH$d.Hd/%%Q
R
R
!:SpsrSCore javaTjavac -d Additionjava
44 "sing package: pack $Addition%
class Addition=se
M
p"blic static void main$String argsNO%
M
packAddition objVnew packAddition$.2-.::%Q
objs"m$ %Q
R
R
!:SpsrSCore javaTjavac -d Addition=sejava
!:SpsrSCore javaTjava Addition=se
S"mV/::
44 "sing package: pack
package packQ
p"blic class S"btraction
M
p"blic static do"ble s"b$do"ble a-do"ble b%
M
101
ret"rn a-bQ
R
R
!:SpsrSCore javaTjavac -d S"btractionjava
44 "sing package: pack $S"btraction%
class S"btraction=se
M
p"blic static void main$String argsNO%
M
packAddition objVnew packAddition$.0-70:%Q
objs"m$ %Q
do"ble resVpackS"btractions"b$.0-70:%Q
S&stemo"tprintln$Pres"lt V PHres%Q
R
R
!:SpsrSCore javaTjavac -d S"btraction=sejava
!:SpsrSCore javaTjava S"btraction=se
S"mV:B:
res"lt V -02:
$or%
import packAdditionQ import packK
import packS"btractionQ
class S"btraction=se.
M
p"blic static void main$String argsNO%
M
Addition objVnew Addition$.0-70:%Q
objs"m$ %Q
do"ble resVS"btractions"b$.0-70:%Q
S&stemo"tprintln$Pres"lt V PHres%Q
R
R
!:SpsrSCore javaTset C)ASS1A('VpskrQQ]C)ASS1A(']
!:SpsrSCore javaTjavac -d S"btraction=se.java
!:SpsrSCore javaTjava S"btraction=se.
S"mV:B:
res"lt V -02:
Import the package name eliminates the before adding class names
Error: - bad class file : SAdditionjava
,e can handle these errors m"st follow some steps
102
Step .: - Java compile searches from the package in jar$java archair% file format in
the following director&
C:Sprogram filesSjavaSjre.:2SlibSe*t
,hat is java archair file5
A% Jar file is a compressed version of several Class or Java files A jar file created
"sing jar "tilit& provided b& s"n micro s&stems
Step /:- Java compiler tapes the present director& as the package and searches for
class files there
Step 0:- Java compiler searches for the package in c"rrent director&
Compile:- pack director& c"t & paste in to pskr folder
!:S psrT cd pskr
!:S psrS pskrTjavac S"btraction=sejava
@
Step 7:- Cinall& java compiler searches for the package in the class path
Class path: -
Class path is an operating s&stem variable (hat stores active director& path
(o sea the class path
!:S psrT echo ]classpath]
!ispla& content of class path
SQC:S1rogram CilesSJavaSjdk.:2\2JSjreSlibSrtjarQd:SpsrQpskrQ
(o set the class path to the package director&
!:SpsrT set classpathVd:SpsrSpskrQQ]classpath]
Compile the above program & r"n it
44 adding interface to the package: pack
package packQ
p"blic interface #&!ate 44 j"st displa& date & time
M
void show!ate$ %Q 44 p"blic abstract
R
!:SpsrSCore javaTjavac -d #&!atejava
44 this is implementation class of #&!ate
package packQ
import pack#&!ateQ
import java"til!ateQ
p"blic class #&!ateImpl implements #&!ate
M
p"blic void show!ate$ %
M
!ate dVnew !ate$ %Q
S&stemo"tprintln$d%Q
R
R
103
!:SpsrSCore javaTjavac -d #&!ateImpljava
44 "sing the implementation class object
import pack#&!ateImplQ
class #&!ateImpl=se
M
p"blic static void main$StringNO args%
M
#&!ateImpl objVnew #&!ateImpl$ %Q
objshow!ate$ %Q
R
R
!:SpsrSCore javaTjavac -d #&!ateImpl=sejava
!:SpsrSCore javaTjava #&!ateImpl=se
#on Sep 20 .0:/0::: IS( /22J
44 creating s"b package
package inetQ
p"blic class Sample
M
p"blic void show$ %
M
S&stemo"tprintln$P'ai how are "P%Q
R
R
!:SpsrSCore javaTjavac Id Samplejava
44 "sing the s"b package
import inetSampleQ
p"blic class Sample=se
M
p"blic static void main$String argsNO%
M
Sample sVnew Sample$ %Q
sshow$ %Q
R
R
!:SpsrSCore javaTjavac Sample=sejava
!:SpsrSCore javaTjava Sample=se
'ai how are "
S"ppose inet director& c"t & paste the director& in the drive pskr director& and set
the path
set classpath d:SpskrQQ]classpath]
104
Access specifiers $or% access modifiers: - An access specifier is a ke& word that
specifies how to read or access the members of a class or the class itself (here are 7
t&pes of access specifiers (he& are
.% private /% p"blic 0% protected 7% defa"lt
.% private members of a class are not accessible in other classes of same package or
another package
/% p"blic members of a class are available to other classes of same package or
another package Its scope is global scope
0% protected members are accessible to the other classes of the same package b"t not
in another package
3 A C
Same director& another
7% defa"lt members are accessible in other classes of the same package (he& are not
accessible in another package Its scope is package scope
+ote: - protected members of a class are alwa&s available to its s"b classes in the
same package or another package also (he scope of protected of some times global
scope & some time package scope
44 access specifiers
package sameQ
p"blic class A
M
private int aV.Q
p"blic int bV/Q
protected int cV0Q
int dV7Q
R
!:SpsrSCore javaTjavac -d Ajava
44 access specifier with same package
package sameQ
105
private
p"blic
protected
defa"lt
import sameAQ
p"blic class 3
M
p"blic static void main$String argsNO%
M
A objVnew A$ %Q
S&stemo"tprintln$obja%Q
S&stemo"tprintln$objb%Q
S&stemo"tprintln$objc%Q
S&stemo"tprintln$objd%Q
R
R
!:SpsrSCore javaTjavac -d 3java
3java:9: a has private access in sameA
S&stemo"tprintln$obja%Q
a
. error
44 access specifier with another package
package anotherQ
import sameAQ
p"blic class C
M
p"blic static void main$String argsNO%
M
A objVnew A$ %Q
S&stemo"tprintln$obja%Q
S&stemo"tprintln$objb%Q
S&stemo"tprintln$objc%Q
S&stemo"tprintln$objd%Q
R
R
!:SpsrSCore javaTjavac -d Cjava
Cjava:9: a has private access in sameA
S&stemo"tprintln$obja%Q
a
Cjava:..: c has protected access in sameA
S&stemo"tprintln$objc%Q
a
Cjava:./: d is not p"blic in sameAQ cannot be accessed from o"tside package
S&stemo"tprintln$objd%Q
a
0 errors
44 access specifier with another package
106
package anotherQ
import sameAQ
p"blic class C e*tends A
M
p"blic static void main$String argsNO%
M
C objVnew C$ %Q
S&stemo"tprintln$obja%Q
S&stemo"tprintln$objb%Q
S&stemo"tprintln$objc%Q
S&stemo"tprintln$objd%Q
R
R
!:SpsrSCore javaTjavac -d Cjava
Cjava:9: a has private access in sameA
S&stemo"tprintln$obja%Q
a
Cjava:./: d is not p"blic in sameAQ cannot be accessed from o"tside package
S&stemo"tprintln$objd%Q
a
/ errors
A1I doc"ment: - Application programming interface
Create A1I doc"ment: - !:SpsrTjavadoc Kjava $press enter%
;pen internet e*plorer & select one choose $dos% file & press enter
E*ception handling: - An e*ception is a r"ntime error
An error is software is called a b"g <emoving errors from software is called
deb"gging
.% Compile time errors: - (hese are errors in the s&nta* or the grammar of the
lang"age (hese errors are detected b& the compiler at r"n time of
compilation $Java compiler can displa& "p to .22 errors% !esk checking is
the sol"tion of compile time errors
/% <"n time errors: - (hese errors are the errors which occ"r beca"se in
s"fficientl& of compiler s&stem (hese errors are detected b& J?# at the time
of r"nning the program <"n time errors are serio"s errors ;nl& the
programmers are responsible for it
0% )ogical errors: - (hese errors represent bad logic $or% these errors are not
detected b& the compiler or J?# )ogical errors are detected b& comparing
the program o"t p"t with man"all& calc"lated res"lt
,hat is checked e*ception5
A% (he e*ception will occ"r d"ring compilation & are e*ec"ted b& the java compiler
are called checked e*ception (he e*ception which are detected at r"ntime b& the
J?# are called "nchecked e*ceptions or r"n time errors
107
E*ception: - An& abnormal event in the program is called an e*ception All
e*ception are represented b& classes in java
44 E*ception e*ample
class E*ception!emo
M
p"blic static void main$String argsNO%
M
S&stemo"tprintln$Popen filesP%Q
int nVargslengthQ
S&stemo"tprintln$PnVPHn%Q
int aV7:4nQ
S&stemo"tprintln$PaVPHa%Q
S&stemo"tprintln$Pclose filesP%Q
R
R
!:SpsrSCore javaTjavac E*ception!emojava
!:SpsrSCore javaTjava E*ception!emo S"nil W"mar <edd&
open files
nV0
aV.:
close files
All e*ceptions are s"bclasses of which class5
A% E*ception class
,hat is throwable5
A% (hrowable is a class that represents is errors & e*ceptions in java
,hat is E*ception5
A% An& abnormal event in the program is called an e*ception
,hat is the difference between an error & e*ception5: -
An e*ception is an error that can be handled An error can not be handled
,hen there is an e*ception J?# displa&s e*ception name & its description & then
abnormall& terminates the program 3eca"se of these the files are data bases are not
closed & hence "serFs entire data will be lost (his is effect of e*ception
javalang
throwable
errors e*ceptions
all errors all e*ception
108
In case of an e*ception the programmer to do the following tasks
.% (he programmer sho"ld write all the statements- where there an e*ception- inside
a tr& blocks
tr& M
StatementsQ
R
,hen there is an e*ception in tr& block J?# will not terminate the program It will
store e*ception details- in an e*ception stack & then j"mps into catch block
tr&
M
StatementsQ
R
Catch$E*ceptionClass obj%
M
StatementsQ
R E*ception block
/% In catch block the programmer has to displa& e*ception details & an& other
messages to the "ser
0% (he programmer sho"ld close the files & data bases In finall& block
+ote: - finall& blocks is alwa&s e*ec"ted whether there is e*ception or not
finall& M
statementsQ
R
44 E*ception e*ample
class E*ceptionE*ample
M
p"blic static void main$String argsNO%
M
tr&
M
S&stemo"tprintln$Popen filesP%Q
int nVargslengthQ
S&stemo"tprintln$PnVPHn%Q
int aV7:4nQ
S&stemo"tprintln$PaVPHa%Q
int bNOVM.2-.9-./-.0RQ
bN:2OV.22Q
R
catch$ArithmeticE*ception ae%
M
S&stemo"tprintln$PaeP%Q
S&stemo"tprintln$Ppl> t&pe data while r"nning the
dataP%Q
109
E*ception
details
R
catch$Arra&Inde*;"t;f3o"ndsE*ception aie%
M
S&stemo"tprintln$PaieP%Q
S&stemo"tprintln$Pplese see that arra& inde* is within
the rangeP%Q
R
finall&
M
S&stemo"tprintln$Pclose filesP%Q
R
R
R
!:SpsrSCore javaTjavac E*ceptionE*amplejava
!:SpsrSCore javaTjava E*ceptionE*ample
open files
nV2
ae
pl> t&pe data while r"nning the data
close files
!:SpsrSCore javaTjava E*ceptionE*ample Swarna )atha <edd&
open files
nV0
aV.:
aie
plese see that arra& inde* is within the range
close files
Even tho"gh m"lti e*ceptions are fo"nd in the program- onl& one e*ception is
raised at a time
,e can handle m"ltiple e*ceptions b& writing m"ltiple catch blocks
A single tr& block can be followed b& several catch blocks
Catch block does not alwa&s e*it witho"t a tr&- b"t a tr& block e*it witho"t a
catch block
Cinall& block is alwa&s e*ec"ted whether there is e*ception or not
44 not handling the e*ception
import javaioKQ
class Sample
M
void accept$ %throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprintln$Penter "r name : P%Q
110
String nameVbrread)ine$ %Q
S&stemo"tprintln$P'ai PHname%Q
R
R
class E*ception+ot'andle
M
p"blic static void main$String argsNO%throws I;E*ception
M
Sample sVnew Sample$ %Q
saccept$ %Q
R
R
!:SpsrSCore javaTjavac E*ception+ot'andlejava
!:SpsrSCore javaTjava E*ception+ot'andle
enter "r name :
<angha <edd&
'ai <angha <edd&
throws: - throws statement is "sef"l to throw o"t an e*ception witho"t handling it
+ote: - if the programmer does not handle an& t&pe of e*ception we sho"ld write
throws E*ception
throw:-it is "sef"l to create an e*ception object and throw it o"t of tr& block
44 (hrow E*ample
class (hrow!emo
M
static void !emo$ %
M
tr&
M
S&stemo"tprintln$Pinside methodP%Q
throw new +"ll1ointerE*ception$Pm& dataP%Q
R
catch$+"ll1ointerE*ception ne%
M
S&stemo"tprintln$PneP%Q
R
R
p"blic static void main$String argsNO%
M
(hrow!emo!emo$ %Q
R
R
!:SpsrSCore javaTjavac (hrow!emojava
!:SpsrSCore javaTjava (hrow!emo
inside method
ne
111
,hat is the difference between throws & throw5
A% throws is "sed to throw o"t of an e*ception witho"t handling it
throw is "sed to create an e*ception & throwing it & handle it
=ses of throw: - .% throw is "sed in software testing to test whether a program is
handling all the e*ceptions have climbed b& programmer
/% throw is "sed to create & throw "ser defined e*ceptions
(&pes of e*ceptions: -
.% 3"ilt-in e*ceptions: - (hese are the e*ceptions which are alread& available in java
E8: -
a% ArithmeticE*ception
b% Arra&Inde*;"t;f3o"ndsE*ception
c% StringInde*;"t;f3o"ndsE*ception
d% +"ll1ointerE*ception
e% +oS"ch#ethodE*ception
f% Class+otCo"ndE*ception
g% Cile+otCo"ndE*ception
h% +"mberCormatE*ception
i% <"ntimeE*ception
j% Interr"ptedE*ception
/% =ser-defined e*ceptions: - (hese are the e*ceptions created b& "ser of the
lang"age
Creating "ser defined e*ceptions: -
a% ,rite "ser e*ception class as a s"b class to e*ception class
E*: - class #&E*ception e*tends E*ception
b% ,rite a defa"lt constr"ctor in "ser e*ception class
E* I #&E*ception$ % M R
c% ,rite a string parameteri>ed constr"ctor & from their call the s"per class
constr"ctor
E*: - #&E*ception$String str%
M
S"per$str%Q
R
d% (o raise the e*ception creates "ser e*ception class object & throw it "sing
throw statement
E*: - throw meQ
44 "ser defined e*ception
class #&E*ception e*tends E*ception
M
private static int accnoNOVM./-.0-..-.7-.:RQ
private static String nameNOVMPs"nP-PS"dP-PsreeP-PsiriP-PlathaPRQ
private static do"ble balNOVM07::0-:07:7:-:07:07-:70::B-0/7B70RQ
#&E*ception$ %
M
112
R
#&E*ception$String str%
M
s"per$str%Q
R
p"blic static void main$String argsNO%
M
tr&
M
S&stemo"tprintln$Paccno PHPStPHPnamePH Pbalance P%Q
for$int iV2Qi[:QiHH%
M
S&stemo"tprintln$accnoNiOHPStPHnameNiOHPStPHbalNiO%Q
if$balNiO[07222%
M
#&E*ception meVnew #&E*ception$Pbalance ammo"nt is lessP%Q
throw meQ
R
R
R
catch$#&E*ception me%
M
meprintStack(race$ %Q
R
R
R
!:SpsrSCore javaTjavac #&E*ceptionjava
!:SpsrSCore javaTjava #&E*ception
accno namebalance
./ s"n 07::0
.0 S"d :07:7:
.. sree :07:07
.7 siri :70::B
.: latha 0/7B70
#&E*ception: balance ammo"nt is less
at #&E*ceptionmain$#&E*ceptionjava:/7%
All application on internet send or receive data is the form of objects
/% (he class in java"til package acts "p on object onl& 3eca"se of the above
reason it is necessar& to convert primitive data t&pes into object
form (his is done b& wrapper classes
,rapper class:- All wrapper classes are defined in javalong package
A wrapper class wraps contains a primitive data t&pe in its object
Character is a wrapper class in to a char data t&pe
1rimitive data t&pe ,rapper class:-
113
char Character
b&te 3&te
int Integer
float Cloat
do"ble !o"ble
long )ong
.% Character class:-
(he character class wraps a val"e of the primitive t&pe EcharF an object An
object of t&pe character contains a single field whose t&pe is char
Constr"ctors:-
.% Character $char ch%
E*: - char ch V EAF
Character objVnew Character $ch%Q
#ethods:-
.% char char?al"e $ %
<et"rns the char val"e of the invoking object
E*: - char * Vobjchar?al"e$ %Q
/% static boolean is!igit$char ch%
<et"rns tr"e if ch is a digit $2 to 9% otherwise ret"rns false
E*:- char V E9FQ boolean * V characteris!igit$ch%Q
0% static boolean is)etter$char ch%
ret"rns tr"e if ch is a letter $A to b or a to >%
7% static boolean is"pperCase$char ch%
ret"rns tr"e if ch is an "pper case letter $A to b%
:% static boolean is)owerCase$char ch%
ret"rns tr"e if ch is an )ower case letter $a to >%
B% static boolean isSpaceChar$char ch%
ret"rns tr"e if ch is coming from Space 3ar
J% static boolean iswhitespace$char ch %
ret"rns tr"e if ch is coming from (ab- Enter- 3ackspace
G% static char to=pperCase $char ch %
converts ch into "pper case
9% static char to)owerCase $char ch%
Converts ch into lowercase
,hich of the wrapper classes does not a constr"ctor with string parameter5 $or%
,hich of the wrapper classes contains onl& one constr"ctor5
A% Character
44 test a character
import javaioKQ
class Character(est
M
p"blic static void main$StringNO args% throws I;E*ception
M
114
while$tr"e%
M
44 to accept data from ke& board
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprintln$PEnter a charP%Q
char chV$char%brread$ %Q
44 test the ch
if$chVVXdX%
S&steme*it$2%Q
else if$Characteris!igit$ch%%
S&stemo"tprintln$PIt is a digitP%Q
else if$Characteris=pperCase$ch%%
S&stemo"tprintln$PIt is a capital letterP%Q
else if$Characteris)owerCase$ch%%
S&stemo"tprintln$PIt a small )etterP%Q
else if$CharacterisSpaceChar$ch%%
S&stemo"tprintln$PIt is coming from space barP%Q
else if$Characteris,hitespace$ch%%
S&stemo"tprintln$PIt is a white spaceP%Q
else
S&stemo"tprintln$PSorr&- i donXt know that characterP%Q
R
R
R
!:SpsrSCore javaTjavac Character(estjava
!:SpsrSCore javaTjava Character(est
Enter a char
p
It a small )etter
Enter a char
7
It is a digit
Enter a char
`
Sorr&- i donXt know that character
Enter a char
d
/% 3&te class: - (he b&te class wraps a val"e of primitive t&pe Eb&teF in an object An
object of t&pe b&te contains a single field whose t&pe is b&te
Constr"ctor: -
.% 3&te$b&te n"m%
/% 3&te$String str%
E*: - .% b&te bV99Q
3&te objVnew 3&te$b%Q
115
/% String strVZ./2Z
3&te objVnew 3&te$str%Q
#ethods: -
.% b&te b&te?al"e$ %
<et"rns the val"e of invoking object as a b&te
/% int compare(o$3&te b%
Compares the n"merical val"e of invoking object with that of EbF ret"rns 2-
if the val"es all e6"al <et"rns a Ive val"e- if the invoking object has a lower val"e
<et"rns a Hve val"e- if the invoking object has a grater val"e
E*: -int nVb.Compare(o$b/%
If b.VVb/Q nVV2Q
If b.[b/Q nVV -veQ
If b.Tb/Q nVV HveQ
0% static b&te parse3&tes$String str% throws +"mberCormatE*ception
<et"rns the b&te e6"ivalent ot the n"mber contained in the string specified
b& EstrF
7% String toString$ %
<et"rns a string that contains the decimal e6"ivalent of the invoking object
:% static 3&te?al"e;f$String str% throws +"mberCormatE*ception
<et"rns a b&te object that contains the val"e specified b& string EstrF
44 creating & comparing b&te objects
import javaioKQ
class 3&tes
M
p"blic static void main$String argsNO%throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$Penter a b&te no : P%Q
String strVbrread)ine$ %Q
3&te b.Vnew 3&te$str%Q
S&stemo"tprint$Penter another b&te no : P%Q
strVbrread)ine$ %Q
3&te b/V3&teval"e;f$str%Q
int nVb.compare(o$b/%Q
if$nVV2%
S&stemo"tprintln$Pboth same P%Q
else
if$nT2%
S&stemo"tprintln$b.HPis bigger thanPHb/%Q
else
S&stemo"tprintln$b.HPis lesser thanPHb/%Q
R
R
116
!:SpsrSCore javaTjavac 3&tesjava
!:SpsrSCore javaTjava 3&tes
enter a b&te no : 07
enter another b&te no : :B
07is lesser than:B
0% Integer Class: - (he integer class wraps a val"e of the primitive t&pe EintF in a
object An object of t&pe integer contains a single field whose t&pe is int
Constr"ctors: -
.% Integer$int n"m%
/% Integer$String str%
#ethods: -
.% int int?al"e$ %
<et"rns the val"e of invoking object as an EintF
/% int compare(o$Integer obj%
Compare the n"merical val"e of the invoking object of EobjF <et"rns 2--ve or
Hve val"e
0% static int parseInt$String str% throws +"mberCormatE*ception
<et"rns int e6"ivalent of the string str
E*: - String strVZ'&derabadZQ
Int nVIntegerparseInt$str%Q
7% String toString$ %
<et"rns a string from of the invoking object
:% static Integer val"e;f$String str% throws +"mberCormatE*ception
<et"rns an integer object that contain the val"e shown b& str
B% static String to3inar&String$int i%
<et"rns a string representation of the integer arg"ment in base /
J% static String to'e*String$int i%
<et"rns a string representation of the integer arg"ment in base B
G% static String to;ctalString$int i%
<et"rns a string representation of the integer arg"ment in base G
44 converting into other n"mber s&stem
import javaioKQ
class ConvertIntegers
M
p"blic static void main$String argsNO%throws I;E*ception
M
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprintln$PEnter int :P%Q
String strVbrread)ine$ %Q
int nVIntegerparseInt$str%Q
S&stemo"tprintln$Pin !ecimal VPHn%Q
strVIntegerto3inar&String$n%Q
S&stemo"tprintln$PIn 3inar& VPHstr%Q
117
strVIntegerto'e*String$n%Q
S&stemo"tprintln$PIn 'e*a!ecimal VPHstr%Q
strVIntegerto;ctalString$n%Q
S&stemo"tprintln$PIn ;ctal VPHstr%Q
R
R
!:SpsrSCore javaTjavac ConvertIntegersjava
!:SpsrSCore javaTjava ConvertIntegers
Enter int :
./0
in !ecimal V./0
In 3inar& V....2..
In 'e*a!ecimal VJb
In ;ctal V.J0
7% Cloat class: - (he float class wraps a val"e of primitive t&pe EfloatF in an object
An object of t&pe float contains a single field whose t&pe is float
Constr"cters:-
.%Cloat $float n"m%
/%Cloat $String str%
#ethods:-
.% float float?al"e$ %
ret"rns the val"e of the involving object as a float
/% do"ble do"ble?al"e$ %
ret"rns the val"e of the involving object as a do"ble
0% int compare(o$Cloat f%
compares the n"meric val"e of the involving object with that of EfF
ret"rns --veHve val"e
7% static float parseCloat $string str% throwsn"mberCormatE*ec"tion
<et"rns the float e6"ivalent the string str
:% String toString $ %
ret"rns the string e6"ivalent of the involving object
B% static float val"e;f $String str% throwsn"mberCormatE*ec"tion
<et"rns the float object with the val"e specified b& string str
:% do"ble class: - (he do"ble class wraps a val"e of primitive t&pe Edo"bleF in an
object An object of t&pe float contains a single field whose t&pe is do"ble
Constr"cters:-.% !o"ble $do"ble n"m%
/% !o"ble $String str%
#ethods:-
.% do"ble float?al"e$ %
ret"rns the val"e of the involving object as a do"ble
/% do"ble float?al"e$ %
ret"rns the val"e of the involving object as a float
0% int compare(o$!o"ble d%
118
compares the n"meric val"e of the involving object with that of EdF
ret"rns --veHve val"e
7%static do"ble parse!o"ble $string str%
throws +"mberCormatE*ec"tion
<et"rns the do"ble e6"ivalent the string str
:% String toString $ %
ret"rns the string e6"ivalent of the involving object
B% static do"ble val"e;f $String str% throws +"mberCormatE*ec"tion
<et"rns the do"ble object with the val"e specified b& string str
B% #ath class: - (he class math contains methods for performing n"merical
operations
#ethods: -
.% static do"ble sin$do"ble arg%
<et"rns the sine val"e of arg Arg is an radians
/% static do"ble cos$do"ble arg%
<et"rns the cosine val"e of arg Arg is an radians
0% static do"ble tan$do"ble arg%
<et"rns the tangent val"e of arg Arg is an radians
7% static do"ble log$do"ble arg%
<et"rns the nat"ral logarithm val"e of arg
:% static do"ble pow$do"ble *-do"ble &%
<et"rns * to the power of & val"e
B% static do"ble s6rt$do"ble arg%
<et"rns the s6"are root of arg
J% static do"ble abs$do"ble arg%
<et"rns the absol"te val"e of arg
G% static do"ble ceil$do"ble arg%
<et"rns the smallest integers which is grater or e6"al to arg
E*:- #athceil$7:% is :2
Ceil means ceiling It means "p
9% static do"ble floor$do"ble arg%
<et"rns the grater integer which is lower or e6"al to arg
E*: - #athfloor$7:% is 72
.2% static do"ble min$arg.-arg/%
<et"rns the minim"m val"e of arg. & arg/
..% static do"ble ma*$arg.-arg/%
<et"rns the ma*im"m val"e of arg. & arg/
./% static long ro"nd$arg%
<et"rns the ro"nded val"e
E*: - #ath-ro"nd$7B% is :
.0% static do"ble random$ %
<et"rns the random n"mbers between 2&.
.7% static do"ble to<adians$do"ble angle%
119
Converts angle in degrees in to radians
.:% static do"ble to!egrees$do"ble angle%
Converts angle in radians in to degrees
44 random n"mbers between . & .2
class <and
M
p"blic static void main$String argsNO% throws Interr"ptedE*ception
M
while$tr"e%
M
do"ble dV.2K#athrandom$ %Q
int nV$int%dQ
S&stemo"tprint$nHZStZ%Q
if$nVV2%
S&steme*it$2%Q
(hreadsleep$0222%Q
R
R
R
!:SpsrSCore javaTjavac <andjava
!:SpsrSCore javaTjava <and
J J G : B / J B . 2
Java"tilit&: -
Stack: - A stack represents arrangement of elements $objects% in )IC; $last in first
o"t% order
Inserting the elements & deleting the elements will take place onl& from one side of
the stack- called top of the stack (he other side is closed & it called bottom of the
stack
Inserting elements into stack is called p"sh operation !eleting element from
top of the stack is called pop operation Searching for an element in the element is
called peep operation
E*: - A file of plates in a hotel- linking of coaches to a railwa& engine-
C! holder- bangles to hand
'ow to create a stack: -
.% (o create a stack
Stack stVnew Stack$ %Q
/% (o know whether a stack is empt& or not- "se empt&$ %
bolean bVstempt&$ %Q
0% (o p"sh an element in to the stack
stp"sh$element%Q
7% (o pop an element from a stack
;bject elementVstpop$ %Q
:% to search for an element in the stack
120
int iVstsearch$element%Q
(his method ret"rns -.- if the element is not fo"nd
(op
3ottom
44 A stack with int val"es
import javaioKQ
import java"tilKQ
class Stack!emo
M
static void stp"sh$Stack st-int a%
M
stp"sh$new Integer$a%%Q
R
static void stpop$Stack st%
M
Integer aV$Integer%stpop$ %Q
S&stemo"tprintln$Ppopped V PHa%Q
R
static int stSearch$Stack st-int a%
M
int iVstsearch$new Integer$a%%Q
ret"rn iQ
R
p"blic static void main$String argsNO%throws E*ception
M
Stack stVnew Stack$ %Q
int element-pos-choiceV2Q
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
121
7
0
/
.
:
44 men"
while$choice[7%
M
S&stemo"tprintln$Pstack operations P%Q
S&stemo"tprintln$P. p"sh element P%Q
S&stemo"tprintln$P/ pop element P%Q
S&stemo"tprintln$P0 search for element P%Q
S&stemo"tprintln$P7 e*it P%Q
S&stemo"tprint$P &o"r choice P%Q
choiceVIntegerparseInt$brread)ine$ %%Q
44 perform a task depending on choice
switch$choice%
M
case .:
elementVIntegerparseInt$brread)ine$ %%Q
stp"sh$st-element%Q
breakQ
case /:
stpop$st%Q
breakQ
case 0:
S&stemo"tprint$Penter element :P%Q
elementVIntegerparseInt$brread)ine$ %%Q
pos V stSearch$st-element%Q
if$posVV-.%
S&stemo"tprintln$Pelement not fo"nd P%Q
else
S&stemo"tprintln$Pelement fo"nd at position :PHpos%Q
breakQ
defa"lt:
ret"rnQ
R
S&stemo"tprintln$Pstack VPHst%Q
R
R
R
!:SpsrSCore javaTjavac Stack!emojava
!:SpsrSCore javaTjava Stack!emo
stack operations
. p"sh element
/ pop element
0 search for element
7 e*it
&o"r choice .
/
stack VN/O
122
stack operations
. p"sh element
/ pop element
0 search for element
7 e*it
&o"r choice .
0
stack VN/- 0O
stack operations
. p"sh element
/ pop element
0 search for element
7 e*it
&o"r choice .
7
stack VN/- 0- 7O
stack operations
. p"sh element
/ pop element
0 search for element
7 e*it
&o"r choice /
popped V 7
stack VN/- 0O
stack operations
. p"sh element
/ pop element
0 search for element
7 e*it
&o"r choice 0
enter element :/
element fo"nd at position :/
stack VN/- 0O
stack operations
. p"sh element
/ pop element
0 search for element
7 e*it
&o"r choice 7
)inked list: -It represents a set of nodes s"ch that each node contains two fields
node
.% (he data field stores data
/% (he link field stores reference of the ne*t node
123
data link
Crate a linked list: -
.% (o create a linked list:
)inked)ist llVnew)inked)ist$ %Q
/% (o add elements to a linked list
lladd$element%Q
(o add element in second position
lladd$/-element%Q
0% (o remove first element from the linked list
llremove$ %Q
(o remove last element from the linked list
llremove)ast$ %Q
(o remove second element
llremove$/%Q
7% (o change the second element with a new element
llset$/-+ew element%Q
to retrieve element b& element from collection objects ,e can "se the following
one of the following interfaces
.% Iterator /% )istIterator 0% En"meration
44 a linked list with string
import javaioKQ
import java"tilKQ
class ))!emo
M
p"blic static void main$String argsNO%throws I;E*ception
M
44 create an empt& linked list
)inked)ist llVnew )inked)ist$ %Q
44 add elements to ll
lladd$PAmericaP%Q
lladd$PIndiaP%Q
lladd$PChinaP%Q
lladd$PJapanP%Q
lladd$PCranceP%Q
lladd$PSri lankaP%Q
44 displa& the linked list
S&stemo"tprintln$P)inked )ist V PHll%Q
44 vars
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
String elementQ
int pos-choiceV2Q
124
44 men"
while$choice[7%
M
S&stemo"tprintln$P)inked )ist operations P%Q
S&stemo"tprintln$P. insert element : P%Q
S&stemo"tprintln$P/ remove element : P%Q
S&stemo"tprintln$P0 change element : P%Q
S&stemo"tprintln$P7 e*it P%Q
S&stemo"tprintln$P &o"r choice :P%Q
choiceVIntegerparseInt$brread)ine$ %%Q
44 depending on choice- perform a task
switch$choice%
M
case .:
S&stemo"tprint$PEnter element : P%Q
elementVbrread)ine$ %Q
S&stemo"tprint$PEnter 1osition +o : P%Q
posVIntegerparseInt$brread)ine$ %%Q
lladd$pos-element%Q
breakQ
case /:
S&stemo"tprint$PEnter 1osition +o : P%Q
posVIntegerparseInt$brread)ine$ %%Q
llremove$pos%Q
breakQ
case 0:
S&stemo"tprint$PEnter element : P%Q
elementVbrread)ine$ %Q
S&stemo"tprint$PEnter 1osition +o : P%Q
posVIntegerparseInt$brread)ine$ %%Q
llset$pos-element%Q
breakQ
defa"lt :
ret"rnQ
R
S&stemo"tprintln$P)ist VPHll%Q
R
R
R
!:SpsrSCore javaTjavac ))!emojava
!:SpsrSCore javaTjava ))!emo
)inked )ist V NAmerica- India- china- Japan- Crance- Sri lankaO
)inked )ist operations
. insert element :
/ remove element :
0 change element :
125
7 e*it
&o"r choice :
.
Enter element : /
Enter 1osition +o : 7
)ist VNAmerica- India- china- Japan- /- Crance- Sri lankaO
)inked )ist operations
. insert element :
/ remove element :
0 change element :
7 e*it
&o"r choice :
/
Enter 1osition +o : 0
)ist VNAmerica- India- china- /- Crance- Sri lankaO
)inked )ist operations
. insert element :
/ remove element :
0 change element :
7 e*it
&o"r choice :
/
Enter 1osition +o : :
)ist VNAmerica- India- china- /- CranceO
)inked )ist operations
. insert element :
/ remove element :
0 change element :
7 e*it
&o"r choice :
0
Enter element : /
Enter 1osition +o : .
)ist VNAmerica- /- china- /- CranceO
)inked )ist operations
. insert element :
/ remove element :
0 change element :
7 e*it
&o"r choice :
7
Arra&s: - Arra&s class contains methods to handle an& arra&
.% (o sort the elements of an arra& in to ascending order
Arra&ssort$arr%Q
/% (o sort onl& a range of elements of the arra&
Arra&ssort$arr-start-end%Q
126
(hese sort elements of EarrF starting from EstartF till EendF -. element
0% (o search for an element in an arra&
Arra&sbinar&Search$arr-element%Q
(his searches for EelementF in the arra& EarrF & ret"rns its position If the EelementsF
is not fo"nd- it re&"rns a Ive val"e
+ote: - 3inar& search$ % will not act onl& a sorted arra&
7% (o compare two arra&s- to know if the& are same or not
Arra&se6"als$arr.-arr/%Q
(his ret"rns tr"e- if arr. & arr/are same- else it ret"rns false
44 sorting & searching in an arra&
import javaioKQ
import java"tilKQ
class Arra&s!emo
M
p"blic static void main$String argsNO%throws I;E*ception
M
44 to accept data from ke& board
3"ffered<eader brVnew 3"ffered<eader$new
Inp"tStream<eader$S&stemin%%Q
S&stemo"tprint$P'ow man& elements &o" want : P%Q
int nVIntegerparseInt$brread)ine$ %%Q
44 create int t&pe arra&
int arrNOVnew intNnOQ
44 store elements into arr
for$int iV2Qi[nQiHH%
M
S&stemo"tprint$PEnter element : P%Q
arrNiOVIntegerparseInt$brread)ine$ %%Q
R
44 displa& the elements
S&stemo"tprint$PLo"r Arra& is : PHZStZ%Q
displa&$arr%Q
44 sort the elements in to ascending order
Arra&ssort$arr%Q
44 displa& the sorted elements
S&stemo"tprint$PSorted arra& is : PHZStZ%Q
displa&$arr%Q
44 searches for an element in an arra&
S&stemo"tprintln$PEnter element to Search : P%Q
int *VIntegerparseInt$brread)ine$ %%Q
int posVArra&sbinar&Search$arr-*%Q
if$pos[2%
S&stemo"tprintln$PElement not Co"ndP%Q
else
S&stemo"tprintln$Pit position is :PH$posH.%%Q
R
127
static void displa&$int arrNO%
M
for$int i:arr%
S&stemo"tprintln$i%Q
R
R
!:SpsrSCore javaTjavac Arra&s!emojava
!:SpsrSCore javaTjava Arra&s!emo
'ow man& elements &o" want :
7
Enter element : J
Enter element : 9
Enter element : 0
Enter element : B
Lo"r Arra& is : J 9 0 B
Sorted arra& is : 0 B J 9
Enter element to Search :
9
it position is :7
Cloning: - Cloning is a technolog& to obtain e*act cop& of a plant- a bird an animal
and a h"man being
Cloning in java: -
;btaining bitwise e*act cop& of an object is called cloning
.% Shallow cloning: - In this- an& modifications to the cloned object- will also
modif& the original object
/% !eep cloning: - in this cloning an& modifications to the cloned object will not
affect the original object
E8: -
44 cloning e*ample
class Emplo&ee implements Cloneable
M
int idQ
String nameQ
Emplo&ee$int i-String s%
M
idViQ
nameVsQ
R
void displa&$ %
M
S&stemo"tprintln$PId V PHid%Q
S&stemo"tprintln$P+ame V PHname%Q
R
Emplo&ee m&Clone$ %throws Clone+otS"pportedE*ception
128
44 $or% protected ;bject Clone$ % throws
Clone+otS"pportedE*ception
M
ret"rn$Emplo&ee%s"perclone$ %Q
44 $or% ret"rn s"perclone$ %Q
R
R
class Clone!emo
M
p"blic static void main$String argsNO% throws Clone+otS"pportedE*ception
M
Emplo&ee e.Vnew Emplo&ee$.2-Ps"dheerP%Q
e.displa&$ %Q
Emplo&ee e/Ve.m&Clone$ %Q
44 $or% Emplo&ee e/V$Emplo&ee%e.Clone$ %Q
e/displa&$ %Q
R
R
!:SpsrSCore javaTjavac Clone!emojava
!:SpsrSCore javaTjava Clone!emo
Id V .2
+ame V s"dheer
Id V .2
+ame V s"dheer
Cloneable method does not contain an& interface A tagging interface or a
marking interface is an interface witho"t an& methods or with >ero abstract
methods
Interface indicates a special p"rpose for the object of the class
E*: - Cloneable- Seriali>ation
Aetting back object from the file is called Seriali>ation Storing a object in to a
file is called a Seriali>ation
129

You might also like