+ All Categories
Home > Documents > 48304337-CORE-JAVA

48304337-CORE-JAVA

Date post: 06-Jul-2018
Category:
Upload: sunnyb77
View: 213 times
Download: 0 times
Share this document with a friend

of 73

Transcript
  • 8/17/2019 48304337-CORE-JAVA

    1/73

    CORE JAVA

    SunMicrosystem divided the Java into three groups:

    1) J2SE(Java2 Standalone Edition): Deals with developing a standalone application!Core

    Java" AdvJava#

    2) J2EE(Java2 Enterprise Edition): Deals with developing $usiness application %or &nternet

    3) J2ME(Java2 Micro Edition): Deals with developing em$edded system and wireless

    application

    Java Program Execution:

    Features O Java:

    1) Simple: Java is simple programming language 'earning and practicing (ava is easy $ecauseits loo) li)e c"c**

      +ointers are eliminated in (ava due to the %ollowing reason:  i# +ointer creates con%usion %or a programmer

      ii# +ointer may crash a program &% we can,t use pointers properly crash occurs

      iii# -sing pointers virus and hac)ing programs are developed

    2) O!"ect Oriented:

    i) O!"ect: An o$(ect is anything that e.ists in the real world O$(ect will have

     properties and it may per%orm some actions

    Properties: +roperties are represented as varia$les in (ava

    #ctions: Actions are represented as methodsAn O$(ect contains varia$les and methods

    ii) $lass: A class is a model to create the o$(ects Class does not e.ist physicallyA class also contains properties and actions

    3) %istri!uted: Java is distri$uted programming language

    &) 'o!ust: &n Ro$ust we have two %eatures:

    i# E.ception /andlingii# Memory Management

    Memor Mangement: Memory allocation is done $y JVM JVM contains class loader

    .(ava .class

      0yte code

    1

      JVM   Result

      (avac

  • 8/17/2019 48304337-CORE-JAVA

    2/73

      su$system to allot memory Memory Deallocation is done $y JVM2ar$age collection 2ar$age collection automatically removed unused

    o$(ects

    ) Porta!le: Java is system independent All (ava program are porta$le $ecause it will give same

    result anywhere

    *) +ig, Perormance: JVM contains i# J&3 compiler and ii# &nterpreter 

    J-.: Just in 3ime compiler is use%ul to increase the speed o% the e.ecution

    -nterpreter: &nterpreter shows the line num$er

    %emo Program:

    %emo/"ava

    import (avalang45

    class Demo6

     pu$lic static void main!String78 args#

    6

    Systemoutprintln!9/ello orld;9#5

    <<

    import: import is a )eyword which is used to import the pac)ages

     "ava: (ava is a (ava li$rary

    lang: lang is the name o% the de%ault pac)age

    class: Class is a )eyword to de%ine class name

    pu!lic: +u$lic data availa$le to all other programmer

    static: static )eyword is used to call main method without creating o$(ect

    void: void is a return type Main method doesn,t return any value $ut it will e.ecute all the

    Statement

    main: =ame o% the main method

    String0 args: /ere args is the name o% the array 3he type o% the array is String

    Sstem: System is a class

    out: Out is a static %ield Static %ield can,t changed

    >

  • 8/17/2019 48304337-CORE-JAVA

    3/73

    println: =ame o% the method

    JM #rc,itecture:

    JVM is divided into %ive sections:

    1# Met,od #rea: &t contains the class code and method code

    ># +eap: O$(ects are created on /eap memory

    ?# Java Stac: Java Stac) are the places where (ava methods are e.ecuted

    @# P$ 'egister: 3hese registers store memory address o% ne.t instruction to $e e.ecuted $ythe microprocessor

    # 4ative Met,od Stac: 3hese are the places where native methods are store

    $omments: Comments represents the description o% aim and %eatures o% a program 3he mainadvantage o% comments are Reada$ility Reada$ility means understanda$le $y

    everyone 3here are three types o% comments in (ava

    i# Single line comment: 3his comment starts with BBii# Multiline comment: 3his comment starts with B4 and ends with 4B

    iii# %ocumentation comment: 3his comment starts with B44 and ends with 4B

    5: 6,at is an #P- document7

    A: !Application +rogramming &nter%ace# &t is a document containing description o% all the %eatures

    o% so%tware or product or technology e can create our own A+& document $y using (avadoccommand

    5: 6,at is t,e dierence !et8een 9include and import7

    A: include ma)es the compiler to copy the entire header %ile code into a c or c** program 3hus itincreases the sie o% the program and waste memory and processor time

    &mport ma)es JVM to goto the (ava li$rary" e.ecute the code there" comes $ac) and

    su$stitute the result into a (ava program

    5: 6,at is J'E(Java 'untime Environment)7

    A: JREJVM * Java'i$rary

    5: 6,at is command line argument7

    A: 3he values pass to main method is called command line argument 3he values store in Stringtype array

    ?

  • 8/17/2019 48304337-CORE-JAVA

    4/73

    4aming 'ule:

    1# +ac)age names in (ava are written in all small letters E.: (avalang4"(avaio4 etc

    ># Each word o% class name and inter%ace name start with a capital letter E.: 3hread" String?# Method name start with a small letter than each word start with a capital letter E.: read!#"

    read'ine!# etc

    @# Varia$le names also %ollow the a$ove method rule E.: String emp=ame" int emp=um etc# Constant should $e written using all capital letters E.: int %inal +&>>BF" MAG1H etc

    I# All )eywords should $e written in all small letters E.: import" pu$lic" static etc

    %ata .pe: A datatype represents the type o% data stored in memory

    +rimitive Data 3ype or undamental Data 3ype:

    -) -nteger %ata .pe

    Data 3ype Memory Sie

    i# $yte 1$yteii# short > $ytesiii# int @ $ytes

    iv# long K $ytes

    --) Float %atatpe

    i# %loat @ $ytes

    ii# dou$le K $ytes

    ---) $,aracter %atatpe

    i# char > $ytes

    -) oolean datatpe

    i# $oolean 1$it!true or %alse#

    5: 6,at is t,e dierence !et8een loat and dou!le7

    A: %loat can represent upto F digit a%ter decimal point accurately Dou$le can represent upto 1

    digit accurately a%ter decimal point

    Operator: An operator is a sym$ol that per%orm some operations

    E.: A * 0 !/ere * is an operator and A"0 is called operand#

    Operand is a varia$le in which the operator acts An operator can act upon single operand than it iscalled Lunary operator An operator can act upon > operand than it is called L$inary operator &%

    an operator act upon ? operand than it is called Lternary operator

    @

  • 8/17/2019 48304337-CORE-JAVA

    5/73

    Operator in Java:

    1# Arithmetic Operator:*"N" " 4" B

    ># -nary Operator:**" NN?# Assignment Operator: " *" N" 4"

    @# Relational Operator: P" P" Q" Q" " ;

    # 'ogical Operator: " " ;I# 0oolean Operator: " " ;

    F# 0itwise Operators:

    a# 0itwise complement: T $# 0itwise and:

    c# 0itwise or: !pipe sym$ol#

    d# 0itwise GOR:U!cap sym$ol#

    K# Dot Operator:a# 3o re%er to a class into pac)age

      e.: (avautilDate" (avaio0u%%eredReader 

     $# 3o re%er to a method o% a class  e.: Mathpow!#" Empsum!#

    c# 3o re%er to a varia$le in a class  e.: Empname" Empno

    -ncrement Operator: 3his operator increases the value o% a varia$le $y 1 e have > types in

    increment operatorE.: **.

    1# Preincrementation: &n +reincrementation " incrementation has done %irst and

    any other operation are per%ormed ne.t

    E.ample:

    BB+reincrementation e.ample

    class +re

    6

     pu$lic static void main!String78 args#6

    int .F5

    Systemoutprintln!**.#5Systemoutprintln!.#5

    Systemoutprintln!**.#5

    Systemoutprintln!.#5

    <<

    ># Postincrementation: &n +ostincrementation any other operation is done %irst"

    incrementation is per%ormed at the endE.: .**

    E.ample:

  • 8/17/2019 48304337-CORE-JAVA

    6/73

    BBpostincrementation

    class +ost

    6 pu$lic static void main!String78 args#

    6

    int .1K5Systemoutprintln!.**#5

    Systemoutprintln!.#5

    Systemoutprintln!.**#5Systemoutprintln!.#5

    <

    <

     +re+ost E.ample

    class +re+ost

    6 pu$lic static void main!String78 args#6

    int .?5

    BBSystemoutprintln!**.4.**4.**#5

    Systemoutprintln!**.4.**4**.#5Systemoutprintln!.#5

    <

    <

    .,e precedence o "ava operators:;;

    1# &ncrement and Decrement Operator 

    ># Arithmetic Operations

    ?# Comparisons@# 'ogical Operations

    # Assignment Operations

    /ighest!# 78

    ** NN T ;

    4 B

    * N

    QQ QQQ PP

    Q Q P P

    I

  • 8/17/2019 48304337-CORE-JAVA

    7/73

    ;

    U

    :

    op

    'owest

     =ote: &% eWual precedence than precedence will count %rom le%t to right

    E.ample:

    class Ope3est

    6

     pu$lic static void main!String78 args#6

    int a1H4@*>HB>N5

    int $1H4!@*>H#B>N5int c!1H*>H#B*5

    int d!1H*>H#B!*#5

    Systemoutprintln!a*9 9*$*9 9*c*9 9*d#5

    Systemoutprintln!9Value o% a isNNQ9*a#5Systemoutprintln!9Value o% $ isNNQ9*$#5

    Systemoutprintln!9Value o% c isNNQ9*c#5

    Systemoutprintln!9Value o% d isNNQ9*d#5<

    <

    .ernar operator:

    class 3ernary6

     pu$lic static void main!String args78# 6

    int i >H5

    int ( 5int H5

    F

  • 8/17/2019 48304337-CORE-JAVA

    8/73

    BB i P ( i : (5 BB ternary operator!output is >H# i Q ( i : (5 BB ternary operator!output is #

    Systemoutprintln!93he value assigned is 9 * #5

    <<

    'everse:

    class Reverse

    6 pu$lic static void main!String78 args#

    6

    int rH"revH5

    int nI?IF@5while!nQH#

    6

    rn1H5rev!rev41H#*r5

    nnB1H5<Systemoutprintln!93he Reverse =um$er isNNQ9*rev#5

    <

    <

    $ontrol Statement: E.ecuting the statement one $y one is called SeWuential E.ecution

      E.ecuting the statement randomly is called Random E.ecution

    Random E.ecution is use%ul to write $etter program Random E.ecution is possi$le $y using control statement Control statements are the statements

    which change the %low o% e.ecution o% a program

    1# i;else statement: 3his statement is use%ul to per%orm a tas) depending upon whether a

    condition is true or %alse

    E.ample 1:

    class &%3est

    6  pu$lic static void main!String78 args#

    6

    int numN@5

    i%!numH#6

    Systemoutprintln!9&t is ero9#5<

    else i%!numQH#

    6

    Systemoutprintln!9&t is positive9#5

    K

  • 8/17/2019 48304337-CORE-JAVA

    9/73

    <else

    6

    Systemoutprintln!9&t is negative9#5<

    <

    <

    E.ample >:

    BB=ested i%Nelse

    class &%Else6

     pu$lic static void main!String args78#

    6int month @5

    String season5i%!month 1> month 1 month >#

    6

    season 9inter95

    <

    else i%!month ? month @ month #6

    season 9Spring95

    <else i%!month I month F month K#

    6

    season 9Summer95<

    else i%!month X month 1H month 11#

    6

    season 9Autumn95<

    else

    6season 90ogus Month95

    <

    Systemoutprintln!9April is in the 9 * season #5

    <

    <

    ># do;8,ile loop: 3his loop is used to repeat early e.ecute a group o% statements as long as

    X

  • 8/17/2019 48304337-CORE-JAVA

    10/73

  • 8/17/2019 48304337-CORE-JAVA

    11/73

    BB%or! i15iP>H5i**#6

    Systemoutprintln!i#5

    <BBSystemoutprintln!i#5

    <

    <

    # s8itc, statement: Switch statement is use%ul to e.ecute a particular tas) %rom among

    several tas) depending upon the value o% a varia$le

    E.ample 1:

    class Switch3est6

     pu$lic static void main!String78 args#

    6char colorYgY5

    switch!color#6case YrY:Systemoutprintln!9red9#5

     $rea)5

    case YgY:Systemoutprintln!9green9#5

     $rea)5case Y$Y:Systemoutprintln!9$lue9#5

     $rea)5

    case YwY:Systemoutprintln!9white9#5 $rea)5

    case YpY:Systemoutprintln!9pin)9#5

     $rea)5de%ault:Systemoutprintln!9=o condition9#5

    <

    <

    <

    E.ample >:

    class Switch3est1

    6

     pu$lic static void main!String78 args#

    6char colorY>Y5

    int a1H"$>H5switch!color#

    6

    case Y1Y:Systemoutprintln!a*$#5

     $rea)5case Y>Y:Systemoutprintln!aN$#5

    11

  • 8/17/2019 48304337-CORE-JAVA

    12/73

     $rea)5case Y?Y:Systemoutprintln!a4$#5

     $rea)5

    case Y@Y:Systemoutprintln!aB$#5 $rea)5

    case YY:Systemoutprintln!a$#5

     $rea)5de%ault:Systemoutprintln!9=o condition9#5

    <

    <<

    Jump Statement:

    rea statement 1:

    BBhen a $rea) statement is encountered inside a loop" the loop is terminated and programcontrol resumes at the ne.t statement %ollowing the loop

    BB -sing $rea) to e.it a loopclass 0rea)'oop

    6

     pu$lic static void main!String args78#

    6%or!int iH5 iP1HH5 i**#

    6

    BBi%!i 1H# $rea)5 BB terminate loop i% i is 1HBBH to XBBi%!i 1H# continue5 BBH to1HH e.cept 1H

    i%!!i> H## continue5

    Systemoutprintln!9i: 9 * i#5<

    Systemoutprintln!9'oop complete9#5

    <

    <

    rea statement 2:

    BB -sing $rea) with nested loops

    class 0rea)'oop>

    6 pu$lic static void main!String args78#

    6%or!int iH5 iP?5 i**#

    6

    Systemoutprint!9+ass 9 * i * 9: 9#5

    %or!int (H5 (P1HH5 (**#6

    1>

  • 8/17/2019 48304337-CORE-JAVA

    13/73

    i%!( 1H# $rea)5 BB terminate loop i% ( is 1HSystemoutprint!( * 9 9#5

    <

    Systemoutprintln!#5<

    Systemoutprintln!9'oops complete9#5

    <<

    $ontinue statement:

    BB Demonstrate continue

    class Continue

    6 pu$lic static void main!String args78#

    6

    %or!int iH5 iP1H5 i**#6

    Systemoutprint!i * 9 9#5i% !i> H# continue5Systemoutprintln!99#5

    <

    <

    <

    'eturn Statement:

    BB3he return statement is used to e.plicitly return %rom a method 3hat is" it causes program

    control to trans%er $ac) to the caller o% the method

    BB Demonstrate return

    class Return

    6 pu$lic static void main!String args78#

    6

     $oolean t true5

    Systemoutprintln!90e%ore the return9#5

    i%!t# BBreturn5 BB return to caller 

    Systemoutprintln!93his wonYt e.ecute9#5

    <

    <

    #ccept t,e value rom e!oard:

    1?

  • 8/17/2019 48304337-CORE-JAVA

    14/73

    Stream: A stream represents %low o% data %rom one place to another place

    a) -nputStream: &nputStream read or receive data

    !) OutputStream: OutputStream send or write data to some other place

    4ote: #ll stream classes are availa!le in "ava/io pacage/

    Sstem class:

    1# Systemin !represent )ey$oard#

    ># Systemerr !represent monitor and display error messages#

    ?# Systemou !represent monitor and display general messages#

    E.ample 1:

    BBAccepting data %rom )ey$oardimport (avaio45

    class &nput6

     pu$lic static void main!String78 args# throws E.ception

    6

    B4&nputStreamReader o$(new &nputStreamReader!Systemin#5

    0u%%eredReader $rnew 0u%%eredReader!o$(#54B

    0u%%eredReader $rnew 0u%%eredReader!new &nputStreamReader!Systemin##5

    Systemoutprintln!9Enter a value9#5

    BBchar ch!char#$rread!#5

    String ch$rread'ine!#5BBint ch1&ntegerparse&nt!ch#5

    %loat ch1loatparseloat!ch#5

    dou$le ch1Dou$leparseDou$le!ch#5

    Systemoutprintln!9Zou have enteredNNQ9*ch1#5<

    <

    E.ample >:

    BBAccepting Employee data

    import (avaio45class Empdata

    6 pu$lic static void main!String78 args# throws E.ception

    6

    0u%%eredReader $rnew 0u%%eredReader!new &nputStreamReader!Systemin##5

    Systemoutprintln!9Enter &d9#5int id&ntegerparse&nt!$rread'ine!##5

    1@

  • 8/17/2019 48304337-CORE-JAVA

    15/73

    Systemoutprintln!9Enter se.!MB#9#5char se.!char#$rread!#5

     $rs)ip!>#5

    Systemoutprintln!9Enter =ame9#5String name$rread'ine!#5

    Systemoutprintln!9&dNNQ9*id#5

    Systemoutprintln!9Se.NNQ9*se.#5Systemoutprintln!9=ameNNQ9*name#5

    <

    <

    #rra: An array represents a group o% elements o% same data type 0y representing a group o%elements with a single array we can ma)e programming easy

    .pes o #rra:

    i# One dimensional array 1Dii# 3wo dimensional array >D

    iii# Multi dimensional array ?D

    1# One dimensional arra 1%: One dimensional array represents a single row o% data or a

    single column o% data

    $reating One dimensional arra:

    i# e can declare and initialie a one dimensional array using assignment operator  E.: int mar)s7861H">H"?H"@H"HH5

      mar)s7>8?H5

      mar)s7?8@H5

      mar)s7@8H5

    E.ample 1:

    BBOne diamansional array e.ampleimport (avaio45

    class Arr16

     pu$lic static void main!String78 args# throws E.ception

    6

    0u%%eredReader $rnew 0u%%eredReader!new &nputStreamReader!Systemin##5Systemoutprintln!9/ow many su$(ects9#5

    1

  • 8/17/2019 48304337-CORE-JAVA

    16/73

    int n&ntegerparse&nt!$rread'ine!##5int mar)s78new int7n85

    %or!int iH5iPn5i**#

    6Systemoutprintln!9Enter Mar)s9#5

    mar)s7i8&ntegerparse&nt!$rread'ine!##5

    <int totH5

    Systemoutprintln!93he mar)s are9#5

    %or!int iH5iPn5i**#6

    Systemoutprintln!mar)s7i8#5

    tottot*mar)s7i85

    <Systemoutprintln!93otal Mar)s9*tot#5

    %loat percent!%loat#totBn5

    Systemoutprintln!9+ercent 9 *percent#5Systemoutprintln!93he length o% array9*mar)slength#5

    <<

    E.ample >:

    BB3he values pass into the runtimeBBimport (avalang45

    class Arr>

    6 pu$lic static void main!String a78#

    6

    int nalength5Systemoutprintln!9=oo% args9*n#5

    Systemoutprintln!93he args are9#5

    %or!int iH5iPn5i**#

    Systemoutprintln!a7i8#5<

    <

    E.ample ?:

    BBCharacter Array!1D#

    class CharArray6

     pu$lic static void main!String78 args#6

    char ch786YAY"Y0Y"YCY"YDY

  • 8/17/2019 48304337-CORE-JAVA

    17/73

    ># .8o dimensional arra 1%: 3wo dimensional array represents several rows columns o%

    elements

    $reating .8o dimensional arra:

    i# e can declare and initialie a two dimensional array using assignment operator  E.: int mar)s7878661H">H"?H"I"I@

  • 8/17/2019 48304337-CORE-JAVA

    18/73

    Systemoutprintln!#5<

    <

    <

    E.ample ?:

    BB&n (ava"a multiNdiamensional array is an array whose components are themselves arrays

    class CharMultiArray

    6 pu$lic static void main!String78 args#

    6

    char ch787866YAY"Y0Y"YCY"YDY

  • 8/17/2019 48304337-CORE-JAVA

    19/73

      modi%iedE.: String

    E.ample 1:

    class Str1

    6 pu$lic static void main!String78 args#

    6

    String s193his is (ava95Systemoutprintln!9hashcode %or s1NNQ9*s1hashCode!##5

    String s>new String!9& li)e9#5

    Systemoutprintln!9hashcode %or s>NNQ9*s>hashCode!##5

    char arr786YaY"YpY"YtY"YeY"YcY"YhY9*s>#5Systemoutprintln!9s?9*s?#5

    Systemoutprintln!93he no o% character in s1 including spaces9*s1length!##5Systemoutprintln!9s1 (oin with s> with s?9*s1concat!s>#*9 9*s>concat!s?##5

    BB Systemoutprintln!s1*9 9*s>*9 9*s?#5

     $oolean .s1startsith!93his9#5

    i%!.#

    Systemoutprintln!9s1 starts with 3his9#5else

    Systemoutprintln!9s1 does not starts with 3his9#5

    String rs1to-pperCase!#5String ss>to-pperCase!#5

    String r1s1to'owerCase!#5

    Systemoutprintln!9A%ter converting s19*r#5Systemoutprintln!9A%ter converting s>9*s#5

    Systemoutprintln!9A%ter converting s19*r1#5

    <

    <

    E.ample >:

    class Str>

    6

     pu$lic static void main!String78 args#

    6String s19/ello95

    Systemoutprintln!9hashcode %or s1NNQ9*s1hashCode!##5String s>9/ello195

    Systemoutprintln!9hashcode %or s>NNQ9*s>hashCode!##5

    BBi%!s1s>#

    i%!s1eWuals!s>##Systemoutprintln!90oth are eWuals9#5

    1X

  • 8/17/2019 48304337-CORE-JAVA

    20/73

    elseSystemoutprintln!90oth are not eWuals9#5

    <

    <

    E.ample ?:

    class Str?

    6

     pu$lic static void main!String78 args#6

    String s19/ello95

    Systemoutprintln!s1#5

      String s>9/ai95s1s1*s>5

    Systemoutprintln!s1#5

    <<

    B42ar$age Collector remove the previous value and storing the new value4B

    E.ample @:

    String Methods:

    BB1# code+ointAt!#:NN 3he code+ointAt!# method returns the character!-nicode code point# at the

    speci%ied inde.

    BB># code+oint0e%ore!#:NN 3he code+oint0e%ore!# method returns the character!-nicode code point#

     $e%ore the speci%ied inde.

    BB?# code+ointCount!#:NN 3he code+ointCount!# method returns the num$er o% -nicode code points

     $etween two indices in the String

    BB@# startsith!#:NN 3he startsith!# method returns a $oolean value to test whether the string startswith a speci%ied pre%i.

    BB# endsith!#:NN 3he endsith!# method returns a $oolean value to test whether the string endswith a speci%ied su%i.

    BBI# to-pperCase!#:NN3he to-pperCase!# method converts the characters in the string to upper case

    BBF# to'owerCase!#:NN3he to'owerCase!# method converts the characters in the string to lower

    case

    BBK# valueO%!#:NN3he valueO%!# method returns the string representation o% the speci%ied

    argument3he argument can have any one o% the values:$oolean" char" %loat" dou$le" int" long etc

    >H

  • 8/17/2019 48304337-CORE-JAVA

    21/73

    BBX# toCharArray!#:NN 3he toCharArray!# method copies the content o% the String to a new characterarray

    BB1H# eWuals&gnoreCase!#:NN 3he eWuals&gnoreCase!# method compares two strings" ignoring caseand returns a $oolean value &% the strings are eWual the method returns a true value" otherwise

    %alse

    class StringMethod

    6

     pu$lic static void main!String78 args#6

    String str9Aptech 2lo$al 'earning Solutions95

    String str19A+3EC/ 2'O0A' 'EAR=&=2 SO'-3&O=S95

    char78 array6YAY"YpY"YtY"YeY"YcY"YhY"Y Y"Y2Y"YlY"YoY"Y$Y"YaY"YlY1

  • 8/17/2019 48304337-CORE-JAVA

    22/73

    BB@# length!#:NN 3he length!# method returns the total num$er o% characters %rom the String0u%%ero$(ect

    BB# replace!#:NN 3he replace!# method replaces characters %rom the String0u%%er o$(ect with newcharacters

    BBI# setCharAt!#:NN 3he setCharAt!# method replaces a character %rom the String0u%%er o$(ect witha new character at the speci%ied inde.

    BBF# set'ength!#:NN 3he set'ength!# method sets the length o% the String0u%%er to a new value

    BBK# capacity!#:NN 3he capacity!# method returns the current capacity o% the String0u%%er o$(ect 3he

    capacity is the amount o% storage availa$le %or newly inserted characters" $eyond which an

    allocation will occur

    BBX# su$string!#:NN 3he su$string!# method creates a new string %rom the string0u%%er o$(ect

    class String0u%%erMethod

    6 pu$lic static void main!String78 args#6

    String0u%%er s$new String0u%%er!9Aptech 2lo$al 'earning Solutions9#5

    String0u%%er s$1new String0u%%er!9Aptech 2lo$al 'earning Solutions9#5

    Systemoutprintln!s$charAt!F##5BB2Systemoutprintln!s$deleteCharAt!##5

    BBAptec 2lo$al 'earning Solutions

    char78 arraynew char71H85s$1getChars!H"I"array"H#5

    Systemoutprintln!array#5BBAptech

    Systemoutprintln!s$1length!##5BB?>Systemoutprintln!s$1replace!>?"?>"9Services9##5

    BBAptech glo$al 'earning Services

    s$1setCharAt!F"YgY#5

    Systemoutprintln!s$1#5BBAptech glo$al 'earning Services

    BBs$1set'ength!?#5

    Systemoutprintln!s$1#5BBAptech glo$al 'earning Services

    s$1set'ength!1?#5

    Systemoutprintln!s$1#5

    BBAptech glo$alString0u%%er s$>new String0u%%er!#5

    Systemoutprintln!s$>capacity!##5BB1ISystemoutprintln!s$1capacity!##5BB@K

      BBcapacitylength*1I

      String str5

    strs$1su$string!F#5Systemoutprintln!str#5BBglo$al 'earning Services

    >>

  • 8/17/2019 48304337-CORE-JAVA

    23/73

    strs$1su$string!F"1?#5Systemoutprintln!str#5BBglo$al

      <

    <

    OOPS(O!"ect Oriented Programming Sstem)

    3he languages li)e c" pascal" %ortron these are called procedural languages $ecause they %ollow an

    approach called procedural approach

    3he languages li)e c** and (ava called o$(ect oriented $ecause they %ollow an approach called

    o$(ect oriented programming approach

    Features o OOPS:

    1) O!"ect and class

    O!"ect: An o$(ect is anything that e.ists in the real world An o$(ect contains properties

    and Actions +roperties are represented as varia$les Actions are represented as

    methods So an o$(ect contains varia$les and methods Class does not e.ist physically $ut O$(ect does e.ist physically

    $lass: A class is a model or idea to create the o$(ects rom the class we create o$(ects Aclass also contains varia$les and methods An o$(ect does not e.ist without a class $ut a

    class e.ist without any o$(ects

    2) Encapsulation: riting varia$les and methods as a single unit is called encapsulationClass is an e.ample o% encapsulation 3he advantage o% encapsulation is

    the programmer can use same name %or the mem$er o% two di%%erent

    classes

    3) #!straction: /iding unnecessary data %rom the user is called a$straction

    &) -n,eritance: +roducing a new class %rom an e.isting class is called inheritance 3he newly

     produced class automatically $uilt inherit all the %eatures o% the e.isting

    class Reusa$ility o% code is the main advantage o% inheritance

    ) Polmorp,ism: &% something e.ist in various %orms it is called polymorphism &% a single

    method per%orm various tas) it is called polymorphism

    *) Message passing: Calling a method is called message passing

    #ccess Speciier: An access speci%ier is a )ey$oard that speci%ies how to access the mem$ers o%class or the class itsel% 3here are %our access speci%ier in (ava:

    1) Private: +rivate mem$er o% a class are not accessi$le in another class either in the same

     pac)age or in another pac)age 3he scope o% private speci%ier is class scope

    >?

  • 8/17/2019 48304337-CORE-JAVA

    24/73

    2) Pu!lic: +u$lic mem$ers o% a class are availa$le in another class o% same pac)age or o% ananother pac)age 3he scope o% pu$lic spci%ier is glo$al scope

    3) Protected: +rotected mem$ers are availa$le in another class in the same pac)age $ut theyare not availa$le in another pac)age class

      =ote: +rotected mem$ers are always availa$le to su$classes!inheritance# in the

    same pac)age or another pac)age

    &) &) %eault: De%ault mem$ers are availa$le in another class in the same pac)age $ut they

    are not availa$le in another pac)age class 3he scope o% de%ault speci%ier is pac)age scope

    Pro!lem 8it, Private speciier:

    Pro!lem 1:

    class +erson6

     private String name5 private int age5BBString name5

    BBint age5

    void tal)!#

    6Systemoutprintln!9/ello & am NNQ9*name#5

    Systemoutprintln!9/ello My Age NNQ9*age#5

    <<

    class irst3ype

    6

     pu$lic static void main!String78 args#

    6

    +erson p1new +erson!#5 p1tal)!#5

    +erson p>new +erson!#5 p>name9san95

     p>age>5

     p>tal)!#5

    <<

    B4note: De%ect in this program is i% we declare the varia$les as private we canYt use that varia$lein other class4B

    >@

  • 8/17/2019 48304337-CORE-JAVA

    25/73

    Pro!lem 2:

    class +erson

    6 private String name9san95

     private int age>5

    BBString name9.y95BBint age>H5

    void tal)!#

    6Systemoutprintln!9/elllo & am NNQ9*name#5

    Systemoutprintln!9/elllo My Age NNQ9*age#5

    <

    <class Second3ype

    6

     pu$lic static void main!String78 args#6

    +erson p1new +erson!#5 p1tal)!#5+erson p>new +erson!#5

     p>name9a$c95

     p>age>X5

     p>tal)!#5<

    <

    BB=ote:de%ect in this program is private varia$le canYt $e initialie second time

    Pro!lem 3:

    class +erson

    6

     private String name5 private int age5

    BBString name5

    BBint age5+erson!#BBde%ault constructor 

    6

    name9.y95

    age>H5Systemoutprintln!9de%ault constructor9#5

    <void tal)!#

    6

    Systemoutprintln!9/elllo & am NNQ9*name#5

    Systemoutprintln!9/elllo My Age NNQ9*age#5<

    >

  • 8/17/2019 48304337-CORE-JAVA

    26/73

    <class 3hird3ype

    6

     pu$lic static void main!String78 args#6

    +erson p1new +erson!#5

    +erson p>new +erson!#5 p1tal)!#5

     p>tal)!#5

    <<

    BB=ote: de%ect in this program is de%ault constructor can initialie only one set o% value

    $onstructor: A constructor is similar to a method which initialies the instance varia$les o%a class A constructor name and class name should $e same A constructor

    may have parameters and may not have parameters A constructor without any

     parameter is called de%ault constructor A constructor with one or more parameters is called parameteried constructor A constructor does not return

    any value not even void A constructor is called only once at the time o%

    creating an o$(ect

    5: 6,at is t,e dierence !et8een deault constructor and parameteri

  • 8/17/2019 48304337-CORE-JAVA

    27/73

    6thisaa5

    this$$5

    thiscc5<

    void display!#

    6

      Systemoutprintln!9/ello name isNNQ9*a#5

    Systemoutprintln!9/ello age isNNQ9*$#5Systemoutprintln!9/ello age isNNQ9*c#5

    <

    <

    class +aramcon6

     pu$lic static void main!String78 args#

    6Constru c1new Constru!#5

    c1display!#5Constru c>new Constru!#5c>display!#5

    Constru c?new Constru!9san9">#5

    c?display!#5

    Constru c@new Constru!9Ram9"#5c@display!#5

    Constru cnew Constru!9...9"K"@#5

    cdisplay!#5Constru cInew Constru!9ddd9"?"@@#5

    cIdisplay!#5

    <<

    BB=ote: &n this program we are a$le to get di%%erent set o% values $y using parameteried

    constructor" even the varia$le are declared as private

    Met,od: A method is a group o% sttements similar to class !or# A method represents a groupo% statements to e.ecute a tas) A method will have two parts:

    i) Met,od +eader or Met,od Prototpe: 3his part contains method name" method

     parameters and method return type

    ii) Met,od od: A method $ody represents a group o% statements containing logicwritten inside 6 <

    &% a method returns some values we should use return statements inside to the method

     $ody e can return only one entity

    >F

  • 8/17/2019 48304337-CORE-JAVA

    28/73

    Met,od 6it,out 'eturn .pe Example:

    BBMethod without return type

    class rt6

    dou$le d1"d>5

    rt!dou$le ."dou$le y#BB+arameteried constructor 6

    d1.5

    d>y5<

    void sum!#

    6

    dou$le d?d1*d>5Systemoutprintln!9sum9*d?#5

    <

    <class Demort

    6 pu$lic static void main!String78 args#6

    rt snew rt!1H?">H#5

    ssum!#5

    rt s1new rt!1H@II">HI#5s1sum!#5

    <

    <

    Met,od 6it,'eturn .pe Example:

    BBMethod with return type

    class ithrt

    6

    dou$le d1"d>5ithrt!dou$le ."dou$le y#

    6

    d1.5d>y5

    <

    dou$le sum!#

    6BBdou$le d?d1*d>5

    BBreturn d?5return d1*d>5

    <

    <

    class Demoithrt6

    >K

  • 8/17/2019 48304337-CORE-JAVA

    29/73

     pu$lic static void main!String78 args#6

    ithrt snew ithrt!1H?">H#5

    Systemoutprintln!9sum9*ssum!##5ithrt s1new ithrt!>?"1>I#5

    Systemoutprintln!9sum9*s1sum!##5

    <<

    .pes o Met,ods:

    1) Static Met,od: Static methods are the methods which does not act upon the instance

    varia$les o% a class Static methods are declared as static $y using a static

    )eyword Static methods are called using classnamemethodname /ere noneed to create an o$(ect Static methods can act upon static varia$le Static

    varia$le should $e declared as static

      E.: static dou$le d1"d>"d?5

    E.ample:

    BBStatic method e.ample

    class Sta

    6

    BBstatic int a1H5static int a11H5

    static void display!#6

    Systemoutprintln!a1#5

    <<

    class Static

    6

     pu$lic static void main!String78 args#6

    Stadisplay!#5

    <<

    2) -nstance Met,od: 3hese are the methods which act upon instance varia$les o% a class

    &nstance methods are called using o$(ectnamemethodnamea) #ccessor Met,od: 3hese methods access or read the instance

    varia$les

    !) Mutator Met,od: 3hese methods not only read the instance varia$le $ut also

    modi%ies the contents o% the o$(ect

    >X

  • 8/17/2019 48304337-CORE-JAVA

    30/73

    E.ample:

    BBAccesor and Mutator method

    class +erson6

    String name5

    int age5char se.5

    +erson!String name"int age"char se.#

    6thisnamename5

    thisageage5

    thisse.se.5

    <BBAccesor Method

    void display!#

    6Systemoutprintln!9=ameNNQ9*name#5

    Systemoutprintln!9AgeNNQ9*age#5Systemoutprintln!9Se.NNQ9*se.#5

    <

    BBMutator Method

    +erson modi%y!+erson o$(#

    6o$(name9yyy95

    NNo$(age5

    o$(se.YY5return o$(5

    <

    <class MutaAcc

    6

     pu$lic static void main!String78 args#

    6+erson pnew +erson!9...9"?I"YMY#5

     pdisplay!#5

    +erson p1pmodi%y!p#5BB/ere we are not creating an o$(ect to p1 /ere p1 is a re%erence varia$le

     p1display!#5

     pdisplay!#5

    +erson p>new +erson!9sai9">>"YMY#5 p>display!#5

    <<

    3) Factor Met,od: A actory method is a method that returns an o$(ect o% the class to

    which it $elongs All the actory methods are static method hen weuse actory Method we can not use new operator

    ?H

  • 8/17/2019 48304337-CORE-JAVA

    31/73

    E.ample:

    BBE.ample %or actory Methodimport (avate.t45

    class actMe

    6 pu$lic static void main!String78 args#

    6

    %inal dou$le +&!dou$le#>>BF5dou$le r@KF5

    dou$le area+&4r4r5

    Systemoutprintln!9Area9*area#5

     =um$erormat o$(=um$erormatget=um$er&nstance!#5o$(setMa.imumractionDigits!>#5

    String stro$(%ormat!area#5

    Systemoutprintln!9ormatted Area9*str#5o$(setMinimumractionDigits!I#5

    String str1o$(%ormat!area#5Systemoutprintln!9ormatted Area9*str1#5

    <

    <

    -nner $lass: A class within another class is called inner class e can not write private $e%oremain class +rivate can $e used only $e%ore inner class &nner class not availa$le to

    the other programmer e can create an o$(ect to inner class only in its outer class

    E.ample:

    BB&nner class e.ample

    class 0an)AcctBBOuter class

    6

     private dou$le $al5

    0an)Acct!dou$le $#

    6 $al$5

    <

    void start!dou$le r#

    6&nterest innew &nterest!r#5

    incalculate&nterest!#5<

     private class &nterestBB&nner class

    6

     private dou$le rate5&nterest!dou$le r#

    ?1

  • 8/17/2019 48304337-CORE-JAVA

    32/73

    6rater5

    <

    void calculate&nterest!#6

    dou$le interest$al4rateB1HH5

    Systemoutprintln!9&nterest9*interest#5 $al*interest5

    Systemoutprintln!90alance9*$al#5

    <<

    <

    class &nnerDemo

    6 pu$lic static void main!String78 args#

    6

    0an)Acct acctnew 0an)Acct!1HHHHH#5acctstart!?#5

    <<

    -n,eritance: +roducing a new class %rom an e.isting class is called inheritance 3he e.istingclass is called super class and the newly produced class is called su$ classSu$class o$(ect contains copy o% the super class 3he main advantage o%

    inheritance is the reusa$ility o% the code e can create an o$(ect to the su$class

    only in inheritance

    .pes o -n,eritance:

    1# Single &nheritance># Multilevel &nheritance

    ?# Multiple &nheritance

    @# /ierarchical &nheritance# /y$rid &nheritance

    E.ample 1:

    BBE.ample o% &nheritance!using setters and getters methods#

    class 3eacher

    6

    int id5String name"addr5

    void set&d!int id#6

    thisidid5

    <

    int get&d!#6

    ?>

  • 8/17/2019 48304337-CORE-JAVA

    33/73

    return id5<

    void set=ame!String name#

    6thisnamename5

    <

    String get=ame!#6

    return name5

    <void setAddr!String addr#

    6

    thisaddraddr5

    <String getAddr!#

    6

    return addr5<

    <

    class Student e.tends 3eacher 

    6

    int mar)s5

    void setMar)s!int mar)s#6

    thismar)smar)s5

    <int getMar)s!#

    6

    return mar)s5<

    <

    class &nherit6

     pu$lic static void main!String78 args#

    6Student stnew Student!#5

    stset&d!1#5

    stset=ame!9...9#5

    stsetAddr!91?1N>>"+3 colony"DS=R9#5stsetMar)s!XK#5

    Systemoutprintln!9&D9*stget&d!##5Systemoutprintln!9=AME9*stget=ame!##5

    Systemoutprintln!9ADDRESS9*stgetAddr!##5

    Systemoutprintln!9MAR]S9*stgetMar)s!##5

    <<

    ??

  • 8/17/2019 48304337-CORE-JAVA

    34/73

     =ote: a# rom a$ove program all the 3eacher class mem$ers are availa$le to student class

      $# 3o access or read all the mem$ers o% 3eacher class!Super class# to Student class!Su$class# we use e.tends )eyword

    E.ample >:

    class OneDemo

    6OneDemo!#

    6

    Systemoutprintln!9OneDemo in super class;9#5

    <<

    class 3woDemo e.tends OneDemo

    6

    3woDemo!#6Systemoutprintln!93woDemo in su$ class9#5

    <

    <

    class 3Demo e.tends 3woDemo6

    3Demo!#6

    Systemoutprintln!93Demo in su$ class9#5

    <<

    class &nheritDemo

    6

     pu$lic static void main!String78 args#6

    3Demo tnew 3Demo!#5

    <<

     =ote: a# rom a$ove program %irst super class constructor is e.ecuted and than the su$ class

    constructor is e.ecuted  $# Super class parameteried constructor is not availa$le to su$ class

      c# Super is a )eyword that re%ers to super class %rom a su$ class &t means super can re%er

    super class instance varia$le" super class constructor and also super class methods

    ?@

  • 8/17/2019 48304337-CORE-JAVA

    35/73

  • 8/17/2019 48304337-CORE-JAVA

    36/73

    a# Static polymorphism $# Dynamic polymorphism

    %namic polmorp,ism: Dynamic means at runtime 3he polymorphism e.ecuted at runtime iscalled dynamic polymorphism or runtime polymorphism or dynamic

     $inding or dynamic lin)ing /ere (ava compiler does not )now which

    method is called $y the user at the time o% compilation JVM can $indthe method" call with the method $ody at the time o% running the

     program

    E.ample:

    Met,od Overloading: riting two or more methods with the same name $ut with di%%erent

    method Signature

    BBMethod Overloading

    class Sample

    6 void add!int a"int $#

    6Systemoutprintln!9sum o% two9*!a*$##5

    <

    void add!int a"int $"int c#

    6

    Systemoutprintln!9sum o% three9*!a*$*c##5

    <

    <class +oly

    6

     pu$lic static void main!String78 args#6

    Sample snew Sample!#5

    sadd!1H">H#5

    sadd!1H">H"?H#5<

    <

    Met,od Signature: Method Signature represents method name and method parameters hen two

    or more methods are written with the same method name and there is a

    di%%erence in the method signature %rom can identi%y those method s di%%erent

    method

    3he di%%erence in the method signature may $e due to the %ollowing %actor:

    a# 3here may $e a di%%erence in the num$er o% parameter

    E.: void add!int a" int $#

      void add!int a" int $" int c#

    ?I

  • 8/17/2019 48304337-CORE-JAVA

    37/73

     $# 3here is a di%%erence in the data type o% parametersE.: void add!int a" int $#

      void add!%loat a" %loat $#

    c# 3here is a di%%erence in the seWuence o% parameters

    E.: int swap!int a" char $#

      &nt swap!char a" int $#

    Met,od Overriding: riting two or more methods in super and su$ classes with the same nameand same signature is called method overriding &n method overriding JVM

    e.ecute a method depending on the data type o% re%erence varia$le used to

    call a method

    E.ample:

    BBMethod Overridingclass One

    6  void calculate!dou$le .#6

    Systemoutprintln!9SWuare9*!.4.##5

    <

    <

    class 3wo e.tends One

    6void calculate!dou$le .#

    6

    supercalculate!>#5Systemoutprintln!9SWuare root value9*

    MathsWrt!.##5

    <

    <class +oly1

    6

     pu$lic static void main!String78 args#6

    3wo tnew 3wo!#5

    tcalculate!>#5tcalculate!X#5

    tcalculate!?I#5<

    <

    Static Polmorp,ism: 3he +olymorphism e.ecuted at compile time is called static polymorphismor static $inding

    ?F

  • 8/17/2019 48304337-CORE-JAVA

    38/73

    =se o Final e8ord:

    1# inal )eyword is writing $e%ore constants

    ># inal )eyword is used $e%ore the class name to prevent inheritance?# inal methods can not $e overridden

    .pe $asting or $asting:Converting one data type into another type is called type casting or

    casting

    $asting primitive data tpe:

    1) 6idening: Casting a lower data type into a higher data type is called widening

      E.: char ch \A,

      int i !int# ch5BBI

    2) 4arro8ing: Converting a higher data type into lower type is called =arrowing

      E.: int n I5  Char ch !char# n5

    #!stract class: A method consists o% two parts: method header and method $ody Methodheader represents the %uture the programmer wants in a class Method $ody

    represents how to implements this %uture &mplementing means writing $ody%or method hen a method has got di%%erent implementation in di%%erent

    o$(ects than the programmer cannot write method $ody in the super class

      A method without method $ody is called a a$stract method A class thatcontains a$stract method is called a$stract class 3o declare a$stract method

    class should $e declare using )eyword a$stract Concrete methods meancomplete methods e cannot create o$(ect to a$stract class

    Points:

    1# An a$stract class is a class with ero or more a$stract methods

    ># An a$stract class can have a$stract method" concrete method and instance

    varia$les?# 0oth the a$stract class and a$stract methods should $e declared as a$stract

    @# e cannot create an o$(ect to a$stract class

    # e can create re%erence varia$les o% a$stract class type

    I# All the a$stract methods o% a$stract class should $e implemented in itssu$classes

    F# &% any method is not implemented that su$class should also $e declare as

    a$stractK# A$stract class re%erence varia$le can $e used to re%er to the o$(ects

    X# A$stract class re%erence cannot re%er to individual method o% a class

    1H# A class cannot have $oth a$stract and %inal

    ?K

  • 8/17/2019 48304337-CORE-JAVA

    39/73

    E.ample:

    a$stract class Car

    6int regno5

    Car!int regno#

    6thisregnoregno5

    <

    void %ill3an)!#BBconcrete method6

    Systemoutprintln!9ill 3an)9#5

    <

    a$stract void steering!int direction#5a$stract void $rea)ing!int %orce#5

    <

    class Maruti e.tends Car 6

    Maruti!int regno#6

    super!regno#5

    <

    void steering!int direction#6

    Systemoutprintln!9Regno o% Maruti9*regno#5

    Systemoutprintln!9Maruti uses manual steering9*direction#5Systemoutprintln!9please drive the maruti car9#5

    <

    void $rea)ing!int %orce#6

    Systemoutprintln!90rea)ing o% Maruti9*%orce#5

    Systemoutprintln!9Maruti uses hydralic $rea)s9#5

    Systemoutprintln!9Apply $rea)s stop the car9#5<

    <

    class Santro e.tends Car 6

    Santro!int regno#

    6

    super!regno#5<

    void steering!int direction#6

    Systemoutprintln!9Regno o% Santro9*regno#5

    Systemoutprintln!9Santro uses manual steering9*direction#5

    Systemoutprintln!9please drive the Santro car9#5<

    ?X

  • 8/17/2019 48304337-CORE-JAVA

    40/73

    void $rea)ing!int %orce#6

    Systemoutprintln!90rea)ing o% Santro9*%orce#5

    Systemoutprintln!9Santro uses hydralic $rea)s9#5Systemoutprintln!9Apply $rea)s stop the car9#5

    <

    <class A$stractDemo

    6

     pu$lic static void main!String78 args#6

    Maruti mnew Maruti!IIII#5

    Santro snew Santro!XXXX#5

    Car c"c15cm5

    c1s5

    c%ill3an)!#5csteering!>#5

    c$rea)ing!>HH#5

    c1%ill3an)!#5

    c1steering!>#5

    c1$rea)ing!>HH#5

    <<

    -nterace: An inter%ace is a speci%ication o% method prototype All the methods o% inter%ace are $y de%ault a$stract only e cannot create an o$(ect to inter%ace

      Points:

    1# &nter%ace is a speci%ication o% method prototype or method header># An inter%ace contains ero or more a$stract methods

    ?# All the methods o% the inter%ace are pu$lic and a$stract $y de%ault

    @# An inter%ace can also contain varia$les which are pu$lic" static and %inal $y

    de%ault# hen an inter%ace is written any third party vendor can implement it

    I# All the a$stract method o% the inter%ace should $e implemented in a class called

    implementation class

    F# &% any method is not implemented than that class should $e declare as a$stractK# e cannot create an o$(ect to an inter%ace

    X# e can create a re%erence o% an inter%ace1H# &nter%ace re%erence can $e used to re%er to the o$(ects o% its implementation

    classes

    11# e can write a class within an inter%ace

    1># An inter%ace cannot implement another inter%ace1?# An inter%ace can e.tends another inter%ace

    @H

  • 8/17/2019 48304337-CORE-JAVA

    41/73

    1@# A single class can implement multiple inter%aces

    E.ample1:

    import (avaio45

    inter%ace My&nter 

    6void connect!#5

    <

    class OracleD$ implements My&nter 

    6

     pu$lic void connect!#

    6Systemoutprintln!9Connecting to oracle9#5

    <

    <class Sy$aseD$ implements My&nter 

    6 pu$lic void connect!#6

    Systemoutprintln!9Connecting to sy$ase9#5

    <

    <class &nter

    6

     pu$lic static void main!String78 args# throws E.ception6

    B4Class cClass%or=ame!args7H8#5

    My&nter mi!My&nter#cnew&nstance!#5miconnect!#54B

    OracleD$ o$new OracleD$!#5Sy$aseD$ s$new Sy$aseD$!#5

    My&nter m1"m>5

    m1o$5m1connect!#5

    m>s$5

    m>connect!#5

    <<

    E.ample >:

    BBAn &nter%ace can e.tends more than one inter%ace"$ut one inter%ace canYt implements another

    inter%ace

    @1

  • 8/17/2019 48304337-CORE-JAVA

    42/73

    inter%ace irst6

    void disp1!#5

    <inter%ace Second

    6

    void disp>!#5<

    inter%ace 3hird e.tends irst"Second

    6void disp?!#5

    <

    class 3est implements 3hird

    6 pu$lic void disp?!#

    6

    Systemoutprintln!9disp?!#9#5<

     pu$lic void disp1!#6

    Systemoutprintln!9disp1!#9#5

    <

     pu$lic void disp>!#

    6Systemoutprintln!9disp>!#9#5

    <

    <class &nter1

    6

     pu$lic static void main!String78 args#6

    3est tnew 3est!#5

    tdisp1!#5

    tdisp>!#5tdisp?!#5

    BBSystemoutprintln!9/ello orld;9#5

    <<

    E.ample ?:

    BBclass inside inter%ace

    inter%ace Outer 6

    void disp!#5

    class &nner 

    6void test!#

    @>

  • 8/17/2019 48304337-CORE-JAVA

    43/73

    6Systemoutprintln!9&nner9#5

    <

    <<

    class Main implements Outer 

    6 pu$lic void disp!#

    6

    Systemoutprintln!9disp9#5<

    <

    class &nter>

    6

     pu$lic static void main!String78 args#

    6Main mnew Main!#5

    mdisp!#5Outer&nner inew Outer&nner!#5itest!#5

    Systemoutprintln!9/ello orld;9#5

    <

    <

    Multiple -n,eritance: Java doesn,t support multiple inheritance e can achieve multiple

    inheritance $y using multiple inter%aces

    E.ample:

    BBMultiple inheritance $y using multiple inter%ace

    inter%ace ather 

    6

    int +RO+1HHHH5%loat /31I>5

    <

    inter%ace Mother 6

    int +RO+>>HHHH5

    %loat /3>@5

    <class Child implements ather"Mother 

    6void property!#

    6

    Systemoutprintln!9Child property9*!+RO+1*+RO+>##5

    <void height!#

    @?

  • 8/17/2019 48304337-CORE-JAVA

    44/73

    6Systemoutprintln!9Child height9*!/31*/3>#B>#5

    <

    <class Multi

    6

     pu$lic static void main!String78 args#6

    Child cnew Child!#5

    cproperty!#5cheight!#5

    <

    <

    Pacages: A pac)ages represents a su$ directory that contains a group o% related classes andinter%aces +ac)ages hide classes and inter%aces in separate su$directories Accidental

    deletion is not possi$le 3he classes o% one pac)age are di%%erent %rom the classes o%another pac)age +ac)ages provide the reusa$ility o% code e can create our own

     pac)age as well as e.tend already availa$le pac)ages

    E.ample 1

    Addition(ava

     pac)age ddd5

     pu$lic class Addition

    6dou$le d1"d>5

     pu$lic Addition!dou$le d1"dou$le d>#

    6thisd1d15

    thisd>d>5

    < pu$lic void sum!#

    6

    Systemoutprintln!9sum9*!d1*d>##5

    <<

     =ote: Q (avac _d Addition(ava

    -seAdd(ava

    import dddAddition5class -seAdd

    6

     pu$lic static void main!String78 args#

    6Addition anew Addition!>@"?@#5

    @@

  • 8/17/2019 48304337-CORE-JAVA

    45/73

    Addition a1new Addition!>"?#5BBdddAddition anew dddAddition!>"#5

    asum!#5

    a1sum!#5<

    <

    E.ample >:

    MyDate(ava

     pac)age cd%5

     pu$lic inter%ace MyDate

    6void showDate!#5

    <

    MyDate&mpl(ava

     pac)age cd%5import (avautilDate5

     pu$lic class MyDate&mpl implements MyDate

    6

     pu$lic void showDate!#6

    Date dnew Date!#5

    Systemoutprintln!d#5<

    <

    -seinter%ace(ava

    import cd%MyDate&mpl5

    class -seinter%ace6

     pu$lic static void main!String78 args#

    6MyDate&mpl mnew MyDate&mpl!#5

    mshowDate!#5

    <

    <

    Su!Pacage Example

    Sample(ava

     pac)age demoaptech5 pu$lic class Sample

    @

  • 8/17/2019 48304337-CORE-JAVA

    46/73

    6 pu$lic void show!#

    6

    Systemoutprintln!9su$ pac)ages9#5<

    <

    -seSample(ava

    import demoaptechSample5class -seSample

    6

     pu$lic static void main!String78 args#

    6Sample snew Sample!#5

    sshow!#5

    <<

    Exception +andling:

    ug: A $ug is an error in so%tware De$ugging means removing the errors

    .pes o Error:

    1) $ompile time error: 3hese are the errors in the synta. or grammar o% the language 3hese

    errors are detected $y the compiler at the time o% compilation Des)chec)ing!line $y line# is the solution %or compile time errors

    2) 'untime error: 3hese errors will arise due to ine%%iciency !enough memory not availa$le#or una$le to e.ecute the statement $y processor E.: something divides $y

    ero Runtime errors are detected $y JVM

    3) >ogical error: 3hese are the errors in the logic o% the program 3hese errors are not

      detected $y the compiler or JVM 3he programmer is completely

    responsi$le %or logical errors

    Exception: An E.ception represents runtime errors

    $,eced Exception: 3he E.ception which are detected $y (ava compiler at the time o%compilation are called Chec)ed E.ception 

    =nc,eced Exception: 3he e.ception which are not detected $y (ava compiler and they aredetected $y JVM at runtime are called -nchec)ed E.ception:

     =ote: An E.ception is an error that can $e handled $ut an error cannot $e handled

    @I

  • 8/17/2019 48304337-CORE-JAVA

    47/73

    E.ample:

    BBwithout handling

    class E.ce11

    6

     pu$lic static void main!String78 args#6

    Systemoutprintln!9open the %iles9#5

    int nargslength5Systemoutprintln!9=um$er o% args areNNQ9*n#5

    int a@Bn5

    Systemoutprintln!a#5

    int a17861H">H# hen there is an e.ception JVM stores e.ception details in the E.ception Stac) &n catch

     $loc) the programmer should display e.ception details and also any messages to the user

    ?# 3he programmer should close all the %iles and data$ases and per%orm cleanup operation $y

    writing them in %inally $loc) 3he statements inside %inally $loc)s are always e.ecuted

    wether there is an e.ception or not

    E.ample:

    BBhandling the e.ception

    class E.ce1

    6

     pu$lic static void main!String78 args#6

    Systemoutprintln!9open the %iles9#5try

    6

    int nargslength5

    Systemoutprintln!9=um$er o% args are9*n#5int a@Bn5

    @F

  • 8/17/2019 48304337-CORE-JAVA

    48/73

  • 8/17/2019 48304337-CORE-JAVA

    49/73

    6Systemoutprintln!9array is out o% $ounds : 9*e#5

    <

    <catch!ArithmeticE.ception e#

    6

    Systemoutprintln!9division $y ero : 9*e#5<

    <<

    E.ample:

    class =ested3ryMethods

    6

    static void nested3ry!int no#BBMethod6

     try 6

     Systemoutprintln!9[n &=S&DE S3A3&C ME3/OD9#5

     i%!no1#

    nonoB!noNno#5

     i%!no ># 6

     int a78 68 FF5 <

     <

      catch!E.ception e# 6

      Systemoutprintln!9%rom method : : 9*e#5

     <

    <  pu$lic static void main!String ar78#

      6

    try6

    int n"res5

      n arlength5

    Systemoutprintln!9 =o o% Command 'ine Arguments 9*n#5  res Bn5

      Systemoutprintln!9 Result 9*res#5  Systemoutprintln!90e%ore &nvlo)ing nested3ry!n# Value o% = 9*n#5

    nested3ry!n#5

    <

      catch!E.ception e#  6

    @X

  • 8/17/2019 48304337-CORE-JAVA

    50/73

  • 8/17/2019 48304337-CORE-JAVA

    51/73

    c7?18 FKX5%or!int iH5iPclength5i**#

    Systemoutprintln!c7i8#5

    <

    catch!Array&nde.OutO%0oundsE.ception e#

    6Systemoutprintln!9Array &nde. Violation 9*e#5

    <

    catch!ArithmeticE.ception e#

    6

    Systemoutprintln!9Divide $y ero9*e#5

    <

    catch!E.ception e#

    6Systemoutprintln!9 %rom Super class E.ception 9*e#5

    <

      %inally

    6

    Systemoutprintln!9a%ter Catch9#5

      <<

    <

    E.ample:

    BBE.ample %or userde%ined e.ceptionclass MyE.ce e.tends E.ception

    6

     private static int accno7861HH1"1HH>"1HH?"1HH@

  • 8/17/2019 48304337-CORE-JAVA

    52/73

    Systemoutprintln!accno7i8*9[t9*name7i8*9[t9*$al7i8#5i%!$al7i8P>HHH#

    6

    B4MyE.ce menew MyE.ce!90alance amount is less9#5throw!me#54B

    throw new MyE.ce!90alance amount is less9#5

    <<

    <

    catch!MyE.ce me#6

    BBSystemoutprintln!me#5

    meprintStac)3race!#5

    <<

    <

    E.ample:

    class MyE.ception e.tends E.ception6

      private int data5

      MyE.ception!int n#

      6  data n5

    Systemoutprintln!9&n MyE.ception 9#5

      <  pu$lic String toString!#

      6

      return 9 E.ception raised due to low $al 79*data*9895  <

    <

    class -serDe%inedE.ce6

      static void chec)!int no# throws MyE.ception

      6  Systemoutprintln!9&n Chec)!9*no*9#9#5

      i%!noPHH#

      throw new MyE.ception!no#5  Systemoutprintln!9=ormal E.it9#5

      <  pu$lic static void main!String ar78#

    6

      try

     6chec)!1HHH#5

    >

  • 8/17/2019 48304337-CORE-JAVA

    53/73

    chec)!?H#5 <

     catch!MyE.ception me#

     6 Systemoutprintln!me#5

     <

     <

    <

    Exception >ist:

    E.ception Meaning:

    1# ArithmeticE.ception Arithmetic error" such as divideN$yNero

    ># Array&nde.OutO%0oundsE.ception Array inde. is outNo%N$ounds

    ?# ArrayStoreE.ception Assignment to an array element o% an incompati$letype

    @# ClassCastE.ception invalid cast

    # &llegalArgumentE.ception &llegal argument used to invo)e a method

    I# &llegalMonitorStateE.ception &llegal monitor operation" such as waiting on an

    unloc)ed thread

    F# &llegalStateE.ception environment or application is in incorrect state

    K# &llegal3hreadStateE.ception ReWuested operation not compati$le with current

    thread state

    X# &nde.OutO%0oundsE.ception Some type o% inde. is outNo%N$ounds

    1H# =egativeArraySieE.ception array created with a negative sie

    11# Class=otoundE.ception class not %ound

    1># Clone=otSupportedE.ception Attempt to clone an o$(ect that does not

    implement the Clonea$le inter%ace

    1?# &llegalAccessE.ception Access to a class is denied

    1@# &nstantiationE.ception Attempt to create an o$(ect o% an a$stract class or inter%ace

    1# &nterruptedE.ception One thread has $een interrupted $y another thread

    ?

  • 8/17/2019 48304337-CORE-JAVA

    54/73

    1I# =oSuchieldE.ception A reWuested %ield does not e.ist

    1F# =oSuchMethodE.ception A reWuested method does not e.ist

    1K# =ull+ointerE.ception invalid use o% a null re%erence

    1X# =um$erormatE.ception invalid conversion o% a string to a numeric %ormat

    >H# SecurityE.ception Attempt to violate security

    >1# String&nde.OutO%0oundsE.ception Attempt to inde. outside the $ounds o% a string

    >># -nsupportedOperationE.ception An unsupported operation was encountered

    6rapper $lass: A wrapper class is a class whose o$(ect contains a primitive data type

    $,aracter class: A Character class contains a value o% primitive type char in an o$(ect An o$(ect

    o% type Character contains a single %ield whose type is char

    E.ample:

    BBCharacter 3estimport (avaio45

    class Char3est

    6

     pu$lic static void main!String78 args# throws E.ception6

    0u%%eredReader $rnew 0u%%eredReader!new &nputStreamReader!Systemin##5

    Systemoutprintln!9+lease Enter a character9#5char ch!char#$rread!#5

    i%!CharacterisDigit!ch##

    Systemoutprintln!9&t is a Digit9#5else i%!Characteris-pperCase!ch##

    Systemoutprintln!9&t is capital9#5

    else i%!Characteris'owerCase!ch##

    Systemoutprintln!9&t is small9#5else i%!CharacterisSpaceChar!ch##

    Systemoutprintln!9&t is coming %rom space$ar9#5

    else i%!Characterishitespace!ch##

    Systemoutprintln!9&t is coming %rom enter"ta$9#5else

    Systemoutprintln!9Sorry & donot )now that character9#5<

    <

    @

  • 8/17/2019 48304337-CORE-JAVA

    55/73

    te class: A 0yte class contains a value o% primitive type $yte in an o$(ect An o$(ecto% type 0yte contains a single %ield whose type is $yte

    E.ample:

    import (avaio45

    class 0yte3est6

     pu$lic static void main!String78 args# throws E.ception

    60u%%eredReader $rnew 0u%%eredReader!new &nputStreamReader!Systemin##5

    Systemoutprintln!9Enter %irst $yte no9#5

    String s1$rread'ine!#5

    0yte $1new 0yte!s1#5Systemoutprintln!9Enter second $yte no9#5

    String s>$rread'ine!#5

    0yte $>new 0yte!s>#5int n$1compare3o!$>#5

    Systemoutprintln!9Compare3o!# methods return the di%%erence $etween twono9*n#5

    i%!nH#

    Systemoutprintln!90oth are eWual9#5

    else i%!nQH#

    Systemoutprintln!9$1 is $igger9*$1#5else

    Systemoutprintln!9$1 is smaller9*$1#5

    <<

    -nteger class: A &nteger class contains a value o% primitive type int in an o$(ect An o$(ecto% type &nteger contains a single %ield whose type is int

    E.ample:

    import (avaio45

    class &nt3est6

     pu$lic static void main!String78 args# throws &OE.ception

    6

    0u%%eredReader $rnew 0u%%eredReader!new

    &nputStreamReader!Systemin##5Systemoutprintln!9Enter a num$er9#5

    String str$rread'ine!#5

    int n&ntegerparse&nt!str#5

    Systemoutprintln!9&n decimalNNQ9*n#5str&ntegerto0inaryString!n#5

  • 8/17/2019 48304337-CORE-JAVA

    56/73

    Systemoutprintln!9&n $inaryNNQ9*str#5str&ntegerto/e.String!n#5

    Systemoutprintln!9&n he.adecimalNNQ9*str#5

    str&ntegertoOctalString!n#5Systemoutprintln!9&n octalNNQ9*str#5

    <

    <

    -OStream:

    Stream: A stream represents %low o% data %rom one place to another place 3here are two types o%

    stream:

    1) -nputStream: &nputStream read or accept data that is coming %rom some other place

    2) OutputStream: OutputStream send or write data to some other place

    te Stream: 0yte Stream handles the data in the %orm o% $its and $ytes 3o handle the data in the%orm o% $ytes the a$stract class &nputStream or OutputStream are used

    -nputStream %ivision:

    1# ile&nputStream

    ># ilter&nputStream

    a# 0u%%ered&nputStream

     $# Data&nputStream?# O$(ect &nputStream

    OutputStream %ivision:

    1# ileOutputStream

    ># ilterOutputStreama# 0u%%eredOutputStream

     $# DataOutputStream

    ?# O$(ect OutputStream

    $,aracterStream or .extStream: 3hey will handle the data in the %orm o% chracters

    'eader %ivision:

    1# 0u%%eredReader 

    ># CharArrayReader ?# &nputStreamReader 

    a# ileReader 

    @# +rintReader 

    I

  • 8/17/2019 48304337-CORE-JAVA

    57/73

  • 8/17/2019 48304337-CORE-JAVA

    58/73

    E.ample ?:

    BB-sing riter create and pass the data into the %ile

    import (avaio45class Create>

    6

     pu$lic static void main!String78 args# throws &OE.ception6

    String str93his is an &nstitute & am a student95

    ileriter %wnew ileriter!9te.t%ilet.t9"true#50u%%eredriter $wnew 0u%%eredriter!%w#5

    %or!int iH5iPstrlength!#5i**#

     $wwrite!strcharAt!i##5

     $wclose!#5<

    <

    E.ample @:

    BBReading %ile using ileReader import (avaio45

    class Read>

    6

     pu$lic static void main!String78 args# throws &OE.ception6

    ileReader %rnew ileReader!9te.t%ilet.t9#5int ch5

    while!!ch%rread!##;N1#

    Systemoutprint!!char#ch#5%rclose!#5

    <

    <

    E.ample :

    BBAccept %ile name %rom )ey$oard and display the contentimport (avaio45

    class ile=ame

    6

     pu$lic static void main!String78 args#6

    try6

    0u%%eredReader $rnew 0u%%eredReader!new &nputStreamReader!Systemin##5

    Systemoutprintln!9Enter %ile name9#5

    String %name$rread'ine!#5ile&nputStream ssnew ile&nputStream!%name#5

    K

  • 8/17/2019 48304337-CORE-JAVA

    59/73

    0u%%ered&nputStream $innew 0u%%ered&nputStream!ss#5int ch5

    while!!ch$inread!##;N1#

    Systemoutprint!!char#ch#5 $inclose!#5

    <

    catch!E.ception %e#6

    Systemoutprintln!9ile not %ound9*%e#5

    <<

    <

    E.ample I:

    BB`ipping a %ile contents

    import (avaio45import (avautilip45

    class `ip6

     pu$lic static void main!String78 args# throws &OE.ception

    6

    ile&nputStream %isnew ile&nputStream!9Myilet.t9#5

    ileOutputStream %osnew ileOutputStream!9ile1ip9#5De%laterOutputStream dosnew De%laterOutputStream!%os#5

    int data5

    while!!data%isread!##;N1#doswrite!data#5

    %isclose!#5

    dosclose!#5%osclose!#5

    <

    <

    E.ample F:

    BB-nip the %ile contentsimport (avaio45

    import (avautilip45

    class -nip

    6 pu$lic static void main!String78 args# throws &OE.ception

    6ile&nputStream %isnew ile&nputStream!9ile1ip9#5

    ileOutputStream %osnew ileOutputStream!9ile>doc9#5

    &n%later&nputStream iisnew &n%later&nputStream!%is#5

    int data5while!!dataiisread!##;N1#

    X

  • 8/17/2019 48304337-CORE-JAVA

    60/73

    BBSystemoutprintln!data#5%oswrite!data#5

    BBiisclose!#5

    %osclose!#5<

    <

    Seriali implements Serialia$le

    6

     pu$lic static int a5 pu$lic %loat $5

     pu$lic String c5

     pu$lic Ctwo>!int a1"%loat $1"String c1#6

    aa15

     $$15

    cc15<

     pu$lic void print!#6

    Systemoutprintln!9value o% a9*a#5

    Systemoutprintln!9value o% $9*$#5

    Systemoutprintln!9value o% c9*c#5<

    IH

  • 8/17/2019 48304337-CORE-JAVA

    61/73

    <

    BBimplements Serialia$le inter%ace

    import (avaioSerialia$le5 pu$lic class Ctwo implements Serialia$le

    6

     pu$lic int varone5 pu$lic %loat vartwo5

     pu$lic String varthr5

    <

    BBSerialiation Demo

    import (avaio45 pu$lic class Ser1

    6

     pu$lic static void main!String78 args# throws E.ception6

    BBCone onew Cone!#5Ctwo onew Ctwo!#5ovarone1H5

    ovartwo>>%5

    ovarthrnew String!9/ello9#5

    ileOutputStream %osnew ileOutputStream!93estSer19#5O$(ectOutputStream oosnew O$(ectOutputStream!%os#5

    ooswriteO$(ect!o#5

    <<

    BBDeSerialiation Demoimport (avaio45

     pu$lic class DeSer1

    6

     pu$lic static void main!String78 args# throws E.ception6

    Ctwo onull5

    ile&nputStream %isnew ile&nputStream!93estSer19#5O$(ect&nputStream oisnew O$(ect&nputStream!%is#5

    o!Ctwo#oisreadO$(ect!#5

    Systemoutprintln!ovarone#5

    Systemoutprintln!ovartwo#5Systemoutprintln!ovarthr#5

    <<

    BB-sing transient )eyword var%r varia$le is not serialia$le

    import (avaio45 pu$lic class Ser>

    I1

  • 8/17/2019 48304337-CORE-JAVA

    62/73

    6 pu$lic static void main!String78 args# throws E.ception

    6

    Ctwo1 onew Ctwo1!#5ovarone>H5

    ovartwo?>?%5

    ovarthrnew String!9hai9#5ovar%rnew Cone!#5

    ileOutputStream %osnew ileOutputStream!93estSer>9#5

    O$(ectOutputStream oosnew O$(ectOutputStream!%os#5ooswriteO$(ect!o#5

    <

    <

    import (avaio45

     pu$lic class Ser?

    6 pu$lic static void main!String78 args# throws E.ception

    6Ctwo> onew Ctwo>!1H">H>>%"9...9#5oprint!#5

    ileOutputStream %osnew ileOutputStream!93estSer>9#5

    O$(ectOutputStream oosnew O$(ectOutputStream!%os#5

    ooswriteO$(ect!o#5ile&nputStream %isnew ile&nputStream!93estSer>9#5

    O$(ect&nputStream oisnew O$(ect&nputStream!%is#5

    o!Ctwo>#oisreadO$(ect!#5Systemoutprintln!oa#5

    Systemoutprintln!o$#5

    Systemoutprintln!oc#5<

    <

    .,reads: A thread represents a process or e.ecution o% statements

      Microprocessor uses internally a thread to e.ecute the program

      E.ecution process or wor)ed is called thread

    E.ample:

    class 3hr16

     pu$lic static void main!String78 args#

    6

    Systemoutprintln!93his is %irst statement9#53hread t3hreadcurrent3hread!#5

    I>

  • 8/17/2019 48304337-CORE-JAVA

    63/73

    tset=ame!9Mythread9#5Systemoutprintln!tisAlive!##5

    Systemoutprintln!93hread name is9#5

    Systemoutprintln!9current thread9*t#5<

    <

     =ote: Every (ava program contains internally a thread called main/

      De%ault priority o% main thread is   3hread group name is main

    Executing statements is o 2 tpes:

    1) Single .asing: E.ecuting only one tas) at a time is called single tas)ing

    2) Multitasing: &n multitas)ing several program e.ecuting at a time e have two types in

    Multitas)ing

    a# Process !ased multitasing: E.ecuting several programs simultaneously is called process $ased multitas)ing

     $# .,read !ased multitasing: E.ecuting di%%erent parts o% the same program with the

    help o% threads is called 3hread $ased multitas)ing

    E.ample 1:

    BBcreating and running a thread using e.tends 3hreadimport (avaio45

    class 3demo e.tends 3hread

    6

     pu$lic void run!#

    6

    %or!int iH5iP1H5i**#6

    Systemoutprintln!i#5

    try6

    3hreadsleep!1HHH#5BB1HHHms1s

    <

    catch!E.ception e#6

    Systemoutprintln!e#5<

    <

    <<

    I?

  • 8/17/2019 48304337-CORE-JAVA

    64/73

    class 3hr>6

     pu$lic static void main!String args78#throws &OE.ception

    63demo o$(new 3demo!#5

    3hread tnew 3hread!o$(#5

    tstart!#5

    <

    <

    E.ample >:

    BBcreating and running a thread using implements Runna$leclass 3heatre implements Runna$le

    6

    String str53heatre!String str#

    6thisstrstr5

    <

     pu$lic void run!#

    6

    %or!int i15iP5i**#6

    Systemoutprintln!str*9 9*i#5

    try6

    3hreadsleep!1HHH#5

    <catch!&nterruptedE.ception ie#

    6

    BBSystemoutprintln!ie#5

    ieprintStac)3race!#5<

    <

    <<

    class 3hr?

    6

     pu$lic static void main!String args78#6

    3heatre o$(new 3heatre!9cut tic)et9#53heatre o$(1new 3heatre!9see movie9#5

    3hread t1new 3hread!o$(#5

    3hread t>new 3hread!o$(1#5

    t1start!#5t>start!#5

    I@

  • 8/17/2019 48304337-CORE-JAVA

    65/73

    <<

    .,read Snc,roni

  • 8/17/2019 48304337-CORE-JAVA

    66/73

  • 8/17/2019 48304337-CORE-JAVA

    67/73

    catch!&nterruptedE.ception ie#6

    Systemoutprintln!ie#5

    <<

    <

     pu$lic class Multi3hreadDemo6

     pu$lic static void main!String args78#

    63hread1 t1new 3hread1!#5

    3hread> t>new 3hread>!#5

    t1start!#5t>start!#5

    <

    <

    .,read Priorit:

    Max Priorit;  1?

    Min Priorit 

    4orm Priorit  1

    E.ample:

    BB+riority Demo

     pu$lic class +riorityDemo e.tends 3hread

    6

     pu$lic void run!#6

    %or!int iH5iP5i**#

    6String str3hreadcurrent3hread!#get=ame!#5

    Systemoutprintln!str*9:9*i#5

    <<

     pu$lic static void main!String args78#

    6+riorityDemo pd1new +riorityDemo!#5

    +riorityDemo pd>new +riorityDemo!#5

     pd1set=ame!9irst9#5 pd>set=ame!9Second9#5

     pd>set+riority!MAGb+R&OR&3Z#5BBpd>set+riority!K#5

     pd1set+riority!M&=b+R&OR&3Z#5

    IF

  • 8/17/2019 48304337-CORE-JAVA

    68/73

     pd1start!#5

     pd>start!#5

    Systemoutprintln!9+riority o% pd1 is 9*pd1get+riority!##5

    Systemoutprintln!9+riority o% pd> is 9*pd>get+riority!##5

    <<

    #pplet: An Applet is a program that comes %rom internet server into a client and gets e.ecuted atclient side and displays the result An applet represents $yte code em$edded in a /3M'

     page

      Applet $yte code * /3M'

    #pplet >ie ccle met,ods:

    1) pu!lic void init(): 3his method is used %or initialiing the varia$les" parameters and to create

    Components !$utton"images# 3his method is e.ecuted only once at the timeo% applet loaded into memory

    2) pu!lic void start(): 3his method will e.ecute when applet gains the %ocus

    3) pu!lic void stop():3his method will e.ecute when applet loss the %ocus

    &) pu!lic void destro(): 3his method is e.ecuted only once when the applet is terminated %romMemory

    E.ample 1:

    import (avaawt45

    import (avaapplet45B4Papplet code9MyApplet19 widthIHH height@HHQ

    PBappletQ4B

     pu$lic class MyApplet1 e.tends Applet

    6String msg9 95

     pu$lic void init!#

    6

    set0ac)ground!Colorpin)#5setoreground!Coloryellow#5

    ont %new ont!9Dialog9"ont0O'D"?H#5setont!%#5

    msg*9&nit95

    <

     pu$lic void start!#6

    IK

  • 8/17/2019 48304337-CORE-JAVA

    69/73

    msg*9start95<

     pu$lic void paint!2raphics g#

    6msg*9paint95

    gdrawString!msg"1HH"1HH#5

    < pu$lic void stop!#

    6

    msg*9stop95<

     pu$lic void destroy!#

    6<

    <

    PhtmlQ

    Ph1QMy irst AppletPBh1QP$ody $gcolor9red9Q

    Papplet code9MyApplet1class9 width@HH height?HHQPBappletQPB$odyQ

    PBhtmlQ

    E.ample >:

    BB&nsert image into Applet

    import (avaapplet45import (avaawt45

    B4Papplet code9MyApplet>class9 width@HH height?HHQ

    PBappletQ4B pu$lic class MyApplet> e.tends Applet

    6

     pu$lic void init!#

    6< pu$lic void paint!2raphics g#

    6

    &mage iget&mage!getDocument0ase!#"9O0JEC3gi%9#5

    BBmove image %rom le%t to right

    %or!int .H5.P1HH5.**#

    6gdraw&mage!i"."H"this#5

    <BBma)e time delay

    try

    6

    3hreadsleep!1HHH#5<

    IX

  • 8/17/2019 48304337-CORE-JAVA

    70/73

    catch!&nterruptedE.ception &E#6<

    <

    <

    PhtmlQ

    Ph1QMy &magePBh1QP$ody $gcolor9red9Q

    Papplet code9MyApplet>class9 width@HH height?HHQ

    PBappletQPB$odyQ

    PBhtmlQ

    E.ample ?:

    BBpassing value %rom /3M' to applet

    import (avaawt45import (avaappletApplet5

     pu$lic class MyApplet? e.tends Applet6

    ont %new ont!9Ariel9"ont0O'D">@#5

    String msg5

     pu$lic void paint!2raphics g#

    6gsetont!%#5

    gsetColor!Color$lue#5

    gdrawString!msg"1H"H#5<

     pu$lic void init!#

    6msgget+arameter!9para9#5

    i%!msgnull#

    msg9=ull value returned95

    <<

    PhtmlQPheadQ

    PtitleQMy pagePBtitleQ

    PBheadQ

    P$ody $gcolor$lueQPh>Q3his page shows use o% AppletPBh>Q

    PpQPapplet code9MyApplet?class9 width@HH height1HHQ

    Pparam namepara value9+assing value %rom /3M'9Q

    PBappletQ

    PB$odyQPBhtmlQ

    FH

  • 8/17/2019 48304337-CORE-JAVA

    71/73

    E.ample @:

    BBDraw graphicsimport (avaawt2raphics5

    import (avaawtont5

    import (avaawtColor5import (avaappletApplet5

     pu$lic class MyApplet@ e.tends Applet

    6 pu$lic void paint!2raphics g#

    6

    ont %new ont!9Courier9"ont0O'D"1I#5

    Color cnew Color!>HH">?">#5gdraw'ine!1H"1"1H"IH#5

    gdraw'ine!1H"1"IH"1H#5

    gdrawRect!1H"1H"H"H#5gsetont!%#5

    gsetColor!c#5gdrawString!9/ave a nice Day;9"11H"H#5

    <

    <

    PhtmlQPheadQ

    PtitleQMy pagePBtitleQ

    PBheadQP$ody $gcolorredQ

    Ph>Q3his page shows use o% 2raphicsPBh>Q

    PpQPapplet code9MyApplet@class9 width@HH height1HHQPBappletQ

    PB$odyQ

    PBhtmlQ

     

    F1

  • 8/17/2019 48304337-CORE-JAVA

    72/73

     

    F>

  • 8/17/2019 48304337-CORE-JAVA

    73/73

     

    F?


Recommended