Вы находитесь на странице: 1из 53

Java Programming

- 1 - SMS (The best friends group)



UNIT-I
JAVA
History of Java
1990:- The Sun Micro System l li l i ; Language in l i Consumer
Electronic Device -ii i l-ii iii ri | r i James Gosling i ini - li ni ii|
1991:- r lln li ni l ; Programming Language i l-ii ii oops Concept ri rini| nii
; ; Language i i- OAK ii ni ii|
1992:- ; Team i Green Team i- ii i ni i l Home Appliances Hand Made
Devices Controlling i i ni ii| ; Touch Screen ii i- - li i ni|
{Team Leader = Peterson}
1993:- W.W.W. (World Wide Web) ii l nnn Text Base Document i Graphical Format -
Convert li ni nii Green Team -i ,ii r lln li ni l ; iiii i i- -
Tiny Program i l-ii li i| i Internet ni Execute ri nii i ii Computers
iii | ;ri Tiny Programs i Applets rn r |
1994:- Green Team -i Hot Java i- i Web Browser ii l ; Language i i-ni i
i li| Hot Java ,ii ri Applets i Locate ( Execute li ini r |
1995:- Legal Snags i (r OAK i- Java i li ii|
1996:- Java i Internet Programming i Leader ri ri l~ oops Language i General Purpose
Language i- ii ii i ni|
Platform Independency: -






Differences
(1) C & Java: - (a) Java ,ii in - l i (i Unique Statement Sizeof, Goto, Type
Definition i in - ri ni r|
(b)Java, Pointer Type i Support ri ni r |
(c )Java, oops Concept i ni r , l C, Procedural Language r |
(d )Java i Development Sun Micro System ,ii , C i Development Dennis Ritchie ,ii
li ni ii|
(e)Java Pre Processors i i- - ri ni r |
C++ & Java
(a) Java, - Operators Overloading i i- - ri n r |
(b) Java, Pointers i in ri ni r |
(c) Java - Multiple Inheritances i nr Interface i in rini r |
(d) Java - Classes ir li ii i i Function i Variable Declare ri n r | ; Fully
oops r n r |
(e) Java - Header Files i Include ri li ini r | ;i nr packages i use - li ini r |
; l Import keyword i use - li ini r |
(f) Java - Destructor (~) i nr Finalize method i in li ini r |
Note: - Java esa Functions dks Method dgrs gSaA
(g) Java - Templates i i- - ri n r |
Note: - fcuk Object dh Class dks Templates dgrs gSaA
(h) Java - Header Files -ii Packages i Import n r |



Source Code Source Code Source Code
Source Code Source Code
Java Programming
- 2 - SMS (The best friends group)









Java Program Structure:-
(Keyword) class Hello(Class Name)
{
public static void main (String arg [])

Access specifier
{
System.out.println (Hello Java);

class Object Method

}
}
Dos Mode
Compilation: - C:\jdk1.5\bin>javac Hello.java
Execution: - C:\jdk1.5\bin>java Hello


Data Type in Java








Variable Declaration
class My
{
int i;
char c;
byte b;
public display ( ){}
public static void main (String arg [])
{
}
}


Operators
(i) Arithmetic Operator (+, -, /, *, mod, +=, -=, /= , *=)
C++
Java
C
Integer Character Floating
Point
Boolean
Byte
1 Byte
Short
2byte
int
4 byte
Long
int 8
byte
Char String
2 byte 4 byte
Float Double
4 byte 8 byte
True False
Java Programming
- 3 - SMS (The best friends group)

(ii) Assignment Operator (=)
(iii) Relational Operator (>, < , >= , <= , !=, !)
(iv) Logical Operator (&&, || , !)
(v) Boolean Operator (True/False)
(vi) Increment & Decrement Operator (++, --)

Program Structure
<class keyword><class name>
{
var declaration ;
constructor declaration;
method declaration ;
public static void main (String arg[])
{
<class object declarations>;
<calling constructor & methods>;
}
}
- By Default Class Public rini r |
- By Default Method Private rin r |
Variables & Theirs Scopes
Variable: - Programs Execution i i li Value Change rini rni r |
Constant: - Value Fixed rni r|
Variables, Java Programs - Basic Storage l i- - l in r | Variables, Identifiers, Type nii
Initializer i Combination rini r | Variable i Scope i Visibility nii Life Time i iini r|
Variables Declaration: - Java - Variable i in r Declare li ini r |
Syntax: - Type Identifier = Value;
ri Type, Java i Data Type - i; Type rini r i l Class i i- i Interface ri ni r|
Identifiers, Variables i i- rini r |
Dynamic Initialization of Variables: -Variables Dynamic Initialization - i li Program
nnn 2 Variables i Double Value Initialize li ini r| nii nii Variable - ri Value i i- -
n r r- Variable i Dynamic Initialize n r |Valid Expressions i i- - B Variable
Declaration Allow ni r |
class My
{
public static vaid main (String a [])
{
double a = 3.0;
double b = 4.0;
double c;

c = Math.sqrt (a*a+b*b);

class method
System.out.println (The Square root of a and b is:+ c);
}
}
Scope& Life time Variables:- i; ii Variable Main Method ri Declare li ini r | l
Java, Variables i li ii Block - Declare i -ln ni r | Block, Middle Bracket ({}) i
Java Programming
- 4 - SMS (The best friends group)

rini r | nii i ii i-- rini r nii Block ,ii ri Variable i Scope n li ini r| rn i
Language, Variables Scopes i 2 Categories - divide ni r |
(i) Global (ii) Local
Perl, Java - rn Global Variable Scope i in - li i ni r | Java n Scope: -
(i) Variables Defined By Class.
(ii) Variables Defined by Methods
i liilin ni r| Class ,ii Define l n Variable i s Unique Properties nii Attributes ri n
r| i Method - Define Variable n n r |
Scope l Method ,ii define li ni r | {} i rin r , nii i Scope {} ri n rni r |
n r r n r l Java ii Local ( Global Variable i li ni r |r Access li i ni r|
Scope i Nested ii li i ni r |

Type Conversion & Type Casting
Type Conversion & Type Casting i - l lsi Programming Experience ri ni r- r in r l
; ,ii li -i; i Value i -i; i Value Variable - Assign li ini r| l ( ii
Types Compatible r ni Comparison nii Conversion Automatically ri ini r | li -i; i Value
i ( -i; i Value - ri Assign li i ni r , n double i byte - Convert i r, ni
Implicitly Convert ri li i ni r | ; l Type Casting i i(ni ri ni r , l-
InCompatible Type - Conversion li i ni r | n Type Casting ,ii InCompatible Types -
Explicit Conversion l in r |

Automatic Type Conversion: -
; nnn -i; i-i i -i; Variable - Automatic Convert li ini r ni ;
i Conversion i Automatic Type rn r | r Conversion nii ri n r l- i in Satisfy
rini ri :-
(1) ii Types Compatible ri ilr|
(2) Source Type li i Conversion li i ri r , Destination Type si-i rii ilr|
ii ii Satisfy rin r , niii Automatic Type Conversions l i n r |
Example: - byte, short, int, long
Character of Boolean Type Data i Interchange ri li i ni|

InCompatible Types Casting
ii l r- in r l Automatic Type Conversion iii Perform l i n r , nii ( User l
Helpful ii r, il ; i Conversion - li i i Extra Types i i(ni ri ri ni r, n l
li Integer Value i Byte Value - Convert i r , ni (r Automatic Conversion ,ii ri li i
ni r | il byte, integer si-i r | n ; i Conversion Explicitly l in r| n r r
n r , l In Compatible Type - Explicitly Conversion l in r , lr Casting of In
Compatible Types ri ini r| n ; l Syntax l- r
(Target Type) Value
Byte a;
int i;
a = (byte)i;


Java Programming
- 5 - SMS (The best friends group)

Feature of Java
(1) Simple:- Java i Professional Programmers l Learning i in l ii -ii ini r
il l (r Program r Object Oriented Concept ini r ni Java l -l ri rini|
l Programmers C++ s difficult Concepts i ii r-i li ni r , l ii Java i ii ii
ini r|
(2) Object Oriented:- Java i i Language i nr Source Code Depend r i i(ni ri
rini r| ; ii Java Clean, Usable, Pragmatic ((ril) -ii ni r | Java C++ s Concept
Inheritance, Encapsulation, Abstraction, Polymorphism i ii Follow ni r nii ;- ii i Object
li ini r|
(3) Robust (Strong): -Robust Program Create l Java i High Priority i n; r | Java,
Programs i Reliability i Maintain ini r nii Java, Most Common Errors i l Programming i
ri ini r i ii -( ri ii ni r | Java Strictly Type Language r , i Code i Compile Time
ni r , ii ri Code i Run Time ii ni r | Java i Robust i l i Main Reason
l- r
(a) Java - Memory Management i Memory i Allocation i DeAllocation Virtually li ini
r| Allocation Completely Automatically rini r |
il Java - Garbage Collection Facility Available r, i UnUse Objects i ri Discard ni r |
(b) Mis Handled Exceptional Conditions:- Program Environment - s Exception i Java ,ii
-( ri Handle li ini r | divide by Zero, File not Found, Exception etc.
n r n r , Java Programs i Run Time Errors i iii Handle l i n r |
(4) Multi Treaded:- Java i Real Word Requirement i Fulfill l Design li ni r l
Java Programs i Internet i ii Interactive ii i | ; l Java Multi Threaded
Programming i Support ni r | l ,ii Programs rn Operations i nini Perform n
r| il Java ; Run Time System Multi Process Synchronization i Support ni r| l
Interactive System i Smoothly Run li i ni r | Java i Easy to Use Approach nii Multi
Threading ,ii Program i Behaviour ( Speed ii ini r |
(5) Architectural Neutral: - Java i Architectural Neutral ;l ri ini r il ;- Advance
Feature JVM (Java Virtual Machine) rini r | l ,ii Programmers i Write Once Run any
Where any time forever i ri ini r | n lsi Languages - Facility Available ri ii|
(6) High Performance: -Java i Low Power C.P.U. si nr i- l ii Design li ni
r| Java Byte Codes i rn Carefully Design li ini r l l Byte Code i Translation,
Directly Native Code Machine - ri i| ; ii Java i Performance -li High ri ini r nii r
Native Code Conversion JIT (Just in time) Compiler ,ii li ini r | ; ii ; Platform
Independent ii ri ini r| i ; High Performance Cross Platform ii ri ini r |
(7) Compile & Interpreted: -Java Cross Platform ri ii ; Programs i Compile
Intermediate Code - Convert li ini r , l Byte Code rn r | ; Byte Code i JVM ,ii li
ii Machine i Interpret li ini r | ;l ; Compiler i Interpreter rn r |
{ Java Source Code i Compile li ini r , ni Java i Compiler Class File ini r l-
Intermediate Code Generate rini r , Byte Code rn r |}
(8) Distributed:- Java i Distributed Language ii ri ini r | il ; Distributed Environment
l ri ii ni r| r Internet Environment - ii i- ni r , nii TCP/IP Protocols i ii Handle
ni r| Java i Original Version l OAK ri ini r , (r Intra Address Space Messaging (IASM)
i i ii ni r | l ,ii i Different Computer ri r Processors i Object iii l- in
r| ; i Interface Package - --i l n r , l RMI (Remote Method Invocation) ri ini
r| ; ,ii Client/Server Programming - Unpredictable Level of Abstraction i ii ini r|
(9) Dynamic: - Java Programs i Run Time Information Verify nii ii ri Run Time
Object i Access i Permission rini r | ; ii r Possible ri l l i; Code Dynamic
Linking Perform ri r ni (r Safely Execute rini| ; ,ii Applets Dynamically Running System
ii Update ri in r |
Java Programming
- 6 - SMS (The best friends group)

(10) Portable: - Byte Code Conversion ii Java i Portable Language ii ri ini r i l rn
Computer Different Types Operating System ii iii Execute ri in r | l ii r
Portable, Executable Code Generate n r |
(11) Security: - Security i l- Java Strong Language r | r ii ii Programs i Download
li ini r ni l Viral Infection i iii i rni ii| n rn Programmer Programs i
Frequently Download i li| i Downloading r Programs - Virus Scanning
r Download li ini ii| Java, Normal System nii Internet - Firewall i i ni r|
Java Web Browsers i in - li ini r ni Java applets li li Extra Document nii li li
Virtual Infection Download l in r | ; i Java Programs i Protections, Java Web
Browsers ,ii ri li ini r|

Java Environment
Java Environment - Java Developer i Classes nii method i Add li ini r | Java Development
Tools - JDK (Java Development Tool Kit) nii ii ri Classes ( Method i Java i Standard Library
(JSL) - l--ln li ini r| Java Development Kit - Applet Viewer, javac , java, javap, javah ,
javadoc nii jdb i iil- li ini r | JSL - language, Input Output Packages, AWT (Abstract
Window Tool Kit) Applet Packages, Net Packages, Utility i iil- li ini r |

Hardware & Software Requirement In Java
Java i H/W nii S/W Requirement l- r
(1) Windows95
(2) IBM Compatible 486 System
(3) 8 Mb Memory Location
(4) Windows Compatible Sound Card
(5) Hard drive
(6) CD ROM
(7) Compatible Mouse
(8) Windows95 Software
Java Source Code i Compile li ini r ni Java i Compiler Class File ini r l-
Intermediate Code rini r l Byte Code rn r |

Java Virtual Machine
Java i (r Team l- Security nii Portability ii --ii i Solve li (r ii Java Compiler
,ii li ri Byte Code Conversion. Byte Code Conversion Highly Optimized Set of Instruction r,
l Java Run Time System Execute li ini r | (r Machine l ,ii i i Execute li
ini r, r JVM (Java Virtual Machine) rini r | ii - JVM Interpreter r , l Byte
Code Execute li ini r| Java Programs Byte Code - Convert - Programs li ii nr
Environment - iii Execute ri n r r l JVM Implementation i i(ni ri ni r | JVM
i i(ni Platform to Platform nz rini r | nii ii Byte Code i Interpret n r| ; i
Byte Code Interpreter Programs, Portables n r |
Java Programs i Interpretation r Security i - ii rini i ni r | i l Java r
Program i Execution, JVM Control - rini r | JVM, Programs i Safe Environment - Execute
ni r nii System l- (i Side Effects i ii ini r |
Java Program i Interpret li ini r , ni (r Compiled Program i ii ii Execute ri ni r | n
Java - Byte Code Conversion ,ii nii JVM i rini Runtime Systems Programs Fastly
Execute rin r | Sun Micro Systems ,ii Byte Code l JIT Compiler (Just In time) ii i
r i JVM i ri Part r | ; Compiler ,ii ri Byte Code Executable Code - Convert ri ni r, nii
Programs Execute rin r |

Java Programming
- 7 - SMS (The best friends group)

JVM















Architecture of JVM

Control Statement
(1) Simple if : -
Syntax: - if (Boolean Expression)
//Which return true or false
True Statement;
False Statement;
(2) if-else: -
Syntax: - if (Boolean Expression)
True statement
else
False statement
if nii if else Condition, Relational Operators i in - i; ini r | Boolean Expression
nnn Boolean Operators nii Relational Operators i l--ln li ini r |
if Statement ( Top to Down rin r | l if Expression - lii ni Condition True r ni r (r
Condition Execute rini nii ii Condition i Skip li ini|

Switch Case: - Switch Case Statement, Multi Way Branch Statement rin r | r Execution n
i Support n r , l- User Source Code Different Parts i Expressions iii Check
li ini r| r if else if i Problems i ni r |
Switch nnn Expression lii ini r , l- character, boolean, integer, byte etc. Data Type i
Value i Specify li ini r| case Statement - case Match ni r; Value i iii ini r|
Switch Case Statement nnn default Keyword i ii i- - li ini r | case - lii n;
Expressions, True ri ini r, ni caase Statement i Execute n r nii ii ri default Statement ii
Execute rini r| Statement Sequence i Terminate l break Keyword i in li ini r,
l r case Statement - lii ini r |

Syntax: - Switch (Expression)
{
case <Value1>:
1
st
Statements;
break;
case <value2>:
Source Code
Byte Code
Interpreter (JVM)
Object Code
Compile (JIT)
Java Programming
- 8 - SMS (The best friends group)

2
nd
statements;
break;
case <value n>
n Statement;
break;
default:
<statements>
}
Example: -
class Switch_Case
{
public static void main (String a [])
{
for (int i=0; i<6; i++)
{
Switch (i)
{
case 0:
System.out.println (i is zero +i);
break;
case 1:
System.out.println (i is one +i);
break;
case 2:
System.out.println ( i is two + i);
break;
case 3:
System.out.println ( i is Three + i);
break;
default:
System.out.println (I is Greater than 3);
}
}
}
}
;- r- Nested switch case - ii in - n r |

Iterative Statements
Iterative Statement i Programming Language i nr ri Java - ii in - li ini r r ni
i rin r
(1) for
(2) While
(3) Do while
(1) For: - ; nnn s Statements i iz Repeat i rini r ni For Loop i i- - li
ini r|
Syntax: - for (initialization; Condition; iteration)
{
body;
// for loop statements
}
Java Programming
- 9 - SMS (The best friends group)

(2) While Loop: - Conditional Statements i nii Conditional Expressions i Control l
While Loop i in - li ini r | l ini- nn n While Loop - lii n; Condition,
False rini r ni While Loop - li n Statements i Execute ri li ini r |
Syntax: - while (Condition)
{
// body
}
Example: - (while)
class Rev
{
public static void main (String a [])
{
int n=10;
while (n>0)
{
System.out.println(n);
n--;
}
}
}
Example: - (for)
class Rev
{
public static void main (String a[])
{
int n ;
for (n=10; n>0; n--)
{
System.out.println (n);
}
}
}
(3) Do While Statements: - iiz r i( ri ini r l While Loop - li n Statements
i ni Execute ri ri ;l Do while Loop i in - li ini r | ii - User
Termination Condition i Test i irni r , ni Do While Loop i in li ini r|
Syntax: - do
{
// Body of Loop
}
while (condition);
Example: - class Rev
{
public static void main (String a[])
{
int n=10;
do
{
System.out.println(n);
n--;
}
Java Programming
- 10 - SMS (The best friends group)

}
}

Example:- *****
****
***
**
*
class My
{
public static void main (String str[])
{
int i,j;
for (i=0; i<10; i++)
{
for (j = i, j<10; j++)
System.out.print(*);
System.out.println();
}
}
}

Break Statements
r- nin r l Switch Case nnn ri Break Statements i Use - li ini r , n r- r n r l
li ii Loop ir i l Break Statement i in - li ini r | r ii r n r l Break
Statements Loop Termination Conditions l in - in r ri Loop Terminate ri ini r
Program i Control, Loop i i Next Line i ini r | Break Statements l i - in
i - ii i( r
(1) ri Loop nnn rn Break Statement i in - li i ni r n l rn
Break Statement i ii li Loop - nii ini r , ni ini- i Efficiency - ri ni r |
(2) Break Statements i Switch Case - in - li ini r ni (r l Switch Statement i
Terminate l ri i- - li ini r |
-n Break Statement i nii in - li ini r | li Special Condition ,ii Loop ir
ii ri|
Example:-
class My1
{
public static void main (String str[])
{
int i;
for (i=0;i<10; i++)
{
if (i= =5)
break;
System.out.println(i);
}
}
}

Java Programming
- 11 - SMS (The best friends group)

Continue Statements
ii ii ini- nn n ; Loop i Continue ii irn r n li Special Iteration i Condition
- Loop i Processing i i ri i ii ri ni ; i Statements l Continue i
in li ini r| While Loop nii Do While nnn Continue Statements ii Statements
Directly Loop i Conditional Expression i in r| For Loop nnn l Continue
Statements i in - li ini r ni (r r Iterative Statements nii i - Conditional Expressions
i - ini r |
Example: -
class My1
{
public static void main (String str[])
{
int i;
for (i=0;i<10; i++)
{
if (i= =5)
continue;
System.out.println(i);
}
}
}
Output: - 1 2 3 4 6 7 8 9

Jump Statement
Jump Statement i -n : iini - i-i ni r | l- i i define li i i r
(1) Break
(2) Continue
(3) Return
Return Statements:- Return Statements - Method ,ii in - li ini r | li Method -
Return Statement i in li ini r ni Statement ,ii ini- i Control ,ii Call l n
Method i i ini r n r- r r n r l Return ,ii ini- Control Method Caller
i i ini r|
class Return
{
public static void main(String args[])
{
boolean t=true;
System.out.println(Before the return);
if(t)
return;
System.out.println(This is not executed);}}
Java Programming
- 12 - SMS (The best friends group)

Java Internet & WWW
Java nii Internet ii i User Interface Programming l in - li ini r | Java ,ii rn
Free Objects i in r i Internet Space (Cyber Space) Freely Move n r| Network
2 Categories Object i Server nii P.C. i - Transmit li ini r |
(1) Passive Information
(2) Active in Dynamic Programs.
li ii Program i Network Download li ini r ni (r ini- i n n Passive i rni r
n l Execute i | Dynamic ( Active Programs ( Programs r i Self Execute ri i i-ni
in r Programs Client Computer Active Agent i i- n r | n ; Dynamic Program
ii iiz Serious Problems ii - ri ini r | li i -n Portability nii Security ri ni r |
W.W.W.:- World Wide Web 1993 - ii| l ,ii Text Environment i Graphical Base
Environment - Convert li ni|World Wide Web iiz Java Developers Tiny Programs
i i ii ; Tiny Programs i Applets ri i ni| n Java Programs i Web ,ii
accept ri i li ni n n www ;ni ln ri ri ii| Internet Java i Fore Front
Computers Language ri ini r| il Net ii Portable Program i i Efficeint
Language ii l ii User iii i n i| ; i Java ,ii Net Portablity i
Problem i li ni|

Tokens
White space: - Java i Language Free ri ini r | n r r n r l Java - li ii nr
Special Indentation i n ri rini r | li ii ini- - - - r Token i - White Space
rii i r | l li Operator, Seprator etc. i in - i li i ni r | Java - White
Space i Tab i New Line ri ini r |
Identifiers :- Class name, Method Name, Variable name etc. i Identifiers ri ini r| Identifier
Upper case Letters, Lower Case Letters, Numbers, UnderScore i ii i Characters i
Sequence i ri ini r | Identifiers ii ii Number ,ii i ri rin r | l Java, Case Sensitive
Language r n Upper Case/Lower Case Identifier nz rin|
Example:- VALUE Both are different Identifiers
value
Literals :- Java nnn li ii Constant Value i Create l i Literal Representation
i( rini r | Literals i ii ii li ii - in li i ni r |
For Example: - const pi = 3.14;

Java Key Wrods: - Java - 49 Key Words i define li ni r Keywords Operators nii Seprators
i Combined i n r | Key words i Class name, Method name etc. ri li i ni r |
1. abstract 18. finally 34. public
2. assert 19. float 35. return
3. boolean 20. for 36. short
4. break 21. goto 37. static
5. byte 22. if 38. strict fp
6. case 23. implements 39. super
7. catch 24. import 40. switch
8. chare 25. instanceOf 41. syncronized
9. class 26. int 42. this
10. const 27. interface 43. throw
11. continue 28. long 44. throws
12. default 29. native 45. transient
13. do 30. new 46. try
Java Programming
- 13 - SMS (The best friends group)

14. double 31. package 47. void
15. else 32. private 48. volatile
16. extends 33. protected 49. while
17. final

Const nii Goto Keyword i Reserve ni li ni r , n in - ri li ini| ; Key Words
i(i True/False nii Null i Reserve -ii ni r |
Bit Wise Operator
(1) ~ : - Bitwise Unary Not (NOT)
(2) & : - Bitwise & Operator (And)
(3) ! : - Bitwise ! Operator (OR)
(4) ^ : - Bitwise XOR Operator (XOR)
(5) >>: - Shift Right
(6) >>>: - Shift Right 0 fill
(7) << : - Shift Left
For Example: -class Bitwise
{
public static void main (String str[])
{
String binary [] ={0000, 1000, 0100, 1100,
0010, 1010 0110, 1111,
0001, 1001, 0101, 1101};
int a = 2;
int b = 2;
int c = a|b;
int d = a&b;
int e = a^b;
System.out.println (a+b=+ binary (c));
System.out.println (a&b= + binary (d));
System.out.println (a^b=+ binary (e));
}
}

Special Operator Ternary Operator :- r Operator, Three way Operator rini r | ; Operator i
Three way Operator ii ri ini r| r rn if-else Statements i Replace ni r | r C i C++ i
nr Java - ii i ni r |
Syntax: - Expressions1? Expression2 : Expression3;
ri Expression1 i Expression r, i Boolean Value i Valuate ni r | l Expression1
True r, ni Expression2 Value Execute rini ii Expression 3 Execute rini| Expression 2 nii 3
l ri r i( r l Return Time Same rini nii ii - i; ii Void Return ri ni|
Labeled Loops: - ii li ini- - Loops ni in r ni Loops i i- ii li i ni r | n
l Loop i r lii ni r First Label li i ni r | Label, Java i Valid Indentifier r
l is Column (:)i in li ini r l li Block i Labeled Block i li ini r , ni
Label i Break Statement i nr ii in - n r | ; i Loop i Block nii End rini
Last Label i Closing Braces l- ini|





Java Programming
- 14 - SMS (The best friends group)

class Lab
{
public static void main(String args[])
{
boolean t=true;
first:{
second:{
third:{
System.out.println(Before the break);
if(t)
break second;
System.out.println(This is not executed);
}
System.out.println(This is not executed);
}
System.out.println(This is after second block);
}
}
}
Output: -Before the break
This is after second break



UNIT-II
Defining The Class
Class Definition i Class i ; Data Type r| i Class i Define n r ni Objects
,ii in - li i ni r | Class Template r l l Class Object ,ii ri in - n
r| ii Class i define li ini r ni nnn i-i nii ri (i Operations i Declaration
nii definition rini r| Java nnn Classes l i-i i Related Code nz i ni r| Class
i Define l Class Keyword i in li ini r |

Class Structure
class <class name>
{
instance variable1;
instance variable2;
instance variable n;
return type method name1(parameter list)
{
//body of method;
}
return type method name (n parameter list)
{
//body of method;
}
}
Class define variables i instance variables ri ini r nii ln ii Operations Data
Perform l in r, ii i Method nnn ii ini r , n Methods nii Variables Class
Members rin r |
Java Programming
- 15 - SMS (The best friends group)

ii Methods i ii Classes i l Program - define r, - li ii Class - main Method
i i(ni ri rini r| ii ri Applets Declare l in r ni - main i i(ni ri ri ni r|

Constructors: - li ii Object i Creation - ri initialize i i- Constructor ,ii
li ini r| Constructor, Class name ri rini r | Constructors in li ii Access Specifier (Public,
Private etc) i ri nii ini r| ri n l void i ii in - ri li ini r | i Constructor
i Define Object Create n - (r Automatically Objects i intialize ni r |
Constructor i i; ii Return Type ri rini r | il (r Class Type Objects i intialize ni r n
Constructor i Return Type ii Class Type rini r |
(Constructor i i Objects i intialize i rini r nil Code Creation - Objects Fully
initialized ri|)
Constructors i -n 2 Parts - i-i ini r |
(1) Default Constructor:- Default Constructor, Object Creation - Automatically invoke ri ni r|
r i( ri r l ini- - Default Constructor i define li i|
Syntax: - classname objectname = new operator classname();
A obj = new A ();
li ii ini- - Default Constuctor i Explicitly Define ri li ini r | ni Java Automatically
Default Constructor i i ni r | ii Default Constructor ,ii Object i initialize li ini
r, ni ii - 0 Store li ini r| Simple Classes Default Constructor n r |


For Example: -

class Const
{
double d;
public static void main (String str[])
{
Const obj = new Const ();
obj.d = 20;
System.out.println (obj.d);
}
}
ri Java Automatically Default Constructor i Call ni r |
class Const
{
double d;
Const ()
{
d=10;
}
public static void main (String str[])
{
Const obj = new Const ();
System.out.println(obj.d);
obj.d=20;
System.out.println(obj.d);
}
}

Java Programming
- 16 - SMS (The best friends group)

ri Explicity Default Constructor i Call li ni r |
(2) Parametrized Constructor: - Parametrized Constructor i ii Object initialization l i- -
li ini r| Parameterized Constructor i Set l i- - li ini r | iri l l ini-
- Base i Value i Calculate i r ni Parameterized Constructor i in - li ii ilr i l
Box i ii nz ri ni r| ; i Constructor - Object Creation - Parameters Pass
l in r |
For Example: -
class Box
{
double height;
double width;
double depth;
Box (double h, double w, double d)
{
height=h;
width=w;
depth=d;
}
double volume()
{
return height*width*depth;
}
}

class Box 1
{
public static void main (String str[])
{
Box b1= new Box();
double vol= b1.volume ();
System.out.println(Volume is+vol);
}
}
This Key Word: - C++ i nr ri this Keyword i Java - ii in li ini r | iiii Method i
this Keyword i i(ni ni r Method i l Object ,ii Print li ni r, i Object
i Refer i ri ni ; l this Keyword i in li ini r |
Current Object i Refer l li ii Method nnn this i in li i ni r | this i
Current Class Object i Refer l ri ii in - li i ni r |
For Example: -
Box (double n; double w, double d)
{
this.height = h;
this.width = w;
this.depth = d;
}
New Operator: - ii Class i Object ii ini r ni Bracket() Constructor i iini r |
Constructor ,ii r iii ini r l Class i Object Create rini ni i rini| iin r- r r
n r l Constructor Object initalization i i- ni r |
new Operator ,ii Object i Create li ini r n Class, Data Type int, char, boolean etc
l new Operator i in - i i(ni ri rini r i l Java nnn Simple Data Types i
Java Programming
- 17 - SMS (The best friends group)

Object i nr Implement ri li ni r, l~ r Normal Variables i nr Implement li ni r|
Object - rn i i l(iini rini r , l ,ii Object nii Variables - Differents -ni i ni r|
new Operator, Objects i Runtime Memory Allocate i i- ni r | ; ini- Execution
- Class ln ii Object i i n r | Run Time Memory Allocation l New
Operator i in li ini r|]
Garbage Collection
Objects i New Operator ,ii Dynamic Memory Allocation li ini r | n ii New Operator
,ii Memory Allocate i ini r ni ini- Execution n ri Object i Memory ri ni r |
n r ri i ni r l Java - ini- Execution n ri Objects i Memory Allocate ri ni r i -
Objects Destroy ri i Memory Reallocation l i Memory i i Reallocation ri ni
r| C++ - Memory de allocation l Delete Operator i in - li ini r | Java - Memory
i de-allocation Automatically Perform rini r (r Technique l ,ii r i li ini r| Garbage
Collection rini r |
li ii Object i s - n in - ri li ini r ni Garbage Collection ,ii Object i
Destroy Memory i in - li ini r |
C++ i nr Java - Object i Explicity Destroy i i(ni ri rini r | Garbage Collection (
Program Execution i i ri i ni r |
Finalize Methods: - iiii Object i Destroy li ini r n Program ,ii i Object i
s Operation Perform l ri ini r | i ll-iln - Java ,ii Machanism Provide li
ni r l Finalization rn r| Finalization ,ii Specific Actions i Perform li ini r|
Object Garbage Collector i r i rini r |
li ii Class - Finalizer i Add l Finalize Method i in li ini r | Java Run time
Environment ; Method i n Call ni r n Object i Destroy l Garbage Collection
i i i i r | Finalize Method Operation i lii ini r l Finalize Method
,ii i Object Destroy ri r Perform i| Garbage Collector ,ii Periodically Operations
Perform l in r | Finalize Method i Class - l- i define li ini r |
Syntax:- protected void finalize ()
{
// Body of Method
}
protected Access Specifier i Finalize i ii l i li ini r l- Class ir i Code
Access | n r- r r n r l Finalize Method i Garbage Collector Just r Call
li ini r|
Method Overloading:- li Class - i i i li Methods i i- Same ri Declaration of
Parameters nz ri ni ; i Methods i Overload Methods nii ; Process i Method
Overloading ri ini r| Method Overloading, Polymorphysm i ri iri r |
ii Over loaded Methods i invoke li ini r n Java ,ii Arguments i Parameters i
li ini r | l Method Call i - ni ni r | Methods i Different Return Type ii ri
ni r |
For Example: -
class Method
{
void test()
{
System.out.println (Not any initialization);
}
void test (int s)
{
System.out.println (s=+s);
}
Java Programming
- 18 - SMS (The best friends group)

void test (int c, int d)
{
System.out.println (c=+c+d=+d);
}
}
class Method1
{
public static void main (String str[])
{
Method m1 = new Method1 ();
m1.test();
m1.test(10);
m1.test(10,20);
m1.test();
}
}

Example:-
class Method
{
double Area (double l, double b)
{
System.out.println(Area of rectangle is:+l*b);
}
int Area (int b, int h)
{
int a;
a = (b*h);
System.out.println (Area of triangle is+a);
}
double Area (double r)
{
double a;
double pi = 3.14;
a = pi
*
r
*
r;
System.out.println (Area of Circle is: +a);
}
}
class Method1
{
public static void main (String str [])
{
Method obj= new Method( );
obj. Area (3.4, 4.0);
obj. Area (5, 4);
obj. Area (5.5);
}
}
Java Programming
- 19 - SMS (The best friends group)

Inheritance
Inheritance Object Oriented programming l Important r il (r Creation of hierarchical
Classification, Java Allow Mainly Four type of interitance are follows:-
(1) Simple
(2) Multi Level
(3) Hierarchical
Java, Interitance - Multiple Inheritance i nr Interface i in - li ini r |
Example: - class A
{
int a,b;
void display ( )
{
System.out.println (a+a+ b+b);
}
}
class B extends A
{
int c;
void display1( )
{
System.out.println(c+c);
}
void sum ( )
{
System.out.println (a+b+c + (a+b+c)};
}
}
class C
{
public static void main (String str [])
{
A obj1 = new A ( );
B obj2 = new B ( );
obj1. a = 10;
obj1.b = 20;
System.out.println(a+b=);
Obj1.display ( );
System.out.println ( );
obj 2. a = 10;
obj 2. b = 20;
obj 2. c = 30;
obj 2. display ( );
obj 2. display ( );
obj 2. sum ( );
System.out.println ( );
}
}
Inheritance, oop nnn -r-(i -ii ini r il li General Class nnn Logically Related
Items i i li i Class ,ii Items i Access li ini r | l Class i Inherit (ii ini
r, Super Class rn r| nii l Class i inherit li ni r , (r Sub Class rini r |
Java Programming
- 20 - SMS (The best friends group)

Super Key word dk Use: - iiii Super Class nnn Duplicate Code ii iil- ri ini r | l
ini- Inefficient ri ini r n Sub Class i l ii Super Class Members i Access i
Permission rini r| l ii Super Class nnn Vairables i Private Declare li ini r ni Sub
Class Object ,ii Variables i Access i initialized ri li i ni r | n oop Concept -
encapsulation i Primary Atribute -ii ini r | ii li Sub Class i Just i Super Class li
Member i Access i intialize i i(ni rini r ni Java ,ii Super Key word i in li
ini r|
Super Key Word i i General Forms rini r |
(1) Super Key word ,ii Super Class Constructor i Call li ini r |
(2) ; Key Word Super Class Members i Access li i ni r l Sub Class
Member Access ri n r | n r ri i ni r l (r Member Super Class ,ii
Hidden l in r |
Super Class Constructor dks Super Keyword }kjk Call djuk:- i; ii Sub Class, Super Class
Constructor Method i Super Key Word ,ii Access ni r |
Super (Parameter List);
ri Parameter List, Super Class Constructor - Define Parameters i Specify ni r| Sub Class
Constructor - i ri Statement Execute rini r (r super ri r|
Inheritance ds vUrxZr Constructor dks Call djuk:- ii li Class - Constructor i in r ni
r super Class Constructor Call rin, i - Sub Class Constructor i Call li ini|
Example: - B Sub Class r , li Super Class A r| nii B Class Constructor i Call li
ini ni r A Class i Constuctor Automatically Call rini| n Sub Class Constructor -
r Super nnn li n Parameters i Call li ini r , i l Super Class Constuctor i i
i i- n r | l li Class - Super Keyword i in ri li ini r ni By Default Super
Call Perameter test Constuctor Automatically Call ri in r |
Example:- Class A
{
A ( )
{
System.out.println {As Constructor);
}
}
class B extends A
{
B ()
{
System.out.println (Bs Constructor);
}
}
class c
{
public static void main (String str[])
{
B obj. = new B ( );
}
}





Java Programming
- 21 - SMS (The best friends group)

Super Key dks mi;ksx esa ysdj cuk;k x;k izksxzke
Class A
{
int i;
}
class B extends A
{
int i;
B (int a, int b)
{
super.i = a;
i=b;
}
void show ( )
{
System.out.println (Constructor for Super Class and i: = + super.i);
System.out.println (Constructor for sub class and i =:+ i);
}
}
class C
{
public static void main (String str [])
{
B obj1 = new B (1,2);
obj1 show ( );
}
}
Second Use of super Class:-
Class Box
{
double height, width, depth;
Box ( )
{
height = -1;
width = -1;
depth = -1;
}
Box (double h, double w, double d)
{
height = h;
width = w;
depth = d;
}
volume ( )
{
double vol;
vol = height * width * depth;
System.out.println (Volume of Box is + vol);
}
}
Java Programming
- 22 - SMS (The best friends group)

class Box1 extends Box
{
double weight ;
Box ( )
{
super ( );
weight = -1;
}
Box1 (double h, double w, doubled d, double m)
{
super (h, w, d);
weight = m;
}
}
class Box2
{
public static void main (String str [])
{
Box1 obj1 = new Box1 ( );
obj1.weight=20;
obj1. volume ( );
System.out.println (weight of Box is + ob1.weight)
}
}
Method Over Riding:- i; Method l Subclass - Same Name nii Same
Singnature(parameters) ii define li ini r | i l Super Class - Method i Define li
ni r ni (r Method i Sub Class - define r Super Class i Over Ride Method ri ini r |
Overide Methods i Subclass ,ii Call li ini r ni r-ii Subclass Method i ri Call n r
;- Super Class i Method Call ri rini r , i Hidden ri ini r |
Example: -
class A
{
int a,b;
A (int i, int j)
{
a= i;
b= j;
}
void show ( )
{
System.out.println (a and is = + (a+b));
}
}
class B extends A
{
int c;
B (int i, int j, int k)
{
super (i, j);
c = k;
Java Programming
- 23 - SMS (The best friends group)

}
void show ( )
{
super.show ( );
System.out.println (Value of c is+c);
}
}
class c
{
public static void main (String str[])
{
c ob1 = new (1,2,3);
ob1.show ( );
}
}
l n ini- - Show Method i Call li ini r ni l Class Obj. ,ii Method i
Call li ni r, (ri Show Execute rini r | l Super Class Show Method i Access i r ni ;
l ii Super Key Word i in li ini r |
class B extends A
{
int c;
B (int i, intj, intk)
{
super (i,j);
c = k;
}
void show ( )
{
super.show ( );
System.out.println (value of c is:+c);
}
}

Dynamic Method Dispatch (DMD)
Method Over riding Java -r-(i Concept Dynamic Method Dispatch li ni r| DMD
i Mechanism r l ,ii Over ridden Methods i Run time li ini r | n DMD ,ii
Java - Run Time Polymorphism Implement rini r | -n -r-(i Concept f Super Class i
Reference ,ii Call li ini r| ni Java ,ii Run Time r ni nii ini r l l Class
Over Ridden Method i Call li ni r | Different Type Objects i Refer li ini r ni Over
ridden Methods Different Version Call rin r | l Super Class Method Contain ni r i i
i Over Ridden Method Sub Class - r ni Super Class Reference Variable ,ii Subclass l(li
Over ridden method i Sub Class Objects ,ii Refer li i ni r |
Class A
{
void display ( )
{ System.out.println(As method called);}
}
class B extends A
{
void display ( )
Java Programming
- 24 - SMS (The best friends group)

{ System.out.println (Bs method called);}
}
class C extends B
{
void display ( )
{ System.out.println (cs called method);}
}
class D
{
public static void main (String str[])
{
A a1 = new A ( );
B b1 = new B ( );
C c1 = new C ( );
A r;
r = a1;
r.display ( );
r = b1;
r.display ( );
r = c1;
r.display ( );
}
}

Abstract Class
l li i Super Class i define i Declare li i i l Class Structure i nini r| n
Class i Complete Implemantation Provide ri (ini r Abstract Class rini r | Abstract Class i
i Sub Class ,ii Access li ini r | l- Class i Implementation rini r | Super Class
Meaning full Method i Implementation Sub Class ,ii li ini r | User ,ii -( i Class
Liabrary i; i ni r| l- Un Common Methods i Define li ini r | Methods li Sub
Class ,ii Over ride li ini r, nii Super Class - l Methods i Structure define ri ni r ni
; l Abstract Type Modifier i in - li ini r n Sub Class i Responsibility ri ni r l (r
Methods i Implement | Abstract Class i l- i define li ini r |
Syntax: - abstract type name (Parameter list);
i; ii Class l- i li abstract method declare l n ri (r Class ii Abstract Class
rini| li ii Class i Abstract i l class keyword r abstract ni n r | Abstract Class
i Object ri ii ini r| n Abstract Class i new Operator ,ii initialize ri li i ni|
Abstract Constructor nii Abstract Static Methods ii Implement ri l i n r |
Example: -
abstract class A
{
abstract void display ( );
void display1 ( )
{System.out.println ( ] and methods of class A);}
}
class B extends A
{
void display ( )
{ System.out.println (Bs Method); }
}
Java Programming
- 25 - SMS (The best friends group)

class c
{
public static void main (String str [])
{
B obj = new B ( );
obj.display ( );
obj.display1 ( );
}
}
Abstract Methods (i Methods rin r l- Abstract Class nnn Method i l Declaration ri ni
r nii Definition i Abstract Class i Sub Class - define i implement i ini r i ni r |
Final Class
(1) Using Final: - Final Key word i 3 nri in - li ini r |
(a) l i Variables i- Same i r ni Final Keyword i in li ini r |
(b) Final Key Word i Over riding i --i i l ii in - li ini r|
Over riding Java i -r-(i Concept r iiii Over Riding ini- - s Problems Create
ri ini r | n li ii Method i Over Ride ri i l Final Key Word i in li ini r |
l Method i Over Ride ri i r in Final ni li ini r |
Example:-
Class A
{
final void meth ( )
{System.out.println (Method Called);}
}
Class B extends A
{
void meth ( )
{ System.out.println (Bs Method); }
}
l class A - Meth i final Declare li ni r n class B - Over Ride ri li i ni r | ;
Compile Time Error Show rini r|
Java ,ii Methods i Run Time Dynamically Call li ini r | ; Late binding rn r n
final Methods ,ii Over Riding i in - ri li i ni r | nii Methods i Compile Time
ri Re-solve li ini r n ; i i Binding Early Binding rini r |
(c) Using Final to Prevent Inheritance: - Inheritance l Final Keyword i in: -
iiii User, class i Inherit i ri irni r n i l class i Final Key Word ii
lii ini r| li ii Class i Final Declare Methods ii Automatically Final Declare ri
in r| li ii Abstract Class i Final Declare ri li ini r | n Abstract i Final ii ii -
in ri l i n|
Example:-
final Class A
{
void math ( )
{System.out.println (method called);}
}
class B extends A//error is generated because A is final class
(a) Final Variables:- Variables i ii Final Declare li i ni r i Variable
Contents i Modify ii ini r | Final Variable Declare r memory - nr ri ni
r|
Example:- final int i = 10;
Java Programming
- 26 - SMS (The best friends group)

final char a = A;

Understanding Static
iiii User s Class Members i define i irni r , l li Class i Object i in -
li i | Normally Class Members i Class Object ,ii ri in - li ini r | i Member
Create i l li Class Object ,ii in - li i | Static ,ii i( r | ii li Member
i Static Declare li ini r| ni Class Object Create r ri in - li i ni
r| Method nii Variables ii i ri Static Declare li ini r | Static method i si example
main method r | il main i static declare li ini r |;l ; Method i Class Object Create
r ri in - n r | l li Variable i Static Declare li ini r | ni (r Variable
Global Variable rini| Class obj. i Declare li ini r ni Static Variable i i; ii ri ni
r l~ i Class Single Static Variable i ri Access ni r |
li Method i Static Declare li ini r ni l 3 Limitations r|
(a) Static Method l i l Static Method i ri Call ni r |
(b) ; nnn in - li i (ii i-i ii Static ri rini|
(c) Static i this i super Key Word ,ii Refer ri li i ni r |

Visibility Controls
Encapsulation: -Encapsulation Attribute Provide (ini r | l Access Control rn r|
Encapsulation ,ii ini- Different Parts i Control li ini r | l ,ii Program Class
Member i Access ni r| Access i Control Class Misuse i i ni r| Class
Member i Access Specifier iii Access li i ni r | Java ,ii l(li Access
Spedifier i Provide (ii ni r |
Java ds Access Specifier: -
(1) Public
(2) Private
(3) Protected
Java ,ii Default Access Level i ii Provide (ii ni r | Protected i nii in - li ini r |
Inheritance i in - li ni r |
Public Access Specifier i i- - li ini r ni Class i Method nnn lii ni i li
ii Class i Method ,ii Access li i ni r | Main Method i ( Public Access Specifier
,ii i- - li ini r| l i; ii Access Specifier Use - ri li ni r | ni By Default (r Class i
Method Public rini r|
ii li Member i Class in Private Access Specifier nii ini r | ni Class Member
,ii Access ri li i ni r |






Java Programming
- 27 - SMS (The best friends group)


UNIT - III
Arrays
ri i-i -i; i l(li Values i ri Contigious Blocks - --i i ri arrays rin r | Array
ri Type Variables i Group r l Single i Common Name ,ii ii ini r| li ii
Data Type arrays i Declare li i ni r nii i ii Diemension ii ri n r
Array i li ii Value i i Index Value ,ii ii ini r | Related Information i ii
Group i i Array Best Option -ii ini r |

One Diemensional Array (1-D Array)
1-D Array i Declare l r array type i Variable Declare i rini r |
Syntax: - data type arrayvariable name [];
ri Type array i Base Type r | Variable name array i name r nii Square Brackets - array i
Length i Declare li ini r| Array i Value i initialze i ri ni new Keyword i in
li ini r, i array i Memory Location Allocate ni r |
Syntax: - arrayname = new type [size];
For Example: - int month [];
month = new int [12];

Example: - Class A
{
public static void main (String str[])
{
month = new int [12];
month [0] = 31;
month [1] = 28;
month [2] = 31;
month [3] = 30;
month [4] = 31;
month [5] = 30;
month [6] = 31;
month [7] = 31;
month [8] = 30;
month [9] = 31;
month [10] = 30;
month [11] = 31;
month [12] = 30;
System.out.println (April has+month [3]+ days);
}
}
2 D Array:- Java - i i i li arrays i ii Declare li i ni r | ;r array of arrays ii rn
r|
Syntax: - int month [ ] [ ];
month = new int [4] [5];

String:- String Object i Create li ini r ni i String Create rini r Change ri li i
ni| i String Object Create ri ini r ni Character - Change ri n r | String i
Class i Super Class String Buffer rini r l ,ii String nii String Buffer ii Classes,
java.lang nnn define i n; r i Programs i Automatically Provide r| ii Classes i final
Java Programming
- 28 - SMS (The best friends group)

Declare li ni r n (ri ii Classes inherit ri ri ni r ii Classes Single Interface i
in - ni r l char Sequence ri ini r |

String Constructors
String Class - l(li Constructors i define li ni r | ii li Empty String Object i Create
i ri ni String i Default Constructor Use - li ini r |
Syntax: - String s = new String ( );
;i i li Object - initial Value i i i ri ni ; l ii l(li Constructors i in -
li ini r| l String Object i Array of Characters initialize i r ni l l-
Constructor i in - n|
Syntax: - String (char are []);
Exmaple: - char arr [] = {a, b, c, d};
String a = new string (arr);
;i i li Character Array i Sub Range initialize i ri ni l- Constructor i in -
li ini r|
String (char arr [], int start Index, int num char);
Char arr [], {a, b, e, d, c, f};
String a new string (arr, 2, 3,);
Output // a=cde
Example: -
Class Cha
{
public static void main (string str [])
{
char arr []={D, E, L, H, I};
String a = new String (arr);
System.out.println (The string inputted is + a);
}
}
// Output: The string inputted is DELHI
Java i Character Data Type 16 Bit Unicode Character Set i Represent ni r nii String Arrays
8 bit bytes i ASCII Character set - Represent ni r | String Class ,ii s Constructors i ii
Provide (ii ni r i Byte Array i String - Convert ni r |
String (byte arr []);
String (byte arr, int start Index, int num char);
Example:- Class Chars
{
public satic void main (String str [])
{
byte arr []={65,66,67,68,69,70};
String a=new String (arr);
String a1=new String (arr,2,3);
System.out.println (The string is+a);
System.out.println (The string a1 is + a1);
}
}
// Output: - The string a is ABCDEF
The string a1 is CDE

Java Programming
- 29 - SMS (The best friends group)

String Length:- String Length Calculate l length Method i i- - li ini r| l(li
String Operations, Java ,ii define Methods i in - ri Perform l in r, l-
Concatenation Operations - r l + Operator ,ii Perform li i ni ii| String
Conversion - Java, String Operatoin i Concatenation Operator ,ii Convert ni r | Java
nnn Automatic Concatenation Operation Perfrom rini r | n ; Operation i Perform l
String Conversion l Call ni r | Java nnn ii Classes i Super Class, Object Class r | n
String Class Object i Subclass nii valueOf Method String Class i Overloaded Method r|
valueOf Method ,ii User Readeble Data Return li ini r | l User ,ii Call fi ini r |
Objects l valueOf method ,d method toString i call ni r | toString method class
,ii create l n String objects i represent ni r | r String Class ,ii toString method i
Implement li ini r i Object Class - defined r | User -( String Representation l
toString i i- - li i ni r | n Override i i( r |
l(li Character Operation l- r |
(1)charAt():- ; Method ,ii Single Character i Represent li ini r | ;- String - li
Character i Abstract n r |
String s = abc;
s.charAt(1);
output = b

(2) getChar( ):- l ii Characters i li Single String - Abstract i ri ni get Char i
in li ini r | Get Character Method ,ii Source Start nii Ending Point define li ini r| ii
ri Character Array l- Number of Character i ii ini nii ri Characters i Copy i r
Target i ii ini r|
Syntax: -
getChar(Starting Index, end point, target array, target Index);
s= Thid is new cap;
int start = 9;
int end = 11;
char a [] = new char (Start_end);
s. getChar (Start, end, a, o);
Example: -
class String
{
public static void main (String str [])
{
string s = This the new demo class:;
int start = 14;
int end = 17;
char buf [];
buf= new char (start_end)
s.getChar(start, end, buf, 0);
System.out.println (buf);
}
}

(3) getBytes( ):- getBytes() Method i getChars ,ii --i l n Character Array Byte Array -
Convert i- - li ini r | n Get Bytes - Byte Array i --i li ini r| By Default
Characters i Bytes - Convert li ini r |
Java Programming
- 30 - SMS (The best friends group)

String Comparison Method
(1) equals( ) and equalsIgnoreCase:- ii i String i Compare i ri ni equals( ) i in -
li ini r| equals( ) Method Boolean Value i Return ni r nii Parameters - String, Object ni
r|
Syntax: - boolean (Object String)
equalsIgnoreCase Method - Comparison String - ri rini r n Case i Ignore ( ) li ini
r|
Syntax: - boolean equalsIgnoreCase (Obj. String)
Class Stringmet
{
public static void main (String str [])
{
String s1 = Hello;
String s2 = My;
String s3 = Dear;
String s4 = HELLO;
System.out.println (s1+equals+s2+s1. equals (s2));
System.out.println (s1+equals+s3+s1.equals (s3));
System.out.println (s1+equals+s4+s1.equalsIgnoreCase (s4));
}
}
(2) compareTo ( ):- ii i Strings - r Check i ri l i i String greater than, less than i
equals r, ; l compareTo Use li ini r | ;- Strings i Dictionary Order - r nii
ini r l i i String Less than r i i i equals.
compareTo Method ,ii integer value i Return li ini r nii Parameters - String Pass i ini
r|
Syntax: - int compareTo (string str[])

Searching String:- String Class ,ii i Methods Provide (i in r l String Search
Operation Perform l in r ri
(1) IndexOf () Method
(2) LastIndexOf () Method.
indexOf Method:- ; Method ,ii Character i String i i Sub String i First Occurrence i
Search li ini r|
;l indexOf Method integer Return ni r nii Parameters - Character i String i in - ni
r|
Syntax: - int indexOf (int ch/string str[])
Str nnn Sub String ii ini r |
Example:- Class My
{
public static void main (String str[])
{
String s = Hello my dear;
System.out.println(s);
System.out.println (indexOf (m) + s.indexOf (M));
System.out.println (Last Index of (M) (my)+s.lastIndexOf (m, 10));
}
}
lastIndexOf Method:- ; ,ii Character i Sub String i Last Occurence i Findout li ini r |
Syntax: - int lastIndexOf (String str, int start index)
Java Programming
- 31 - SMS (The best friends group)


String Modification Method
(1) subString: - String li si- iin i Extract i Sub String ,ii ri li ini r |
Syntax: - String subString (int start index) start index - sub string ri start rini Address li ini
r|
(2) concate( ):- i string i concate i ri i ii ri ni concate method i in li ini r
-n ; l + Operator i ri in - n r |
Example: - String s1 = new;
String s2 = s1.concate(Delhi);
//Output = new Delhi
Concate Operator i + Operator i nr ii in - li ini r |
Example: - String s1 = new;
String s2 = s1+ Delhi;
//Output = new Delhi
(3) replace ( ):- Replace Method - l li Character i nr li Character i ii ri ni ;i
in - n r |
Syntax: - String replace (char orignal, char replace)
Example: - String s = Amit.replace (A, su);
Output sumit
(4) trim ( ): - Remove White Space
(5) toUpperCase ( ):- String i Upper Case - Convert l
(6) toLowerCase ( ):- To Convert the String in Lower Case.
r Method String Return n r |
(7) reverse ( ):- String Buffer - l-in String i Reverse l String Reverse Method i in
li ini r|
Example: - StringBuffer s = new StringBuffer (This);
s. reverse ( );
System.out.println(s);
li ii i- (alias) i i Super Class - Convert i ri ni ; l Wrappers Classes i in
- li ini r |
Wrappers
Java, Simple Data Types i in - i r int, char, etc. n Data Types Objects i ii - ri
in r r- in r l ii Classes i Super Class, Object Class r nii Object Class - Simple Data
Type i define ri li ni r |
; ii Simple Data Types l Object Class - nz Classes i define li ni r |
(1) Number Class: - Number Class Abstract Class r l Numeric Data Type i Super Class
Key ri ini r| Numeric Data Types nnn Byte, Short, integer, long, double etc. i iil- li
ini r| Number Class ,ii s Abstract Methods ii Provide l n r l Object Value i
Different Number Formats - Convert li i ni r | Double Value Method ,ii Double
Type i Value i define li ini r| ;i nr ii Numeric Data Type l Abstracts Methods
i define li ni r|
byte byteValue ( )
int intValue ( )
long longValue ( )
short shortValue ( )
float floatValue ( )
double doubleValue ( )
; Methods ,ii Return ri (ii Value Rounded ri lin rini r |
(2) Double & Float : - Floating Point Data Type l Wrappers, Float nii Double - ii Float i
Double Value i Call i ri ni l ; Constructor i Call i ni r |
Java Programming
- 32 - SMS (The best friends group)

For Example: - float (float no)
double (double no)
Double Objects i Double Value ,ii Construct li ini r | Float i Double - l- Constants i
l--ln li ni r |
(a.) MAX_VALUE
MIN_VALUE
NAN= Not a Number
POSITIVE_INFINITY
NEGATIVE_INFINITY
TYPE The Class Object of float & Double
(3) Void Class:- Void Class - Field rini r Type l- Void Type i Class Object i
Reference rni r|
Vectors: - Vector Class nnn Class Object i Store ii ini r | r l~ Arrays i nr
ri i n r | ;- ii Object Contigous blocks - Stored rn r ;r Dynamic Array ii ri ini r| r
array list -i ri rin r n - i difference r
(1) Vectors Syncronized rin r n r li Framework Collection i Part ri rin r Java Tool
nnn Vectors i Abstract List ,ii extend li ini r | Vector Class ,ii l(li Constructors
Provide l n r i l- r
(i) Default Constructor Vector ( )
(ii) Vector (int size)
(iii) Vector (int size, int increment)
(iv) Vector (Collection c )
Default Constructor - By Default 0 Value i Insert li ini r | i Constructor - Vector
i initial Catrgory i Size - lin li ini r | Third Type Vectors - Size nii Increment ii i
Specify li ini r| Increment Vector nnn li Specific - ln Elements i Stored ni|
; lin li ini r | Fourth Type Constructor - Collection Element i ii ini r |
Interfaces:- Interface Keyword Fully Abstract Class l in - li ini r | Interface i
li Class - in li ini r ni Class i i i ilr r Interface - ri define li ini r n
li ini r Specify ri li ini r | Interface, Classes i nr ri rin r | n ;- Instant
Variable nii Method i Declare li ini r n i Body i define ri li ini r | n r r
n r l Interfaces - l Variable nii Methods i Declare li ini r | Interfaces - Define
Method i r Class ,ii in - li ini r | Java ,ii interface Keyword i Provide li
ni ni ;i i One Interface, Multiple Method n Fully Utilize li i ni r l Polymorphism
nnn l--ln li ni r|
Interfaces i Dynamic Resolution i Runtime Support l Design li ini r | Interfaces Multiple
Inheritance i nr ri i- - li ini r | Interfaces i -i;- i (ii Problems i l
ri Design li ni r | Interfaces i Classes n define li ini r | nii ri interface i
Different Classes - Implement li i ni r |
Defining Interface:- Interfaces ii Functionality l Applications ,ii Require li ini r i
provide n r l nr C++ - Multiple Inheritance use - li ini r i i Java - Interfaces i
in - li ini r | Interface li Class - l- i define li ini r |
Syntax: - access interface name
{type return type name (Parameter List);
type return type method name (paramenter list);
type final variable name = value1;
}
ri Access Specifier Public ii ri ni r | i public l nii ni ri ni ii Interface i
Implement li i ni r| n ri Interface defining i i nz ri ini r |
(1) l Public Keyword Use - li ni ri ni Interface l i Package Member ,ii
Access li i ni r i; ii Package Interface i in - ri ni r|
Java Programming
- 33 - SMS (The best friends group)

(2) l Interface i Public define li r ni ir li ii Package ,ii in - li i ni
r|
Interface name ri i; ii Valid identifier ri ni r | Interface - define l n Method l
Declare l in r i Definition i Interface - define ri li ini n Methods i Parameter
List i Semicolumn (;) ni Show li ini r | r Class l- Interface i Include li ni r
- Interface ii Methods i Implement i i( rini r |
Variables i ii Interfaces - Declare li i ni r | n ( Variables Implicitly final vkSj static ri n
r lr li ii Interface Implementing Class ,ii Change ri li i ni| ii ri r li
Constant Value Initialize i ii i( rini r ii Methods i Variable By default public ri n
l Interface i public Declare li ni r |
Implementing Interfaces
Interface i i li Class - ii Implement li ini r | l i i li Classes
i inherit l extends Keyword i in - n r i i Interface i li Class -
inherit i ri ni l implements keyword i in li ini r | ri interface i Multiple
Classes - Implement li i ni r | l i nz Interface i ri Class - implement li i
ni r |
interface A1
{ // static final keyword is used for variables
} // Declaration of methods
interface A
2

{//final static keyword is used for variables}
class A
3
implements A1, A2
{ //class Body}

interface Area
{
static final float pi = 3.14;
public void show ( );
}
class Rectangle implements Area
{
float i, b;
void show ( )
{System.out.println (l*b+l*b);}
}
class Circle implements Area
{
float r;
void show ( )
{System.out.println (Area of circle is+pi*r*r);}
}

class Area1
{
public static void main (String str[])
{
Area ref;
Rectangle r = new Rectangle ( );
Circle c = new Circle ( );
ref =r;
Java Programming
- 34 - SMS (The best friends group)

System.out.println (Area of rectangle is+ref.show ( ));
}
}
interface Area
{
static final float pi = 3.14;
public void show ( );
}
class Rectangle implements Area
{
float i, b;
void show ( )
{System.out.println (l*b+l*b);}
}

Extending Interaces: - Class i i Class - inherit li ini r ni i Class - Interface i
Implement ri li i ni r| r- in r l Interface - final static Variables nii Method
Declaration rin r nii Classes - ; Interfaces i use - li ini r ni r Class - Method i ;
Definition rini r i Class i Inherit li ini r ni Class i ii Properties Derived Class - i
ini r|
Example:-
interface A1
{// static c final keyword is use for variable.
} // Declaration of methods
Class A
{
int i. b;
public void show ( )
}
class B extends A implements A1
{ // class body
}
Packages:- Different Classes i li Single Class - Access l ii Classes i Group
- i li ini r l Package rn r | Package i li ii Class ,ii in - li i ni r ;
l import Keyword i in n r | Java Library - ii Main 6 Packages i Define li ni r l-
Number of Classes i Method i Include li ni r |
Package i Classes i nii Methods i i nr in - li i ni r |
(a) Package i ri li Class nnn
(b) User ,ii i; n; External Class
Package i li Class i Method in Public Keyword i in - li ini r ni Classes i
Package ir (ii Class ,ii ii in - li i ni r | i l Public Keyword (ii
Classes ri Method ( Class i in - ni r | n Package Private i Public ii nr i
ni r | Packages - Protected Keyword i in ii li i ni r |



Java Programming
- 35 - SMS (The best friends group)



Javas In builts Packages
JAVA Super Class






(1) Lang: - Lang Package Automatically r Class l Import rini r ; language Package ii
ri ini r| l- Java i Language Related ii Commands, Classes nii Method define
l n r lr java Language ,ii in - li ini r |
(2) Util: - Util Utility Package r l- Languages i Utility i define li ni r | ln ii Utility
Commands lr Java ,ii in - li ini r | l ; Package - l(li Classes nii
Methods define l n r |
(3) Net: - Java Applications Programs - Socket Programming i Remote Server
Programming Concepts i in - li ini r | n net Package i in li ini r| ;
Package - ii nr i Networking Related Classes ( Method i l--ln li ni r |
(4) Applets: - Internet l si-z Programs i JVM i rini Dynamically Run li ini
r r Applets rn r | Applet Realted ii Method i Classes i l Packages - ii ini
r| ri Applet Package i i- li ni r | n Applets ( ii Commands, Applet Package -
define rin r |
(5) i/o: -Java - Input/output Related ii Classes ( Method i I/O Package - ii ni r|
(6) awt: - awt, Java Package i -r-(i Package -ii ni r , l- Window i nr Dialog
Boxes, Button, Labels etc. ii i Facility Available r| n ; Package i Abstract Window
Tool Kit ii ri ini r| awt Package, Window Programming Related ii Classes (
Methods i define ni r|
Creative Packages
(1) System Package: -Packages i Hierarchy Structure - define li ini r | Java, awt
Package i Contain ni r, l- GUI lin ii Classes i Implement li ini r |











Representation of Hierarchy of Java.awt Package
Packages i Access l i nii i ii i ni r |
(1) Package ii l Class i in - li i ri r | i i i- l Operator ,ii nz
li i ni r | iri l l Color Class i Import i r ni l- Syntax lii ini|
import java.awt.Color;
awt Package r l Java Package i Sub Package ii ri ini r ; Approch i nii in - li
ini r awt i l ri Class i Access i ri iiz User awt Related ii Classes i
in - i irni ri ni l l- Syntax i in - li ini r |
Packages
Lang Util net applets I/O awt
Java - Packages
Color
Graphics
Image
awt
Sub Package
Awt Contianer Classes
Awt Contianer Method
Java Programming
- 36 - SMS (The best friends group)

import java.awt.*;
; ii Declarations i ini- Top lii ini r |
Packages, Java Naming Packages i iin Lower Case Letters ,ii l Class i Name i
Package - n li i | n l li ini- - Packages i define i ri ni ; l l- Syntax
in - li ini r |
double y = java.lang.Math.sqrt (x);



Package SubPackage Class Method
Packages i -i i in l r i( r l r Package i Unique name li i| l i
Packages i i- Same ii ini ni ; Run Time Error i ii(i ini r | Packages Create
l r Package Name Declare i| i( rini r ; l package Key Word i in
li ini r| ; i ; Related Different Classes i define li ini r |
Syntax: - package Packagename;
Public class first
{ // body of class
}
ri Class First i Package i ri Part -ii ini r ; Class name ri Save li ini| n
; Save File i Package name (ii Directory - ii ini| nii ; Class i Compile li ini r ni
; (ii Class File ii i Directory - --i rii i( r | Directory i i- ii package i-
ri rii ilr| i r Directory ii Classes i ii Container ni r | -( i Package i l l-
step i in - li ini r|
(1) Package Declare i| (Program i iin)
Syntax: - package Package name
(2) Package - l Classes i ii r r define i nii ii Classes i Public declare i|
(3) Sub Directory Create i ini r l- ii Files i --i li ini r |
(4) Java i- i ii Files i Sub Directory - --i li ini r
(5) Files i Compile i Class File ni r ii Sub Directory - --i li ini r|
; ii Steps l r i( r l Sub Directory i i- (r Package, Hierarchy Structure Maintain
n r | n Package name nnn n i; i Package ri ni li l l- Syntax i in
li ini r|
package packagename.Another Package;
java Package - li Class Definition i ii ini r | l Classes i Package - Define li
ini r| r Public define i|
Accessing Package: -Java System Package i Fully Qualities Class Name iii Access li i
ni r | ; l Import Keyword i in li ini r | User define package i in - l ii
Import Keyword Use - li ini r | ; ,ii Package i List - Class name i Search li i ni
r| Import Statement ,ii li Class i Search l l- Syntax Use - li ini r|
Syntax: - import package1(.package2)(.package3).class name
ri package1 top level package r| package2, package1 i Sub package, package3, package2 i Sub
package r| Class name Package3 - defined class i i- r |
Using Package
Package i l i( ri ni ini- - import li i ni r |
For Example: - package package name;
public class class name
{
public void display ( )
{ System.out.println(As class);}
}
Java Programming
- 37 - SMS (The best friends group)

;- i ii Source File ni r| Subdirectory - --i li ini r | l package1 i li i
Class - Import i ri ni l l- Syntax r|
import package package1;
public class B
{
public static void main (String str[])
{
A obj = new A ( );
obj.display ( );
}
}
ClassB i Compile li ini r ni Compiler i Package1 - l-in A Class File i in - ri
li ini r| ; l Package1. class file i i(ni rini r | package1 i li ini r ni
package1.class file i i classes ii ; iii access |
Import Classes from Other Packages
Packages ,ii ii s Classes Provide (ii ini r lr User i Classes - Import li i ni
r| l i packages i ii in - i r ni Java r Facility Provide ni r |
For Example: -
import program
import package.pakage1;
import pakcage2. *;
class A
{
public static void main (String str [])
{
A ob1 = new A ( );
B ob2 = new B ( );
ob1.display( );
ob2. display ( );
}
}
li Package - Class i Add i li ii Class i package - Add i rn Simple i ri ni r |
For Example: - Package p1;
Public class A;
{ // Body of A;
}
ii li package - li Class i Add li ini r ni Class in public Keyword nii
i( r| l Package p1 - ri Class B i Add i ri ni ; l l- Steps i Follow i
rini|
(1) Class i public define i|
(2) Class definitions r package statement i in - i|
(3) B.java File i Directory P1 - --i i|
(4) B.java File i Compile i Class File ri r ii Directory p1 - --i i|
;i i Non-public Class i ii package - Add li i ni r | l Multiple Classes i Package
i ri ri l- ii Classes Public ri ni ; l l- Step i in - li ini r |
(1) Package Name define i|
(2) Directory Sub Directory ii i - ii Source File i --i i|
(3) Package nnn i (ii ii Classes i n --i i nii r Package Statement
define i|
Java Programming
- 38 - SMS (The best friends group)

(4) Sub Directory - i r Source File i Compile i| l i Directory - Class File
Store ri i|
Hiding Classes
ii li Package i Import li ini r | ni ii - Asterisk (Star) i in li ini r l
ii public Classes Import ri ini r | l s i Classes lr User hide i irni ri ni r Non-
Private Public define li ini r l package import ii ( Classes import ri ri ni r| n
ii li Class i Public define li ini r ni Class i ii Users ,ii i ii Applications
Programs ,ii in - li i ni r |

Java Programming
- 39 - SMS (The best friends group)

UNIT - IV
Multi Treaded Programming
r- in r l Windows is Operating System Several Programs i Simultaneousy Execute ni r ;
MultiTasking ri ini r | System Point of View ii i ni Programs ii One by one
Instructions i Follow n r Execute rin r ; Concept i Multi Treading ri ini r Multi
Treading i~l Programming Concept r ri Programs (Process) i Sub Parts - (Sub Process)
- divide li ini r | nii ii Process Parallel implement rin r |
Example: - Sub Programs Screen Animation lin ni r ni i n n Animation i
Display i n ii ni r n r ri i ni r l i i rn si-si- ii - l(iiln li
ni r | nii r si-i i n User ,ii Independently ( Simultaneously perform li ini r|
Java Programs Single nii Sequential Flow Control i n r | ;- Programs i Sequentially
Execute li ini r |
Thread, ii ini- ri Single Flow Control i n r | Threads i Beginning, Body nii ending
rini r | Thread Body i Sequentially Execute li ini r | li ii ini- - - - Thread ri ni
ri r ls ii Programs - Single Threaded Programs i in - li ini r |
Java i Property Multithreded i Support ni r | l- ini- i Multiple Flow Control Use li
ini r| r ini- i l(li si-si- Programs - l(iiln li r | lr Modules rn r nii -
Module i Threads Consider li ini r | (r ini- i Multiple Flow i Support ni r| Multi
Threaded Program rini r |
(r iiii i Multi Treaded i Support ni r Concurrency rn r | n Java - Threads, Main
Application Programs i Part rini r nii i Same Memory Space Share n r | r Light Wait
Treads i Light Wait Process ri ini r| Threads i Parallel Run li ini r n ;i i r ri r
l ;r Same Time li ini r | il ii Thread Single Processers - rin r | n Threads i
Execution Flow Shared rini r| Java i Interpreter, threads i - Switch Controlling i i ni
r| l Threads Concurrent Run rin r | Multi Threading Concept ii ri Java i i iiii
n -ii ni r | Multi Threading, User i - - rn i i -ln ni r | Programs i
si-si- Threads - Divide li ini r | nii Processor ,ii Parallel Execute li ini r| Threads
i -n Java Web Browser (Hot Java) l in - li ini r |

Creating Threads
Java - Threads Create i Simple r | Threads i Object i Form - Implement li ini r| ;
l Method i in - n r l run() Method rn r | li ii Thread l run Method
Important rini r l- Thread Body nii Thread Behaviour i Implemenmt li ini r |
Syntax: - public void run ( )
{ //body of thred
}
Run Method i Thread Object ,ii Invoke li ini r | Thread i initialize l
Method (Start) i i(ni rini r li ii Thread i i nr Create li i ni r |
(1) Thread Class dks Create djds%& li Class i Thread Class ,ii Extend Run Method
i Over Write i|
Syntax: - Class classname extends
(2) iwjh Class dks Thread esa Convert djuk%& ; l li Class i define Runable Interface
Implement i| Runable Interface - l run Method rini r |
Extending the Thread Class
Class i Runable ii r l nn Thread i li i | ; l java.lang. Thread
i Extend li ini r| ; ii Thread Method i Directly Access n r | ; l l- Steps
i Follow li ini r|
(1) Class Declare i ii ri Thread i class Extend i|
Syntax: - class classname extends Threads
Java Programming
- 40 - SMS (The best friends group)

(2) Run Method i implement i i Code Sequential Execution l Reposible ri ni r |
(3) Thread Object i Create i nii Thread Execution l Start Method i Call i|
(a) Declare The Class
class Thread1 extends Thread
{ // Class body
}
; ,ii i Thread ii ni r i Thread1 r|
(b) Implement the run Method: - run Method i ii Thread Class i Part -ii ni r i
Implement l Method i Over ride i i( r | program - run ( ) l- i Implement
li i ni r |
public void run ( )
{ // body of run
}
Run Method i li Class - Call li ini r ni Class Related ii Actions i Run Method
- Perform n r |
(c) Starting new Thread:- i Thread i l Thread Class i Object ii i( ri ni r |
Thread1 ob1 = new Thread1( );
ob1.start ( );
Code - First Line - Thread1 i Obj. ,ii initialize li ni r | Thread i Obj. ni i li ni
r n Thread Run ri ri ri| Thread i ; Stage i New Born State ri ini r |
i Line i lii ini r l- Obj. ,ii Start Method Call li ni r | ni - Thread
New Born State Runable State - i ini r |
n Java i Run Time Envoirnment Thread l Run Method i invoke ni r | l Thread i
; State l- ini r| l Runing State rn r |
class A extends Thread
{
public void run ( )
{
for (int i = 0; i<5; i ++)
{
System.out.println (i = + i);
}
System.out.println (exit to class A);
}
}
class B extends Thread
{
public void run()
{
for (int j = 0; j<5: j++)
{System.out.println (j= +j);}
System.out.println (exit from B class);
}
class C
{
public static void main (String str[])
{
Thread t1 = new Thread ( );
// A t1 = new A ( );
// B t2 = new B ( );
Java Programming
- 41 - SMS (The best friends group)

t1. start ( );
t2.start ( );
}
}
; ini- nn n i New Thread i Initialize Start li ni r | Threads nini n i n|
Thread i Execution Sequential ri rini| n Output ii Sequential ri i ni| l Thread i
Execute ri i chance l- ini r | - (r Execute ri ini r | n Threads Depend
ri n r n i l Thread Start ri in r ni i Order of Exectuion Decide ri li i ni|
Stoping & Blocking Threads
(1) Stopping the Thread: - li Running State (i Thread i Stop i ri ni l Stop
Method i in li ini r|in Example - l t1 Thread i ii ri ni l l- Syntax i
in li ini|
Syntax:- t1.stop ( );
; Statement ,ii Thread Runing State dead State - i ini r | i; Thread i End State
- rni r ni (r Automatically Dead State - i ini r | li Thread i Premature Death -
ri ri ni l Stop Method i in - li ini r |
Blocking a Thread: - Thread i Temporary Block li i ni r l (r Runnable State - ri
rni| ; l l- Thread Method i in - li ini r |
(1) sleep ( ):- ; Method i l li Thread Specific Time l Block i ri n in -
li ini r|
(2) suspend ():- Suspend i Thread Order Block l in - li ini r |
(3) wait ( ):- Thread i li Specific Condition l Block i ri ni Wait Method Use - li
ini r|
; ii Methods ,ii Thread ii i Thread i sleep( ) ,ii Block li ni ri ni (r s -
in i Specific Time - Runable State - i ini r | supend( ) ,ii Thread i Block
li ini r| i in li ini r| nii wait( ) l notify i in - li ini r |
Life Cycle of Threads
Thread i Life Time - l(li Stages ri nni r
(1) new born state
(2) runable state
(3) running state
(4) blocked state
(5) dead state

Diagram












(1) New Born State:- thread i Obj. Create li ini r n Thread i - rini r l new
Newborn State ri ini r ; State - Thread i ri li ini| ; State - Thread i
Operations i Perform li ini r |
New born state New State
Runable Running Dead
Blocked
sleep
suspend wait
Idle State
Stop
Active
State
Stop
stop
resume notify
Java Programming
- 42 - SMS (The best friends group)

(a) Start Method (b) Stop Method

-n Newborn State i Thread i Next State, Runable rini r l li Method i Apply
li ini r ni Thread Execption Throw ni r |







(2) Runable State:- Runable State - Thread Execution l n i rini r i Processor i
Avalability i Wait n r r r n r l Throw queue - ;--i ri ini r i Execution l
Wait ni r l ii Thread i Same Priority i n; r ni Threads i Execution l different
times slot - divide li ini r i Round Robin Fashion - rini r (r Process l- Threads i
Time Slot - divide li ini r| Time Slice rn r |
Yeild ( ) ,ii thread i Priority i Control li ini r |











(3) Running State:- Running State ni- Thread Execution r| Thread n n ri ni r
n i; High Prority i Thread i i i| i; ii Running Thread l- Situation - ini r |
(a) Thread i Suspend li ni ri| Suspend Running State - i l resume ( )
i in - n r l (r Runable State - i ini r |










(b.) Thread i Sleep ( ) ,ii s l bbk li i| ri Time Mili Second - ri ni n
Thread Specific Time i Queue ir i i i Runable State - i ini r |







New Born
Runable Dead
Start Stop
Runable
. .. .
Yeild
RUNNING RUNABLE SUSPEND
RESUME ( )
Suspend ( )
RUNNING RUNABLE SLEEP
Sleep ( )
Java Programming
- 43 - SMS (The best friends group)

(c.) Thread i Wait (ii ri ni Wait ( ) Use li ini r | Wait Runable State - i l
Notify ( ) i in - n r |







(d) Blocked State:- li Thread i Runable State - i ii ri ni Blocked State i in -
li ini r | Thread i Suspend, Sleep ( Wait ,ii block State - ii ini r | ; State i Not
Runable ii rn r| n li Method i Suspend li ini r i (i Runable State - i
l Resume ( ) i in - n
(e) Dead State: - r Thread i i Life Cycle rini r i Run ( ) ,ii ii Execution i Complete
li ini r ni Thread Dead ri ini r | ; Thread i Natural Death ii rn r l li Thread i
Running State - ri Kill i ri ni Stop ( ) i in - li ini r ; Thread i Premature Death ri
ini r|
Using Threads Methods
Thread Class - Thread Behaviour Controlling ( ) i in - li ini r| Thread i States
i State - i l ri nz Method i in - li ini r | Yeild, Sleep nii Stop
Method, yield method li ii Running Thread i Block li i ni r | ii ri Thread Priority i
li ini r|
Thread Exception
Sleep Method ii li Thread i Runnable State - ii ini r ni Related ii Statement
i Try i Catch Block - lii ini r | il Sleep ( ) ,ii Exeception i Throw li ini r| l
ini- Exception i Catch - Fail ri ini r ni ini- i Compilation ri ini r | Java, Run
System ,ii illegalThreadStateException i Throw li ini r | i; Method Thread Given
Statement i Invoke ri ini|
For Example:- Thread Sleeping Statements - rini r n (r Resume i Instruction i Handle ri
ini| Catch Statement ,ii ii Execption i Handle li ini r |
(1) Catch (ThreadDeath e);
{ // body of catch;
// killed Thread;
}
(2) catch (InterruptedException ( ))
{
// body of catch;
// cant handled in the current state;
}
(3) catch (IllegalArgumentException e)
{ // body of catch;
// illgal method argument;
}
(4) catch (Exception e)
{ // body of catch;
// Any other exception;
}
RUNNING RUNABLE WAIT
Wait ( )
Java Programming
- 44 - SMS (The best friends group)

Thread Priority
r Thread i Prority Assign i ini r l Thread Exception i Schedule n li ini r| r- in
r l Thread i same Priority i ini r | ii Same Priority (i Java Schedule ,ii Equal Treatment
li ini r|
setPriority( ) ,ii Thread Priority i Set li ini r |
Syntax:- Thread name.setPriority (int num);
int num Integer Value r| l Thread Priority i Set li ini r | Thread Class l(li Priority
Constant i define ni r |
(1) MIN_PRIORITY = 1;
(2) NORM_PRIORITY = 6;
(3) MAX_PRIORITY = 10;
; i ; Constant Value i Use - Priority Set i ini r | By Default Thread i Normal
Priority Set rini r| User Level Process - NORM Priotity i in - li ini r | n Back Ground
Task Network I/O, Screen Pointing etc. l -n Minimum Priority i in - li ini r |
High Priority Value i in - i r ni ; l -n iii( i i(ni rini r|

Synchronization
r- in r l Thread ii Date i Method run( ) - define li ini r| l r ii i l Method
Outside define li i ni ; Serious Problem Create ri ini r |
For Example: -l Thread li Specific File Record i Reading ri r i i - i;
Thread File - Write i irni r ni ; Serious Problem Occur ri ni r | ; Problems i
Resolve l Technique i in - li ini r | l Synchronization rn r |
Synchronization ,ii li ii --i i Solve li i ni r | li File - Updation i ri ni
r File i Synchronize li ini r | ; l l- Method i in - li ini r |
Synchronizaton void Update (// Code for Syncrinize) li Method i Synchronize Declare li
i ni r | ni Java Monitor Create ni r i Method First Time Call ii Calls i
Check ni r| n Monitor Thread i hold l r ini r n n i; i Thread
Synchronize Section - Enter ri ri ni r | Monitor ii i nr r l l Threads ,ii ri
Open li ini r| Thread Synchronize Method ,ii i i Complete ni r n Monitor
,ii Next Thread i in - li ini r | i Same Response i in - l n i rni r|
Synchronize Block i l- i lin li ini r |
Syncronize (block object)
{
}
Interesting Condition n - rini r i i i li Threads ri Resource i Control
l Wait ni r ; - dead lock i l-iln - ri ini r | ; l l- Threads r
Threads A
synchronize method 2 ( )
{
synchronize method 1 ( )
Java Programming
- 45 - SMS (The best friends group)

{
;
}
} Thread B
synchronize method 1 ( )
{
synchronize method 2 ( )
}
}
}
Implementing The Running Interface
r- in r l Threads i i i Create li ini r |
(1) Using Thread Class
(2) Implementing Runable Interface
Runable Interface run( ) i Declare ni r l programs - Thread i Implement li ini r ;
l l- Steps i Follow li ini r |
(1) Class i Declare i nii runable interface implement i| Class Implement
Runable
(2) Run ( ) i Implement i| Public void num ( )
(3) Object define Thread i Create i|
(4) Start ( ) ,ii Thread i Call i i i| A. Start ( );








Java Programming
- 46 - SMS (The best friends group)

UNIT - V
Applets
Applet Small Java Programs r lr Internet Computing l in - li ini r | ; Small
Programs i Internet Computer Computer Applet Viewer i Web Browser ,ii
li ini r| li ii Application Program i nr Applets - ii rn i Modifications l i n r|
Applets, Airthmetic Operation, Graphics, Sounds, User input, moving image create animations nii
Inter active games i ii Intract i Develop li i ni r | Java ,ii Fully Interactive Multimedia
i in nii Create li i ni r |
(1) Local Applet
(2) Remote Applet

Local & Remote Applets
Web Pages Applets i i i ii i ni r |
(1) User ,ii -( Applets Develop l i n r i r Web Page ii ini r|
(2) li Remote Computer System Applets i Download li ini r | ni ; Applets i ii
Web Browser - ii ini r |
(3) Applet i Locally develop li ini r ni (r Applet i Local System - ri (ni r l
Local Applet rn r | li Web Page ,ii Local Applet i Find li ini r ni ; l
Internet Connection i i(ni ri rini r| ; Applets i Local System i Directory -
Search li ini r|
Remote Applet i li User ,ii ii ini r i ; Applet i --i ii Remote Computer li
ini r| n ; Applets l Internet Connection i i(ni rini r | l System - Internet
Connection r ni Remote Applet i User System Download li i ni r | Remote
Applet i Load l Web Applet Address i rii i( r | ; Address i URL ri ini
r |
Difference Between Applets & Application
Applets nii Application Program ii ri Java Programming nnn in r n ;- s Difference
r| Applets i Full Featured Application Program ri ri ini il ;r si-z ii i Complete
l ii ini r ;r Internet in l Design li ini r |
Applets nii Application Program - n l- r
(1) Applets ,ii Program Execution l Main Method i in - ri li ini r| Applets
i i li ini r ni Applet Class Method ri Applet i Start ( Execute n r|
(2) Application Programs i nr Applets i independently run ri li i ni r| ; l
Web Page i in - li ini r |
(3) Applets i Local Computer i Files Read i Write ri li i ni|
(4) Network Applets Server Communication ri ni r |
(5) Local Computer Programs i Applet Run ri ni|
(6) Applets i Programming Liabrary C, C++ etc. i in - i lii ri r |
; ii Limitations i System Security l n i li ni r | ; ,ii r -- r l Applets, Local
System i i; i ri rini r |
Preparing To Write Applets
Java, Application Program i Single Main Method i Object Create Execute li ini r|
Applets i Create l l- ini i i - ii i( r : -
(1) Applets i in - li ini|
(2) Applets i ni r |
(3) Applets i i z Features r|
(4) -( i Applets i Create li ini r ni r Start l ; Method i in li
ini|
Java Programming
- 47 - SMS (The best friends group)

Needs for Applets
(1) User, Web Page i Dynamic Look li ini r n Applets i in li ini r |
i -i- l li Web Page i Design li ni r i i List i Bar Chats - Show
li ini r |
(2) Web Page Flesh Output i n rini r n Applets i i(ni rini r |
(3) i Program n i i ri i Internet ii Users i i ri n Applets i
i(ni rini r |
Applets i li r r i( r Java i Applet Viewer i Installed ri| Applets i Test
l l- Steps i in - li ini r |
(1) Applets i n i i| (Java File ii)
(2) Executable Applet Create i| (.Class File ii)
(3) HTML Tag i in - Web Page i Designing i|
(4) Applet Tag i Prepare i|
(5) HTML File Create i|
(6) Applet Code i Test i|
Building Applet Code: - Applet i nr i Class Serivces i in - ni r |
(a) Applet
(b) Classes lr Java Class Liabrary - define li ni r |
Applet Class, java.applet Package Provide (ini r| ; Class - Applet Methods init ( ), start ( ),
paint ( ) i define li ni r | l nr Java Application Program i nr Main Method i in -
li ini r| i i Applet Execution l init ( ), start ( ) i paint ( ) i in - li ini r|
Applet i i li ini r ni Java Automatically, Applet Class ii Function i i ni r|
paint ( ) ,ii Applet Result i Screen ii i ni r | Output, Text, Graphics i Sound s ii
ri ni r | paint ( ) ,ii Graphics Class Object i Argument i nr in - li ini r |
public void paint (Graphics g)
Graphics class i in - l java.awt package i import li ini r | l ,ii Graphics
Class - define ii Output Methods i in - li i ni r | Applet Program i Syntax l- r |
Syntax:-
import java.awt .*;
import java.applet .*;
public class class name extends Applet
{
=
=
public void paint (Graphics g)
{ // Body of paint;
}
}
Example:-
import java.awt.*;
import java.applet.*;
{
public class Applet1 extends Applets
{
=
=
}
public void paint (Graphics g)
{ // Body of paint
g.drawString (Hello world, 10,100);
Java Programming
- 48 - SMS (The best friends group)

}
}
Applet Class, Panel Class i Sub Class r | Panel class, Container class sub class r | Container
class component i sub class r | component class, object class i sub class r , l java.lang package
,ii define li ni r |


















Applet Life Cycle
Applets, applet class ,ii inherit rini r , li Applet i i li ini r ni (r l(li stages
nni r |
(1) Born State/Initialization state
(2) Running state
(3) Idle state
(4) Dead state
(5) Display state (paint)












(1) Initialization State:- Applets i i li ini r ni (r i Initialization State rini r |
; l Applet Class init ( ) i in - li ini r | Applets i Bound Statement - l- i l
i n r |
(1) Applets l i( Object Create i|
(2) Initial Value i Set i|
(3) Images ( Font i i i|
(4) Colors i Setting i|
Java.lang.Object
Java.awt.Component
Java.awt.Container
Java.awt.Panel
Java.awt.Applet
BORN
RUNNING
IDLE
stop ( )
paint ( )
DEAD
Destroy ( )
start ( )
start ( )
Java Programming
- 49 - SMS (The best friends group)

Applet Life Cycle - Initializate l i li i ni r | ; l init ( ) i Override i
i( r |
public void init ( )
{ // body of init & different
initialization
}
(2) Running State:- Applet Running State - nii Enter ni r System ,ii Applet Class
Start Method i Call li ini r | Applet Initialization i r Method Automatically Call
li ini r| ;l Start Method i Override i i( r |
public void start ( )
{ // body of start
}
(3) IDLE STATE/STOP STATE: - Applet i Running State - i i li ini r ni ;
State i Idle State rn r| Applet Automatically Stop ri ini r | ;- Currenlty Running Applet
i si li i li i| Applets i Explicitly ii Stop Method ,ii Stop li i
ni r | l User Applet i l Threads i in - ni r ni Stop Method i in Thread
i i l li ini| Stop ( ) i ii Over ride li ini r |
public void stop ( )
{ // body of stop
}
(4) Dead State:- Applet i Memory Remove li ini r ni (r i Dead State rini r |
Web Browser - li Applet i li i ri r i i - n Close - Press li ini r
ni Browser ,ii Automatically destroy() i Call li ini r | Initiliazation i nr ri Applets i
Destroying State ii ri i ini r | l Applets ,ii rn Resources i Create li i ri r ni
Resources i Clean-up l Destroy Method i in li ini r |
public void destroy ( )
{ // body of destroy
}
(5) Display State:- ; State - Output Operations i Screen Display li ini r | r State
Display State rini r | ; State i Complete l Paint ( ) i in - li ini r | r Applet
Paint ( ) i in - ni r |
public void paint (Graphics g)
{ // body of paint
}
Executable Applets dks Create djuk%& Applet i .class file ri Executable Applet rini r |
Applet Source Code i Compile li ini r ni class File i l-ii rini r | Application Programs i
nr Applets i Compile li ini r |
(1) javac Programname.java
(2) ; Command ,ii .class file i l-ii rini r | l Source (ii Directory - ri ii ini r |
(3) l Applet Program - i; Error ri ni r Check, Correct li ini r l Applet
i Compile n r |

Designing a Web Page
Web Page -n HTML Tags i in - ii ini r | ; ASCII Text Editor - ii Prepare li
i ni r | Web Pages i Extension .HTML rini r | Web Page i iin - r Opening
HTML tag i lii ini r i n - closing HTML tag i </HTML> lii ini r | r tag ni parts -
divide li ini r |
(1) Comment Session (Optional)
(2) Head Session (Optional)
(3) Body Session (Compulsary)
Java Programming
- 50 - SMS (The best friends group)


Applet Tag
Applet Tag i HTML Body Session i Declare li ini r | ; tag - l Applet i i i ri
i i- i Width nii Height i ini r | HTML Code Web Browser i r nini r l li
Applet i i r |
Syntax:- <applet code = .class>
Applet tag - 3 points i Describe li ini r |
(1) Applet i i-
(2) Applet i Width
(3) Applet i Height

Applet To HTML File
<HTML>
<HEAD>
<TITLE> My page </Title>
</HEAD>
<BODY>
<applet code = .class file width = 50 height = 50>
</applet>
</body>
</HTML>
Running the Applets
r- in r l Applets l i Files i in - li ini r
(1) Java File
(2) HTML File
li ii Applet i l l- i Tools i i(ni rini r
(1) Java Supported Web Browser (Hot Java, Netscape Navigator)
(2) Java Applet Viewer
l Web Browser i in - li ini r Web Page Applet i ii i ni r i l Applet
Viewer i in - li ini r ni l Applet Output i ii i ni r |
Applet Viewer
; ,ii HTML tags i ignore li ini r | ;- l <applet> - lii ri code i Run ri ni r |
li ii Applet i l Applet Viewer - (Command Prompt) l- Command lii ini
r |
appletviewer file name.HTML
r i ii i( r l appletviewer.class File i ri ni r | l HTML File i
Execute n r | Applet tag - l- Atributes i Add li i ni r |
(1) Web Page Create l Applet tag i insert i|
(2) Applet tag - Class file i Specify i|
(3) l Class file Current Directory - ri r ni ; l code base Parameters i Specify i|
(a) File i Local System i path ri Provide i|
(b) l File Remote Computer r ni Directory URL i Specify i|
(4) i( Space l Applet i height, width consider i i ni r | ; Specify i|
(5) l User define parameters i in - i ri ni Param tag i Add i|
(6) HTML tags l alterneve ii| l Java i Web Browser is not use.
(7) Applet Declaration i </applet> tag ii close i|

Passing Parameters to Applets
r- in r l User define Paramenters i <applets> - Param tag ii in li ini r | r Param
tag - name, attribute i( r | ri color ( value attribute i ii lii i ni r | n Param tag
Java Programming
- 51 - SMS (The best friends group)

,ii text, constants i different colors i formating according change li i ni r | ri s
tags applet - Show (ii ri ni ; l l- Syntax r
<Param name. text value = Amit> ; i Param tag - Parameters i Add li i ni r |
Parameter i set ( handle l i nii i i(ni rini r
(1) HTML Document - Param tag i add i|
(2) ; tag - parameter pass i| parameters applet, loading - pass l in r | parameters
pass l init method i ii in - li ini r |

Alignment The Display
HTML Document - Alignment Attribute i ii in - li ini r | ; Attribute - 9 - li 1
Value i lii ini r | (Left, Right, Top, Bottom, Texttop, Middle, ABS middle, Base line, ABS
Bottom)
left alignment output page left side - ini r|

Display Numerics
Applets - Numerics Value i ii Display li ini r | r ; Values i String - Convert li
ini r | i Graphics Class drawString Method i in - li ini r | ; i(i li ii
individual value i String - Convert l vlaueOf Method i in - li ini r |
Example:-
public class Applet1 extends Applet
{
public void paint (Graphics g)
{
int value1 = 10;
int value2 = 20;
int sum = value1+value2;
String s= sum of + String.valueOf (sum );
g.drawString (s, 10, 100);
}
}
r ; program i Compile li ini| l .class File i l-ii rini r| r- in r l Applet
i l appletviewer i i(ni rini r | ; Applet Viewer - l HTML Coding (Applet
tag) i ri iii ini r n HTML File ii ii rini|
<HTML>
<APPLET>
CODE = Applet1.class
WIDTH = 400
HEIGHT = 400>
</APPLET>
</HTML>
Getting Input from the user:- r- in r l Applets, Graphical Environment - i ni r |
Applets - Input, String - li ini r | ; l r User Area i Create li ini r , l-
User Text i Inputing | li ii Data type i Values i in - li i ni r | ; l
Applet Package i Text Field, Class i in - li i ni r | ri Text Field i in -
Inputing l Field i l in r i - r Edit ii li i ni r |
Applets Throw i n; Calculating i Retrive l Text Fields - String i Form - Text
Input (ii ini r | ; l li ii Data Type i r String - Convert i i( r | ;
i Input l String Conversion i Calculations Perform l r i Data Type
- Convert li ini r | ;i i Text Field - Output ii String Form - ri iii i ni r | n li
ii Data Type Result i String - convert li ini|
Java Programming
- 52 - SMS (The best friends group)

; l Program Analysis li ni r l- i Numbers i Add i r | ; l Applet ,ii i
Text Field Objects i Declare li ini|
TextField text1, text2
; ii Objects ,ii Text boxes i Represent li ini r ri i numbers i Input li ini|
; l Applet Class init ( ) i Over ride li ini r | l- l- initializations rin r
(1) i TextField Object i Create i| i String i Input |
(2) ; Objects i Applet Display Area - Add i|
(3) ; Contents i By Default 0 initialize i|
; Method i Over ride i paint ( ) i Declare li ini r | ri r 3 integer
Variable nii 3 String Variables i Declare li ini| il r- in r l Text Boxes - String
Values i ri Input li i ni r |
;l ; String i Inputing l getText() Method i in - li ini| i - ; Text Boxes -
Input Strings i Integer - Convert li ini| ; l Integer Class parseInt ( ) i in - li
ini r| ; i ; ii Inputted Data Operations perform l in r | il parseInt ,ii Data
i Integer - Convert li ni r | n ii Operations Integer Operations rin|
Final Result Produce ri ini r n Data i (int) String form - convert li ini r l l
valueOf ( ) i in - n r | i String Class i Method r |

Example: -

import java.awt .*;
import java.applet .*;
public class Input extends Applet
{
TextField text1, text2;
public void init ( )
{
text1= new TextField (20);
text2= new TextField (20);
add (text1);
add (text2);
text1.setText (0); // 0 Initialize
text2.setText (0);
}
public void paint (Graphics g)
{
int x = 0, y = 0, z = 0;
String s1, s2, s3;
g.drawString (Input the value is text boxes, 10, 50);
try // Where the Exception are generated

Java Programming
- 53 - SMS (The best friends group)

t1.getText ( );
x = Integer.parseInt (s1);
s2 = text2.getText ( );
y = Integer.parseInt (s2);
}
catch (Exception e) {} // Exception handler
z = x+y;
s3= String.valueOf (z);
g.drawString (The addition of two no. are, 100, 50);
}
g.drawString (s3, 100, 70)
public boolean action (Event e,Object o)
{
repaint ( );
return true;
}
}

String to
integer
Convert