Pemrograman Berorientasi Objek Pengenalan JAVA 1 Pemrograman Berorientasi
Pemrograman Berorientasi Objek Pengenalan JAVA 1
Pemrograman Berorientasi Objek Outline • • • Pengenalan Java First Simple Program Struktur program meng-compile program Keyword, tipe data, operator Pengenalan JAVA 2
Pemrograman Berorientasi Objek Struktur Program JAVA • • • Pemrograman Java adalah kumpulan spasi, komentar, kata kunci, identifier, literal, operator, dan pemisah. Pemrograman Java adalah bahasa bebas bentuk. Tidak perlu mengatur tata letaknya agar dapat bekerja. Asalkan ada sekurang-kurangnya satu spasi, tab, atau baris baru diantara setiap token sebelum disisipi operator atau pemisah lain. Komentar, ada beberapa bentuk : – Komentar baris tunggal, Diawali dengan tanda // dan diletakkan diakhir baris yang diberi komentar. – Komentar baris banyak, Diawali dengan tanda /* dan ditutup dengan */ semua diantara kedua tanda tersebut dianggap komentar dan akan diabaikan oleh compiler. – Komentar terdokumentasi, dengan menggunakan piranti Javadoc, yang menggunakan komponen compiler Java untuk secara otomatis menghasilkan dokumentasi antarmuka public suatu class. Aturan pembuatan komentar yang dapat diolah oleh Javadoc adalah : sebelum deklarasi class, method, dan variabel public harus digunakan komentar bertanda /** untuk menyatakan komentar dokumentasi, diakhiri dengan tanda */. Javadoc akan mengenali sejumlah variabel khusus yang didahului dengan tanda @ didalam bagian komentar. Pengenalan JAVA 3
Pemrograman Berorientasi Objek RESERVED WORD Sampai dengan Versi 1. 0 terdapat 59 kata kunci seperti terlihat dalam tabel : abstract boolean break byte byvalue cast catch char class const continue default do double else extends false finally float for future generic goto if implements import inner instanceof interface long native new null operator outer package rest return short static super switch synchronized this throws transient true try var void volatile while private protected Pengenalan JAVA public 4
Pemrograman Berorientasi Objek • Identifier – Digunakan untuk nama class, method, dan variabel. Suatu variabel dapat berupa urutan tertentu huruf (besar atau kecil), angka, garis bawah, dan tanda dolar. – Tidak boleh diawali oleh angka dan bersifat case sensitive. – Kelompok Java mengikuti aturan penamaan identifier untuk semua method public dan variabel instans dengan huruf awal kecil dan menandai bagian kata selanjutnya dengan huruf besar. • Misalnya next. Item, current. Value, get. Time. Of. Day. – Untuk variabel private dan lokal identifier akan berupa huruf kecil semua dikombinasikan dengan garis bawah • misalnya next_val, temp_val. Untuk variabel final yang mewakili suatu konstanta, digunakan huruf besar semua, misalnya TOK_BRACE, DAY_FRIDAY. • Literal – Besaran konstanta pada Java dihasilkan dengan menggunakan literal yang mewakilinya. – Setiap literal merepresentasikan nilai suatu tipe, dimana tipe itu sendiri menjelaskan bagaimana sifat nilai tersebut dan bagaimana penyimpanannya. Pengenalan JAVA 5
Pemrograman Berorientasi Objek • Separator (Pemisah) Simbol Nama Fungsi () Kurung Digunakan untuk menghimpun parameter dalam definisi dan pemanggilan method, juga digunakan untuk menyatakan tingkatan pernyataan, menghimpun pernyataan untuk pengaturan alur program dan menyatakan tipe cast. {} kurung kurawal Digunakan untuk menghimpun nilai yang otomatis dimasukkan kedalam array, juga digunakan untuk mendefinisikan blok program, untuk cakupan class, method, dan lokal variabel. [] kurung siku Digunakan untuk menyatakan tipe array, juga digunakan untuk membedakan nilai array. ; titik-koma , koma Pemisah urutan identifier dalam deklarasi variabel, juga digunakan untuk mengaitkan pernyataan didalam pernyataan for. . titik Dugunakan untuk memisahkan nama paket dari sub-paket dan class, juga digunakan untuk memisahkan variabel atau method dari variabel referensi. pemisah pernyataan. Pengenalan JAVA 6
Pemrograman Berorientasi Objek TIPE DATA Keyword Description Size/Format (integers) byte Byte-length integer 8 -bit two's complement short Short integer 16 -bit two's complement int Integer 32 -bit two's complement long Long integer 64 -bit two's complement (real numbers) float Single-precision floating point 32 -bit IEEE 754 double Double-precision floating point 64 -bit IEEE 754 (other types) char A single character 16 -bit Unicode character boolean A boolean value (true or false) true or false Pengenalan JAVA 7
Pemrograman Berorientasi Objek OPERATOR • Operator Aritmatika (Arithmetic operator), Operator Use Description + op 1 + op 2 Adds op 1 and op 2 - op 1 - op 2 Subtracts op 2 from op 1 * op 2 Multiplies op 1 by op 2 / op 1 / op 2 Divides op 1 by op 2 % op 1 % op 2 Computes the remainder of dividing op 1 by op 2 • Operators increment/decrement Operator Use Description ++ op++ Increments op by 1; evaluates to the value of op before it was incremented ++ ++op Increments op by 1; evaluates to the value of op after it was incremented -- op-- Decrements op by 1; evaluates to the value of op before it was decremented -- --op Decrements op by 1; evaluates to the value of op after it was decremented Pengenalan JAVA 8
Pemrograman Berorientasi Objek • Operator Hubungan (Relational Operator) Operator Use Returns true if > op 1 > op 2 op 1 is greater than op 2 >= op 1 >= op 2 op 1 is greater than or equal to op 2 < op 1 < op 2 op 1 is less than op 2 <= op 1 <= op 2 op 1 is less than or equal to op 2 == op 1 == op 2 op 1 and op 2 are equal != op 1 != op 2 op 1 and op 2 are not equal • Conditional operators Operator Use Returns true if && op 1 && op 2 op 1 and op 2 are both true, conditionally evaluates op 2 || op 1 || op 2 either op 1 or op 2 is true, conditionally evaluates op 2 ! ! op op is false & op 1 & op 2 op 1 and op 2 are both true, always evaluates op 1 and op 2 | op 1 | op 2 either op 1 or op 2 is true, always evaluates op 1 and op 2 ^ op 1 ^ op 2 if op 1 and op 2 are different--that is if one or the other of the operands is true but not both Pengenalan JAVA 9
Pemrograman Berorientasi Objek • Shift Operators Operator Use Operation >> op 1 >> op 2 shift bits of op 1 right by distance op 2 << op 1 << op 2 shift bits of op 1 left by distance op 2 >>> op 1 >>> op 2 shift bits of op 1 right by distance op 2 (unsigned) • Logical Operators Operator Use Operation & op 1 & op 2 bitwise and | op 1 | op 2 bitwise or ^ op 1 ^ op 2 bitwise xor ~ ~op 2 bitwise complement Pengenalan JAVA 10
Pemrograman Berorientasi Objek • Other Operators Operator Use Description ? : op 1 ? op 2 : op 3 If op 1 is true, returns op 2. Otherwise, returns op 3. [] type [ ] Declares an array of unknown length, which contains type elements. [] type[ op 1 ] Creates and array with op 1 elements. Must be used with the new operator. [] op 1[ op 2 ] Accesses the element at op 2 index within the array op 1. Indices begin at 0 and extend through the length of the array minus one. . op 1. op 2 Is a reference to the op 2 member of op 1(params) Declares or calls the method named op 1 with the specified parameters. The list of parameters can be an empty list. The list is comma-separated. (type) op 1 Casts (converts) op 1 to type. An exception will be thrown if the type of op 1 is incompatible with type. new op 1 Creates a new object or array. op 1 is either a call to a constructor, . or an array specification. instanceof op 1 instanceof op 2 Returns true if op 1 is an instance of op 2 () (type) Pengenalan JAVA 11
Pemrograman Berorientasi Objek Referensi • • Herbert Schildt, Java 2 : A beginner’s Guide, Second Edition, Mc. Graw-Hill/Osborne Patrick Naughton, Java Handbook : Konsep dasar pemrograman java, Mc. Graw. Hill/Osborne Pengenalan JAVA 12
Pemrograman Berorientasi Objek Kesimpulan • Pengenalan struktur bahasa JAVA • Tipe data yang disediakan java berupa tipe data primitif • Reserved word pada java bersifat case sensitif • Java merupakan bahasa pemrograman yang bebas aturan penulisan Pengenalan JAVA 13
- Slides: 13