Java Advance Class

  • Upload
    vicky

  • View
    234

  • Download
    0

Embed Size (px)

Citation preview

  • 8/11/2019 Java Advance Class

    1/39

    Java Advance Class

    Imam F Rozi

  • 8/11/2019 Java Advance Class

    2/39

    Legal Identifier Boleh diawali dengan karakter huruf, $

    atau _ dan tidak boleh selainnyatermasuk diawali dengan angka.

    Setelah karakter pertama boleh diikutioleh kombinasi karakter apapun Tidak ada batasan panjang karakter Tidak boleh pakai Java Keyword utk

    identifier Case Sensitive

  • 8/11/2019 Java Advance Class

    3/39

    Suns Java Code Convention Class dan Interface

    Harus diawali dengan huruf besar Jika namanya terdiri dari 2 atau lebih kata, maka

    tiap kata diawali dengan huruf besar Contoh: Pegawai, BangunDatar, PrintWriter,

    Seriallizable Method

    Huruf pertama diawali dengan huruf kecil

    Jika terdiiri dai 2 kata atau lebih, huruf dari katapertama huruf kecil, dan hruf pertama kataselanjutnya diawali dengan huruf besar

    Contoh: printInfoPegawai

  • 8/11/2019 Java Advance Class

    4/39

  • 8/11/2019 Java Advance Class

    5/39

    JavaBeans Standard JavaBeans merupakan klas dalam Java

    yang memiliki atribut. Atribut tsb biasanya memiliki modifier

    akses Private. Karena atributnya Private, satu2nya

    cara untuk mengaksesnya adalahdengan membuat method dgmodifier akses public.

    Method tsb disebut GETTER danSETTER

  • 8/11/2019 Java Advance Class

    6/39

    JavaBeans Standard

    GETTER : method untukmendapatkan/mengambil nilai atributdari klas

    SETTER : method untuk memberikannilai kepada atribut dari suatu klas.

  • 8/11/2019 Java Advance Class

    7/39

    JavaBeans Standard

    Aturan penamaan dalam JavaBeans: Jika tipe data atribut selain boolean,

    maka nama method getter harus diawalidengan get. Misal atribut gaji getGaji(). Tipe dari getter methodmenyesuaikan dengan tipedata dariatributnya.

    Jika tipe data atributnya boolean, makaawalan getter bisa get atau is. Misal:atribut oke tipe data boolean getOke() atau isOke().

  • 8/11/2019 Java Advance Class

    8/39

    JavaBeans Standard Untuk setter method, diawali dengan set

    dan diikuti dengan nama atribut. Misalatribut gaji setGaji(); Tipe settermethod selalu VOID

    Getter dan setter harus memiliki modifierakses PUBLIC

    Getter tidak punya argumen fungsi Setter memiliki argumen fungsi dengan

    tipe data argumen sesuai dengan tipedata atributnya. Misal : atribut double gaji

    public void setGaji(double gj).

  • 8/11/2019 Java Advance Class

    9/39

    Valid JavaBeans Example

    public void setMyValue(int v) public int getMyValue()

    public boolean isMyStatus()

  • 8/11/2019 Java Advance Class

    10/39

    Invalid JavaBeans Example

    void setCustomerName(String s) //must be public

    public void modifyMyValue(int v) //can't use 'modify'

  • 8/11/2019 Java Advance Class

    11/39

  • 8/11/2019 Java Advance Class

    12/39

    Paket java.lang Kompiler java otomatis mengimport

    seluruh klas dalam paket java.lang kesetiap file program

    Klas terpenting yang ada dalam paketini : Object Math Wrapper Class String StringBuffer

  • 8/11/2019 Java Advance Class

    13/39

    Klas Object Klas Object merupakan klas paling atas atau

    akar dari semua klas Java Deklarasi klas tanpa extends Object, secara

    implisit pasti ditambahkan extends Object

    public class Empolyee(){} Sama dengan Public class Empoyee extends Object(){}

    Method yang dimiliki: wait() notify() notifyAll() equals() toString()

  • 8/11/2019 Java Advance Class

    14/39

    Equals() Method public boolean equals(Object object)

    Untuk class Object: Fungsi method equals() sama dengan

    operator ==. Yaitu untuk membandingkan reference nya.

    Override equals() so that it performs auseful comparison.

    Object reference comparison if (d1 == d2)

    Relevant instance variables comparison if (d1.equals(d2))

  • 8/11/2019 Java Advance Class

    15/39

    Contoh

  • 8/11/2019 Java Advance Class

    16/39

    Contoh

    MyDate dt1 = new MyDate(1,2,2000);MyDate dt2 = new myDate(1,2,2000);if (dt1==dt2) System.out.println (sama ); else System.out.println (beda ); if (dt1.equals(dt2))

    System.out.println (sama ); else

    System.out.println (beda );

  • 8/11/2019 Java Advance Class

    17/39

    Contoh

    Override method equals dari kasMyDate agar lebih berguna

  • 8/11/2019 Java Advance Class

    18/39

    Method toString()

    Menghasilkan representasi string dari stateobject

    Menampilkan nama kelas object diikutioleh kode hash

    Hash code merupakan tanda unik berupainteger diberikan oleh Java pada object

    Override method toString() agar lebihberguna

  • 8/11/2019 Java Advance Class

    19/39

    String Class

    Memiliki beberapa konstruktor. Common string constructors:

    String s1 = new String(immutable); String s1 = immutable;

    Java mempunyai media penyimpananliteral string yang yang disebut pool.

    Jika suatu literal string sudah ada dipool, Java tidak akan membuat copylagi.

  • 8/11/2019 Java Advance Class

    20/39

    String POOL

    Kedua potongan program diatas OK Contoh pertama membandingkan contentnya. Contoh kedua membandingkan referencesnya.

  • 8/11/2019 Java Advance Class

    21/39

    == dan equals() utk String

    Method equals() membandingkancontent-nya

    == membandingkan alamatnya

  • 8/11/2019 Java Advance Class

    22/39

    Contoh

    Variabel java1 dieksekusi pada saatruntime

  • 8/11/2019 Java Advance Class

    23/39

    String method char charAt(int index): Returns the indexed

    character of a string, where the index of the initialcharacter is 0.

    String concat(String addThis): Returns a new stringconsisting of the old string followed by addThis.

    int compareTo(String otherString): Performs a lexicalcomparison; returns an int that is less than 0 if thecurrent string is less than otherString, equal to 0 ifthe strings are identical, and greater than 0 if thecurrent string is greater than otherString.

    boolean endsWith(String suffix): Returns true if thecurrent string ends with suffix; otherwise returnsfalse

  • 8/11/2019 Java Advance Class

    24/39

    String method boolean equals(Object ob): Returns true if ob

    instanceof String and the string encapsulated by obmatches the string encapsulated by the executingobject.

    boolean equalsIgnoreCase(String s): Creates a new

    string with the same value as the executing object,but in lower case. int indexOf(int ch): Returns the index within the

    current string of the first occurrence of ch.Alternative forms return the index of a string andbegin searching from a specified offset.

    int lastIndexOf(int ch): Returns the index within thecurrent string of the last occurrence of ch. Alternativeforms return the index of a string and end searchingat a specified offset from the end of the string

  • 8/11/2019 Java Advance Class

    25/39

    String method int length(): Returns the number of characters in the

    current string. String replace(char oldChar, char newChar): Returns a

    new string, generated by replacing every occurrenceof oldChar with newChar.

    boolean startsWith(String prefix): Returns true if thecurrent string begins with prefix; otherwise returnsfalse. Alternate forms begin searching from aspecified offset.

    String substring(int startIndex): Returns the substring,beginning at startIndex of the current string andextending to the end of the current string. Analternate form specifies starting and ending offsets.

    String toLowerCase(): Creates a new string with thesame value as the executing object, but in lower case

  • 8/11/2019 Java Advance Class

    26/39

  • 8/11/2019 Java Advance Class

    27/39

    StringBuffer Class represents a string that can be

    dynamically modified. Constructors:

    StringBuffer(): Constructs an empty stringbuffer StringBuffer(int capacity): Constructs an

    empty string buffer with the specified

    initial capacity StringBuffer(String initialString): Constructsa string buffer that initially contains thespecified string

  • 8/11/2019 Java Advance Class

    28/39

    StringBuffer Method StringBuffer append(String str): Appends str to the current string

    buffer. Alternative forms support appending primitives andcharacter arrays; these are converted to strings beforeappending.

    StringBuffer append(Object obj): Calls toString() on obj andappends the result to the current string buffer.

    StringBuffer insert(int offset, String str): Inserts str into thecurrent string buffer at position offset. There are numerousalternative forms.

    StringBuffer reverse(): Reverses the characters of the currentstring buffer.

    StringBuffer setCharAt(int offset, char newChar): Replaces thecharacter at position offset with newChar.

    StringBuffer setLength(int newLength): Sets the length of thestring buffer to newLength. If newLength is less than the currentlength, the string is truncated. If newLength is greater than thecurrent length, the string is padded with null characters

  • 8/11/2019 Java Advance Class

    29/39

    Modifying StringBuffer

  • 8/11/2019 Java Advance Class

    30/39

    String Concatenation the Easy Way concat() method of the String class append() method of the StringBuffer

    class

    + operator. Example: String Concatenation: a + b + c

    Java compiler treats as: newStringBuffer().append(a).append(b).append(c).toString();

  • 8/11/2019 Java Advance Class

    31/39

    Math Class

    a collection of methods and twoconstants that support mathematicalcomputation.

    Two constans: E dan PI is final, so you cannot extend it. constructor is private, so you cannot

    create an instance. the methods and constants are static

  • 8/11/2019 Java Advance Class

    32/39

    Maths method

  • 8/11/2019 Java Advance Class

    33/39

    Maths method

  • 8/11/2019 Java Advance Class

    34/39

    Wrapper Class

    is simply a class that encapsulatesa single and immutable value.

    Integer class wraps up an intvalue,

    Float class wraps up a float value.

  • 8/11/2019 Java Advance Class

    35/39

    Primitive and Wrapper

  • 8/11/2019 Java Advance Class

    36/39

    equals() dan == pada wrapperclass

    Method equals() membandingkancontent-nya

    == membandingkan alamatnya

  • 8/11/2019 Java Advance Class

    37/39

    Contoh equals() dan == pada wrapper

  • 8/11/2019 Java Advance Class

    38/39

    Retrieval Method public byte byteValue() public short shortValue() public int intValue() public long longValue() public float floatValue() public double doubleValue() public boolean booleanValue() public char charValue()

  • 8/11/2019 Java Advance Class

    39/39

    Kesimpulan Wrapper Class Tiap tipe data primitif mempunyai

    korespondensi dengan satu tipe wrapperclass.

    Semua tipe wrapper class dapat dibuat

    dari tipe data primitifnya Wrapped values can be tested for equality

    with the equals() method. All six numeric wrapper types support all

    six numeric XXXValue() methods. Wrapped values can be extracted with

    various XXXValue() methods. Wrapped values cannot be modified