Description

Java Advance ClassImam F Rozi Legal Identifier • Boleh diawali dengan karakter huruf, $ atau _ dan tidak boleh selainnya termasuk diawali dengan angka. • Setelah karakter pertama boleh diikuti oleh kombinasi karakter apapun • Tidak ada batasan panjang karakter • Tidak boleh pakai Java Keyword utk identifier • Case Sensitive Sun’s 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 kata pertama huruf kecil, dan hruf pertama kata selanjutnya diawali dengan huruf besar – Contoh: printInfoPegawai Sun’s Java Code Convention • Variable atau Atribut – Sama dengan aturan Method – Contoh: sisiMiring, tunjangan, bonus, namaPasien • Konstanta – Konstanta dibentuk dengan menggunakan kata kunci FINAL dan STATIC – Ditulis dengan huruf besar semua, dan menggunakan _ sebagai pemisah antar katanya – Contoh: MIN_HEIGHT, PHI JavaBeans Standard • JavaBeans merupakan klas dalam Java yang memiliki atribut. • Atribut tsb biasanya memiliki modifier akses Private. • Karena atributnya Private, satu2nya cara untuk mengaksesnya adalah dengan membuat method dg modifier akses public. • Method tsb disebut GETTER dan SETTER JavaBeans Standard • GETTER : method untuk mendapatkan/mengambil nilai atribut dari klas • SETTER : method untuk memberikan nilai kepada atribut dari suatu klas. JavaBeans Standard Aturan penamaan dalam JavaBeans: • Jika tipe data atribut selain boolean, maka nama method getter harus diawali dengan get. Misal atribut gaji  getGaji(). Tipe dari getter method menyesuaikan dengan tipedata dari atributnya. • Jika tipe data atributnya boolean, maka awalan getter bisa get atau is. Misal: atribut oke tipe data boolean  getOke() atau isOke(). JavaBeans Standard • Untuk setter method, diawali dengan set dan diikuti dengan nama atribut. Misal atribut gaji  setGaji(); Tipe setter method selalu VOID • Getter dan setter harus memiliki modifier akses PUBLIC • Getter tidak punya argumen fungsi • Setter memiliki argumen fungsi dengan tipe data argumen sesuai dengan tipe data atributnya. Misal : atribut double gaji  public void setGaji(double gj). Valid JavaBeans Example • public void setMyValue(int v) • public int getMyValue() • public boolean isMyStatus() Invalid JavaBeans Example • void setCustomerName(String s) // must be public • public void modifyMyValue(int v) // can't use 'modify' Advance Class • Object Class • String Class • StringBuffer Class • Math Class • Wrapper Class Paket java.lang • Kompiler java otomatis mengimport seluruh klas dalam paket java.lang ke setiap file program • Klas terpenting yang ada dalam paket ini : – Object – Math – Wrapper Class – String – StringBuffer 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() 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 a useful comparison. – Object reference comparison • if (d1 == d2) – Relevant instance variables comparison • if (d1.equals(d2)) Contoh 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”); Contoh • Override method equals dari kas MyDate agar lebih berguna Method toString() • Menghasilkan representasi string dari state object • Menampilkan nama kelas object diikuti oleh kode hash • Hash code merupakan tanda unik berupa integer diberikan oleh Java pada object • Override method toString() agar lebih berguna String Class • Memiliki beberapa konstruktor. • Common string constructors: – String s1 = new String(“immutable”); – String s1 = “immutable”; • Java mempunyai media penyimpanan literal string yang yang disebut “pool”. • Jika suatu literal string sudah ada di pool, Java “tidak akan membuat copy lagi”. String POOL Kedua potongan program diatas OK • Contoh pertama membandingkan contentnya. • Contoh kedua membandingkan referencesnya. == dan equals() utk String • Method equals() membandingkan content-nya • == membandingkan alamatnya Contoh • Variabel java1 dieksekusi pada saat runtime String method • char charAt(int index): Returns the indexed character of a string, where the index of the initial character is 0. • String concat(String addThis): Returns a new string consisting of the old string followed by addThis. • int compareTo(String otherString): Performs a lexical comparison; returns an int that is less than 0 if the current string is less than otherString, equal to 0 if the strings are identical, and greater than 0 if the current string is greater than otherString. • boolean endsWith(String suffix): Returns true if the current string ends with suffix; otherwise returns false String method • boolean equals(Object ob): Returns true if ob instanceof String and the string encapsulated by ob matches the string encapsulated by the executing object. • 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 and begin searching from a specified offset. • int lastIndexOf(int ch): Returns the index within the current string of the last occurrence of ch. Alternative forms return the index of a string and end searching at a specified offset from the end of the string 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 occurrence of oldChar with newChar. • boolean startsWith(String prefix): Returns true if the current string begins with prefix; otherwise returns false. Alternate forms begin searching from a specified offset. • String substring(int startIndex): Returns the substring, beginning at startIndex of the current string and extending to the end of the current string. An alternate form specifies starting and ending offsets. • String toLowerCase(): Creates a new string with the same value as the executing object, but in lower case String method • String toString(): Returns the executing object. • String toUpperCase(): Converts the executing object to uppercase and returns a new string. • String trim(): Returns the string that results from removing whitespace characters from the beginning and ending of the current string StringBuffer Class • represents a string that can be dynamically modified. • Constructors: – StringBuffer(): Constructs an empty string buffer – StringBuffer(int capacity): Constructs an empty string buffer with the specified initial capacity – StringBuffer(String initialString): Constructs a string buffer that initially contains the specified string StringBuffer Method • StringBuffer append(String str): Appends str to the current string buffer. Alternative forms support appending primitives and character arrays; these are converted to strings before appending. • StringBuffer append(Object obj): Calls toString() on obj and appends the result to the current string buffer. • StringBuffer insert(int offset, String str): Inserts str into the current string buffer at position offset. There are numerous alternative forms. • StringBuffer reverse(): Reverses the characters of the current string buffer. • StringBuffer setCharAt(int offset, char newChar): Replaces the character at position offset with newChar. • StringBuffer setLength(int newLength): Sets the length of the string buffer to newLength. If newLength is less than the current length, the string is truncated. If newLength is greater than the current length, the string is padded with null characters Modifying StringBuffer 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: – new StringBuffer().append(a).append(b).appen d(c).toString(); Math Class • a collection of methods and two constants that support mathematical computation. • 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 Math’s method Math’s method Wrapper Class • is simply a class that encapsulates a single and immutable value. • Integer class wraps up an int value, • Float class wraps up a float value. Primitive and Wrapper equals() dan == pada wrapper class • Method equals() membandingkan content-nya • == membandingkan alamatnya Contoh equals() dan == pada wrapper 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() Kesimpulan Wrapper Class • Tiap tipe data primitif mempunyai korespondensi dengan satu tipe wrapper class. • 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
Copyright © 2024 DOKUMEN.SITE Inc.