You are on page 1of 13

Java Programming

Fundamental and Flow Control

Penamaan variabel
Case sensitive dan tidak boleh sama dengan keyword Diawali dengan huruf, _ , atau $. Dianjurkan menggunakan huruf untuk awal setiap variabel Tidak boleh mengandung spasi atau karakter khusus lainnya Jika variabel terdiri dari satu kata, dianjurkan untuk menggunakan huruf kecil. Dan jika lebih dari satu kata menggunakan Huruf besar untuk awal kata kedua dan seterusnya Variabel didefinisikan dalam format:
type identifier [ = value][, identifier [= value] ...] ;

Java Keyword

Tipe data
Java memiliki 8 tipe data standart (primitive types of data) yaitu: byte, short, int, long, char, float, double, and boolean. Kedelapan tipe data ini dikelompokkan menjadi: Integers (byte, short, int dan long) Floating-point (float and double) Characters I (char, dapat berisi dari huruf dan angka atau karakter khusus) Boolean (boolean, menunjukkan nilai true/false)

Tipe data.
Tipe data byte short integer long float double char boolean Default 0 0 0 0L 0.0f 0.0d \u0000 false ukuran 8bit/1byte 16 bit/2 byte 32 bit/4 byte 64 bit/8 byte 32 bit/4 byte 64 bit/8 byte Rentang nilai -128 s.d. 127 -32,768 s.d 32,767 -2,147,483,648 s.d. 2,147,483,647 -9,223,372,036,854,775,808 s.d. 9,223,372,036,854,775,807 1.4E-45 s.d 3.4028235E38 4.9E-324 s.d. 1.7976931348623157E308

16 bit unicode \u0000 s.d. \uffff true or false

Variable scope
Dalam java, sebuah variabel akan dikenali didalam blok dimana variabel tersebut dideklarasikan dan blokblok yang ada ditingkat yang lebih rendah
// Demonstrate block scope. class Scope { public static void main(String args[]) { // known to all code within main int x; x = 10; if(x == 10) { // start new scope // known only to this block int y = 20; // x and y both known here. System.out.println("x and y: " + x + " " + y); x = y * 2; } // y = 100; // Error! y not known here // x is still known here. System.out.println("x is " + x); } }

Operator
Simple Assignment Operator = Simple assignment operator

Arithmetic Operators + Additive operator (also used for String concatenation) Subtraction operator * Multiplication operator / Division operator % Remainder operator
Unary Operators + Unary plus operator; indicates positive value (numbers are positive without this, however) Unary minus operator; negates an expression ++ Increment operator; increments a value by 1 -Decrement operator; decrements a value by 1 ! Logical compliment operator; inverts the value of a boolean

Operator
Equality and Relational Operators == Equal to != Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to Conditional Operators && Conditional-AND || Conditional-OR ?: Ternary (shorthand for if-thenelse statement) Bitwise and Bit Shift Operators ~ Unary bitwise complement & Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR << Signed left shift >> Signed right shift >>> Unsigned right shift

Selection, if statement
Syntax:
if (condition) statement1; else statement2;
atau

Contoh:
int a, b; // penggunaan if if(a < b) a = 0; else b = 0;

if (condition) { statement1; } else { statement1; }

int bytesAvailable; // ... if (bytesAvailable > 0) { ProcessData(); bytesAvailable -= n; } else { waitForMoreData(); bytesAvailable = n; }

Selection, nested if
Syntax:
if(condition) statement; else if(condition) statement; else if(condition) statement; . . . else statement;

Contoh:
class IfElse { public static void main(String args[]) { int month = 4; // April String season;
if(month == 12 || month == 1 || month == 2) season = "Winter"; else if(month == 3 || month == 4 || month == 5) season = "Spring"; else if(month == 6 || month == 7 || month == 8) season = "Summer"; else if(month == 9 || month == 10 || month == 11) season = "Autumn"; else season = "Bogus Month"; System.out.println("April is in the " + season + ".");

}
}

Selection, switch
Syntax:
switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break; . . . case valueN: // statement sequence break; default: // default statement sequence }

Contoh:
class Switch { public static void main(String args[]) { int month = 4; String season; switch (month) { case 12: case 1: case 2: season = "Winter"; break; case 3: case 4: case 5: season = "Spring"; break; case 6: case 7: case 8: season = "Summer"; break; case 9: case 10: case 11: season = "Autumn"; break; default: season = "Bogus Month"; } System.out.println("April is in the " + season + "."); } }

Iteration: while, do..while and for


Syntax:
while(condition) { // body of loop }

do {

// body of loop } while (condition);

for(initialization; condition; iteration) { // body }

Iteration: contoh

You might also like