Pengenalan Java Applet (2)

JAVA APPLET
Creating Web Application using Java Applet
Introduction to Java (2nd part)


Tipe Data Pada Java
Keyword Description Size/Format
(integers)
byte Byte-length integer 8-bit two's complement
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


Contoh
Literal Data Type
178 int
8864L long
37.266 double
37.266D double
87.363F float
26.77e3 double
'c' char
true boolean
false boolean

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
Operator Use Description
+ 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 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
+ +op Promotes op to int if it's a byte, short, or char
- -op Arithmetically negates op
Operator Use Returns True if
> op1 > op2 op1 is greater than op2
>= op1 >= op2 op1 is greater than or equal to op2
< op1 < op2 op1 is less than op2
<= op1 <= op2 op1 is less than or equal to op2
== op1 == op2 op1 and op2 are equal
!= op1 != op2 op1 and op2 are not equal
Operator Use Returns True if
&& op1 && op2 op1 and op2 are both true, conditionally evaluates op2
|| op1 || op2 either op1 or op2 is true, conditionally evaluates op2
! ! op op is false
& op1 & op2 op1 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 Use Operation
>> 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 Use Operation
& op1 & op2 bitwise and
| op1 | op2 bitwise or
^ op1 ^ op2 bitwise xor
~ ~op2 bitwise complement

Assignment Operator
Operator Use Equivalent to
+= op1 += op2 op1 = op1 + op2
-= op1 -= op2 op1 = op1 - op2
*= op1 *= op2 op1 = op1 * op2
/= op1 /= op2 op1 = op1 / op2
%= op1 %= op2 op1 = op1 % op2
&= op1 &= op2 op1 = op1 & op2
|= op1 |= op2 op1 = op1 | op2
^= op1 ^= op2 op1 = op1 ^ op2
<<= op1 <<= op2 op1 = op1 << op2
>>= op1 >>= op2 op1 = op1 >> op2
>>>= op1 >>>= op2 op1 = op1 >>> op2


Additional
Operator Use Description
?: 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.
. 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.
new new op1 Creates a new object or array. op1 is either a call to a constructor, or an array specification.
instanceof op1 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] + " ");
}
}
}
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 Type Keyword
looping while, do-while , for
decision making if-else, switch-case
exception handling try-catch-finally, throw
branching break, continue, label:, return


do…while & while…do
do…while syntax
do {
statement(s)
} while (expression);

while…do syntax :
while (expression) {
statement
}


for…
The for statement syntax :
for(init; termination; inc) {
statement
}

To the infinity…
for ( ; ; ) {
// infinite loop ...
}


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)
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...