JAVA APPLET Creating Web Application using Java Applet Introduction to Java (2 nd part)
Tipe Data Pada Java KeywordDescriptionSize/Format (integers) byte Byte-length integer8-bit two's complement short integer16-bit two's complement int Integer32-bit two's complement long Long integer64-bit two's complement
Tipe Data Pada Java KeywordDescriptionSize/Format (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 character16-bit Unicode character boolean A boolean value (true or false) true or false
Contoh LiteralData Type 178int 8864Llong double Ddouble Ffloat 26.77e3double 'c''c'char trueboolean falseboolean
Tata Cara Penulisan Penulisan variabel (wajib) : Serangkaian Unicode karakter yang diawali dengan huruf. Bukan keyword Java, boolean true/false, & null. Harus unique dalam sebuah scope. Boleh sama jika memiliki scope yang berbeda.
Konvensi Penulisan Konvensi penulisan untuk method dan field (tidak wajib tapi direkomendasikan) : Nama variabel diawali huruf kecil, dan nama class diawali huruf kapital. Nama method klausa kerja dan ditulis dengan diawali oleh huruf kecil untuk kata pertama dan huruf besar untuk setiap huruf pertama dari kata-kata berikutnya, jika ada. Contoh : hitungLuas(), hitungKell(), selesai(). Field static ditulis dengan menggunakan huruf kapital semua. Contoh : MAX_PANJANG, LEBAR, PHI Field biasa tidak dibuat public melainkan diakses dan diubah dengan melalui pemanggilan method method.
Contoh public class MaxVariablesDemo { public static void main(String args[]) { //integers byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; //real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; //other primitive types char aChar = 'S'; boolean aBoolean = true;
System.out.println(“max byte = " + largestByte); System.out.println(“max short = " + largestShort); System.out.println(“max integer = " + largestInteger); System.out.println(“max long = " + largestLong); System.out.println(“max float = " + largestFloat); System.out.println(“max double = " + largestDouble); if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is uppercase."); } else { System.out.println("The character " + aChar + " is lowercase."); } System.out.println("The value of aBoolean is " + aBoolean); }
Operator OperatorUseDescription +op1 + op2 Adds op1 and op2 -op1 - op2 Subtracts op2 from op1 *op1 * op2 Multiplies op1 by op2 /op1 / op2 Divides op1 by op2 %op1 % op2 Computes the remainder of dividing op1 by op2
Operator OperatorUseDescription ++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 ++op Promotes op to int if it's a byte, short, or char --op Arithmetically negates op
Operator OperatorUse Returns True if >op1 > op2op1 is greater than op2 >=op1 >= op2op1 is greater than or equal to op2 <op1 < op2op1 is less than op2 <=op1 <= op2op1 is less than or equal to op2 ==op1 == op2op1 and op2 are equal !=op1 != op2op1 and op2 are not equal
Operator OperatorUse Returns True if &&op1 && op2op1 and op2 are both true, conditionally evaluates op2 ||op1 || op2 either op1 or op2 is true, conditionally evaluates op2 !! opop is false &op1 & op2op1 and op2 are both true, always evaluates op1 and op2 |op1 | op2 either op1 or op2 is true, always evaluates op1 and op2 ^op1 ^ op2 if op1 and op2 are different--that is if one or the other of the operands is true but not both
Operator OperatorUseOperation >>op1 >> op2 shift bits of op1 right by distance op2 <<op1 << op2 shift bits of op1 left by distance op2 >>>op1 >>> op2 shift bits of op1 right by distance op2 (unsigned)
Operator OperatorUseOperation &op1 & op2 bitwise and |op1 | op2 bitwise or ^op1 ^ op2 bitwise xor ~~op2 bitwise complement
Assignment Operator OperatorUseEquivalent to +=op1 += op2op1 = op1 + op2 -=op1 -= op2op1 = op1 - op2 *=op1 *= op2op1 = op1 * op2 /=op1 /= op2op1 = op1 / op2 %=op1 %= op2op1 = op1 % op2 &=op1 &= op2op1 = op1 & op2 |=op1 |= op2op1 = op1 | op2 ^=op1 ^= op2op1 = op1 ^ op2 <<=op1 <<= op2op1 = op1 << op2 >>=op1 >>= op2op1 = op1 >> op2 >>>=op1 >>>= op2op1 = op1 >>> op2
Additional OperatorUseDescription ?:op1 ? op2 : op3 If op1 is true, returns op2. Otherwise, returns op3. []type [] Declares an array of unknown length, which contains type elements. type[ op1 ] Creates and array with op1 elements. Must be used with the new operator. op1[ op2 ] Accesses the element at op2 index within the array op1. Indices begin at 0 and extend through the length of the array minus one.
Additional.op1.op2 Is a reference to the op2 member of op1. ()op1(params) Declares or calls the method named op1 with the specified parameters. The list of parameters can be an empty list. The list is comma-separated. (type)(type) op1 Casts (converts) op1 to type. An exception will be thrown if the type of op1 is incompatible with type. newnew op1 Creates a new object or array. op1 is either a call to a constructor, or an array specification. instanceofop1 instanceof op2 Returns true if op1 is an instance of op2
ARRAY public class ArrayDemo { public static void main(String[] args) { int[] anArray; anArray = new int[10]; for (int i = 0; i < anArray.length; i++) { anArray[i] = i; System.out.print(anArray[i] + " "); }
ARRAY public class ArrayOfStringsDemo { public static void main(String[] args) { String[] anArray = { "String One", "String Two", "String Three" }; for (int i = 0; i < anArray.length; i++) { System.out.println(anArray[i].toLowerCase()) ; }
Statement Conditional dan Looping
Statement TypeKeyword loopingwhile, do-while, for decision makingif-else, switch-case exception handlingtry-catch-finally, throw branchingbreak, continue, label:, return
do…while & while…do do…while syntax do { statement(s) } while (expression); while…do syntax : while (expression) { statement }
The for statement syntax : for(init; termination; inc) { statement } To the infinity… for ( ; ; ) { // infinite loop... } for…
if…else… Syntax : if (expression) { statement(s) } [else { statements (s) }]
Example of if…else… public class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); }
Bentuk Lain if.. else ?: operator syntax condition? statement1 : statement2; Jika condition bernilai true, maka statement1 akan dieksekusi. Sebaliknya, bila bernilai false maka statement2 akan dieksekusi.
Switch switch statement syntax : switch (integer expression) { case integer expression: statement(s); break;... default: statement(s); break; }
Contoh public class SwitchDemo { public static void main(String[] args) { int month = 8; switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Heh!?"); break;} }
Introduction to Exception Untuk menangani kesalahan dalam program, Java menyediakan sebuah mekanisme yang disebut exception. Ketika sebuah error muncul, program akan mengeluarkan exception. Dengan adanya exception berarti program tidak berjalan dengan semestinya (misalnya file yang akan dibaca tidak ditemukan, dll), maka JVM akan mencari exception handler.
Bentuk Umum Exception handler adalah kode yang akan menangani masalah yang bersangkutan. Ada banyak jenis exception, seperti FileNotFoundException, IOException, dll. Syntax : try { statement(s) } catch (exceptiontype name) { statement(s) } finally { statement(s) }