Upload
sivamskr
View
215
Download
0
Embed Size (px)
Citation preview
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
1/16
1Core Java Siva Kameswara Rao Munipalle
Audience (Who is this book for.. ? )
This book is intended for all those who have a basic idea on Java ( You need not be an expert!! " This will make #ou
perfect in $ava
www.theknowledgelabs.wordpress.com
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
2/16
2Core Java Siva Kameswara Rao Munipalle
Table of Contents
HISTORY OF JAA..................................................................................................................... 3
HO! JAA !OR"S#.................................................................................................................. 3
JAA $R%&R%'(ISIT%S............................................................................................................... 3
S%TTI)* ($ JAA %)IRO)+%)T................................................................................................. 4
I,%-S.....................................................................................................................................5
OO$S O/J%0T ORI%)T%, $RO*RA++I)* I) JAA..........................................................................5
TH% R%F%R%)0% A), O/J%0T 0R%ATIO) I) JAA............................................................................ 5
0O)STR(0TORS......................................................................................................................5
THIS A), S($%R..................................................................................................................... 5
+%+ORY +A)A*%+%)T............................................................................................................ 5
0O)TRO STAT%+%)TS.............................................................................................................5
A00%SS +O,IFI%RS..................................................................................................................5
)O)&A00%SS +O,IFI%RS........................................................................................................... 6
$RI+ITI% S !RA$$%R 0ASS%S................................................................................................ 6
TY$% 0ASTI)* A), TH% 0O)S%'(%)0%S..................................................................................... 6
O%RRI,I)*........................................................................................................................... 6
O%ROA,I)*........................................................................................................................ 6
I++(TA/% 0ASS................................................................................................................... 7
I++(TA/% 0ASS%S AR% THOS% 0ASS%S3 !HOS% O/J%0T 0A))OT /% +O,IFI%, O)0% 0R%AT%,3 IT
+%A)S A)Y +O,IFI0ATIO) O) I++(TA/% O/J%0T !I R%S(T I) A)OTH%R I++(TA/% O/J%0T...........7
,IFF%R%)0% /%T!%%) STRI)* A), STRI)*/(FF%R....................................................................... 7
THR%A,S............................................................................................................................... 7
0O%0TIO)S FRA+%!OR"...................................................................................................... 7
%40%$TIO)S........................................................................................................................... 7
%&&'()*+..............................................................................................................................8
www.theknowledgelabs.wordpress.com
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
3/16
5Core Java Siva Kameswara Rao Munipalle
H6stor7 o8 Ja9a
Java has become so popular with the Java b#te,code- which is platform independent
*n .//.- a 0roup of Sun Micros#stems en0ineers led b# James 1oslin0 decided to develop a lan0ua0e for consumer devices (cable
boxes- etc" The# wanted the lan0ua0e to be small and use e2cient code since these devices do not have powerful C&3s The#
also wanted the lan0ua0e to be hardware independent since di4erent manufacturers would use di4erent C&3s The pro$ect was
code,named
1reen
These conditions led them to decide to compile the code to an intermediate machine,like code for an ima0inar# C&3 called a
virtual machine (%ctuall#- there is a real C&3 that implements this virtual C&3 now" This intermediate code (called b#te code" is
completel# hardware independent &ro0rams are run b# an interpreter that converts the b#te code to the appropriate native
machine code
Thus- once the interpreter has been ported to a computer- it can run an# b#te,coded pro0ram
Sun uses 3*+ for their computers- so the developers based their new lan0ua0e
on C55 The# picked C55 and not C because the# wanted the lan0ua0e to be ob$ect,oriented The ori0inal name of the lan0ua0e
was 6ak 7owever- the# soon discovered that there was alread# a pro0rammin0 lan0ua0e called 6ak- so the# chan0ed the name
to Java
The 1reen pro$ect had a lot of trouble 0ettin0 others interested in Java for smart devices *t was not until the# decided to shift
0ears and market Java as a lan0ua0e for web applications that interest in Java took o4 Man# of the advanta0es that Java has for
smart devices are even bi00er advanta0es on the web
How Ja9a works#
Java ,,,,,, Java compiler ,,,,,, class 8les 9 You need J)K to do this:
class 8le 9b#te,code: is passed across the networks as this is a binar# 8le
class ,,,,,, J;M ,,, native code 9 You can do this with a JR' J)K internall# contains JR':
Sometimes all the class 8les are packed into a sin0le 8le and is called $ar 9Java %chieve:
www.theknowledgelabs.wordpress.com
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
4/16
:Core Java Siva Kameswara Rao Munipalle
packagecom.KLabs.CoreJava;
publicclassFirstProgram {
publicstaticvoidmain(String[] args) {
System.out.println(!ey.." !ai..#elcome to Java);
$
$
Ja9a pre&re; %dvanced tab
Click on 'nvironment ;ariables- under S#stem ;ariables- 8nd &%T7- and click on it
*n the 'dit windows- modif# &%T7b# addin0 the location of the class to the value for &%T7 *f #ou do not have the
item &%T7- #ou ma# select to add a new variable and add &%T7as the name and the location of the class as the
value
www.theknowledgelabs.wordpress.com
O=$
!ey.." !ai..#elcome to Java
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
5/16
>Core Java Siva Kameswara Rao Munipalle
Reopen Command prompt window- and run #our $ava code
Windo"s '# Start ,> Control &anel ,> S#stem ,> %dvanced
Click on 'nvironment ;ariables- under S#stem ;ariables- 8nd &%T7- and click on it
*n the 'dit windows- modif# &%T7b# addin0 the location of the class to the value for &%T7 *f #ou do not have the
item&%T7
- #ou ma# select to add a new variable and add&%T7
as the name and the location of the class as the
value
Close the window
Reopen Command prompt window- and run #our $ava code
Checkin the n*iron+ent
*f #our environment variables are set correctl#- #ou should see #our settin0s
6r- #ou can $ust t#pe in
$avac
$ava
*f #our &%T7 and C=%SS&%T7 is correct- windows should reco0ni?e those commandsYou do 6T need to restart #our computer when installin0 Java
I,%-s
OO$s Ob?ect Or6ented $rogramm6ng 6n Ja9a
'ncapsulation
&ol#morphism
%bstraction
*nheritance
'ncapsulation
'ncapsulation is the abilit# to packa0e data- related behavior in an ob$ect bundle and control@restrict access to them (both data
and function" from other ob$ects This is done with the help of access, modi8ers
www.theknowledgelabs.wordpress.com
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
6/16
@Core Java Siva Kameswara Rao Munipalle
&ol#morphism
This enables to have sin0le method names used di4erentl# , overridin0- overloadin0
%bstraction
*nheritance
The Re8erence and Ob?ect 0reat6on 6n Ja9a
The 6b$ect is created usin0 thenew
ke#word
new Classame("A
Can we create ob$ect for abstract Class or interface B
0onstr
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
7/16
Core Java Siva Kameswara Rao Munipalle
0ontrol Statements
Access +od6Bers
public
private
protected
default
)on&Access +od6Bers
static
8nal
s#nchroni?ed
abstract
interface
Static
www.theknowledgelabs.wordpress.com
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
8/16
CCore Java Siva Kameswara Rao Munipalle
Static ;ariable
Static initiali?er static block with no function names
Thu+b ,uleStatic initiali?er block is executed onl# once when the class is loaded
*nterfaceD
;ariables can be declared in an interface
;ariables are implicitl# public- static- and 8nal
%n interface can extend another interface
% Class implements interface to de8ne the abstract methods
Can an -nterface be nal?
)o we can-t declare an 6nter8ace to be Bnal.
Can "e dene /ri*ate and /rotected +odiers for *ariables in interfaces?
)o3 9ar6ables are p
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
9/16
NCore Java Siva Kameswara Rao Munipalle
Abstract Class
*tEs a class which cannot be instantiated withnew("operator
You need to extend it and provide a concrete class
%bstract class is commonl# used to de8ne base class for a t#pe hierarch# with default implementation- which is
applicable to all child classes
Can abstract class ha*e Constructor?
$r6m6t69e s !rapper 0lasses
T7pe 0ast6ng and the conse;
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
10/16
1Core Java Siva Kameswara Rao Munipalle
0ollect6ons Framework
1roup of ;alues 9 ollections :
%llow all values F *nsertion order , %rra#=ist
o duplicates ,
Guick F Random , 7ashSet
Guick F *nsertion 6rder , =inked7ashSet
Guick F Sorted , TreeSet
Ke#, value pairs 9Map :
Random H 7ashMap
*nsertion 6rder , =inked7ashMap
Sorted , TreeMap
%cept6ons
Checked
P These are exceptions that are checked durin0 the compile time
P The compiler forces to handle these exceptions usin0 either tr#catch(" or usin0 throws
3nchecked
These exceptions occur durin0 the runtime&ro0rammer ma# not expect such errors
3ser, )e8ned
www.theknowledgelabs.wordpress.com
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
11/16
11Core Java Siva Kameswara Rao Munipalle
%ppendix
Collectionssort
Collectionss#nchroni?edMap
collectionss#nchroni?ed=ist
Ihat is the di4erence between J)K and JR'
J)K is a complete
&ro0ram ilesJava$dk.LNO.
&ro0ram ilesJava$dk.LNO.bin
&ro0ram ilesJava$dk.LNO.$rebin
Ihere does the 8rst execution startB
)i4erence between 7eap and Stack memor#B
*s Java pass b# value or pass b# referenceB
Java =ibrar# @Java %&*B
*s,% ;s 7as,% relation B
Ihat is the di4erence between 7ashTable and 7ashMapB
)oes importin0 comK=absP import all the sub packa0es like comK=absCoreJavaB
Ihat is the di4erence between de8nin0 and declarin0 variableB
)e8nin0D Strin0 sA
)eclarin0D Strin0 s
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
12/16
12Core Java Siva Kameswara Rao Munipalle
packagecom.KLabs.CoreJava
publicclassFirstProgram {
privatestaticvoidmain(String[] args) {
System.out.println(!ey.." % am in private main.."");
$
$
)o comp6lat6on %rrors
R
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
13/16
15Core Java Siva Kameswara Rao Munipalle
7ashMap Key-ale Pairs
=inked7ashMap Key-ale Pairs
TreeMap Key-ale Pairs
Tree , Sorted 9%scendin0 order:
Set , o )uplicates
7ash ,
Map , ke# value pairs
*terator
The $avautil classes are listed here
%bstractCollection (Java " 'vent 6b$ect &ropert#Resourceundle
%bstract=ist (Java " 1re0orianCalendar Random
%bstractMap (Java " 7ashMap (Java " Resourceundle
%bstractSeQuential=ist (Java " 7ashSet (Java " SimpleTimeone
%bstractSet (Java " 7ashtable Stack
%rra#=ist (Java " *dentit#7ashMap (Java " Strin0Tokeni?er
%rra#s (Java " =inked7ashMap (Java " Timer (Java - v.U"
itSet =inked7ashSet (Java " TimerTask (Java - v.U"
Calendar =inked=ist (Java " Timeone
Collections (Java " =istResourceundle TreeMap (Java "
Currenc# (Java - v.V" =ocale TreeSet (Java "
)ate 6bservable ;ector
)ictionar# &roperties Ieak7ashMap (Java "
'vent =istener&rox# (Java - v.V" &ropert#&ermission (Java "
$avautil de8nes the followin0 interfaces otice that most were added b# Java
Collection (Java " =ist (Java " Random%ccess (Java - v.V"
Comparator (Java " =ist*terator (Java " Set (Java "
'numeration Map (Java " SortedMap (Java "
www.theknowledgelabs.wordpress.com
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
14/16
1:Core Java Siva Kameswara Rao Munipalle
'vent=istener Map'ntr# (Java " SortedSet (Java "
*terator (Java " 6bserver
*er ,idin Conce/ts
packagecom.KLabs.CoreJava;
publicclass/ {
publicvoidmet0o'&ne()
{
System.out.println(Sper Class);
$
$
packagecom.KLabs.CoreJava;
publicclass1 extends/{
publicvoidmet0o'&ne()
{
System.out.println(Sb Class);
$
$
www.theknowledgelabs.wordpress.com
A & /ase 0lass S
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
15/16
1>Core Java Siva Kameswara Rao Munipalle
Super lass Sub lass %llows overridin0
&ublic &ublic Yes
&rivate
protected
default&rivate &ublic
&rivate
protected
default
protected &ublic
&rivate
protected
default
default &ublic
&rivate
protected
default
Thu+b ,uleD ase class methods cannot be private or protected The# can be public @default
The overridin0 method cannot have a more restrictive access modi8er than the method bein0 overridden ('xD You
canWt override a method marked public and make it protected"
You cannot override a method marked 8nal
You cannot override a method marked static
ase class method is Static B
ase class throws exceptions not sub class doesnWt throwB SubClass throws exception but aseClass doesnWt throw exceptionB
ase class method has parameter , double and sub class ,intB
ase class ,(lon0 - int" and sub class (int - lon0" B
packagecom.KLabs.CoreJava;
publicclass/ {
privatevoidmet0o'&ne()
{
System.out.println(Sper Class);
$
$
packagecom.KLabs.CoreJava;
publicclass1 extends/{
www.theknowledgelabs.wordpress.com
8/9/2019 1. KLabsCoreJava Draft Need Cover Page
16/16
1@Core Java Siva Kameswara Rao Munipalle
pblic voidmet0o'&ne()
{
System.out.println(Sb Class);
$
$