OBJECT ORIENTED PROGRAMMING D Day 2: Dasar Pemrograman Java
TOPIK: | | | | | | | | | | |
Menggunakan M k komentar k t di file fil sumber. b Membedakan antara valid dan invalid identifiers. Mengetahui g Java technology gy keywords. y Mengetahui 8 tipe data primitif. Mendefinisikan literal value untuk tipe data numerik dan tekstual. tekstual Mendefinisikan primitive dan reference variable. Mendeklarasikan variabel bertipe p class. Membuat obyek dengan menggunakan operator new. Mengetahui nilai inisialisasi default. St t t t pada State-state d saatt assign i nilai il i pada d variabel i b l bertipe b ti class Garbage collection
SOURCE FILES |
|
Java source files J fil must end d with i h the h .java extension. Th Three top-level t l l elements l t known k as compilation il ti units may appear in a file. Package Declaration y Import Statements y Class Definitions y
CLASS FUNDAMENTALS: MAIN METHOD |
The main() Method public static void main(String[] args) • Public : method main() dapat diakses oleh apa saja, termasuk java technology interpreter. • Static : keyword ini berfungsi untuk memberi tahu kompiler bahwa method th d main i bisa bi langsung l g g digunakan dig k dalam d l contex t class l yang g bersangkutan. Untuk mengeksekusi/menjalankan method yang bertipe static, tidak diperlukan instance nya. • Void : menunjukkan bahwa method main() tidak mengembalikan nilai • Main : merupakan nama method utama dari program java • String [] args : Menyatakan bahwa method main() menerima single parameter yaitu args yang bertipe array. Digunakan pada saat memasukkan parameter pada saat menjalankan program. Contoh: java TestGreeting args[0] args[1] …
JAVA KEYWORDS AND RESERVED WORDS |
are considered as reserved keywords
|
may not be used as identifiers.
|
None of the reserved words have a capital letters
|
2 keyword that are reserved in Java but which are not used : const dan goto abstract do
implements private
this
boolean
double
import
throw
break
else
instanceof public
byte
extends int
return
transient
case
false
interface
short
true
catch
final
long
static
try
char
finally native
strictfp
void
class
float
new
super
volatile
continue for
null
switch
while
default
package
synchronized
if
protected
throws
Public class TestDog{ public static void main(String args[]){ Dog d = new Dog(); d.setWeight(42); System.out.println(“Dog d’s weight is “ + d.getWeight()); } }
| |
Modifier : public, private, protected, dan default Constructor adalah bukan method, sehingga gg tidak p punya y return values dan tidak diturunkan/diwariskan
Note: • Jika kita mendeklarasikan constructor pada suatu class yang sebelumnya tidak mempunyai constructor, maka k d default f l constructor class l tersebut b akan k hilang. hil • Sehingga bila constructor yang kita buat tadi mempunyai p y argumen, g , kemudian kita buat obyek y dengan cara new Xxx(), proses kompilasi akan menghasilkan error.
IDENTIFIERS | | | |
Nama yang digunakan oleh programer untuk memberi nama pada variable, class, atau method. Can start with a Unicode letter, underscore (_), or dollar sign ($) Are case-sensitive and have no maximum length Examples: 1. 2. 3. 4. 5 5.
foobar BIGinterface
$incomeAfterExpenses 3 3_node5 d 5 digit 6. !theCase 7.
// // // // //
legal g legal: embedded keywords are OK. legal ill illegal: l starts t t with ith a
// illegal: must start with // letter, $, or _
PRIMITIVE TYPES |
The Java programming language defines eight primitive types: Logical - boolean b l y Textual - char y Integral - byte, short, int, and long y Floating - double and float y
PRIMITIVE TYPES
LITERALS is a value | cannot appear on the left side of assignments. |
LOGICAL LITERALS Th boolean data The d t ttype h has ttwo lit literals, l true and d false. | For example, example the statement: |
1. boolean isBig = true; 2 boolean 2. b l isLittle i i l = f false; l
Note: boolean literal tidak boleh berharga 0 atau 1
TEXTUAL LITERALS The range: 0 ~ 2166 - 1. | Java characters are in Unicode character (16-bit encoding). di ) |
CHAR LITERALS Expressed by enclosing the desired character in single quotes (‘ ‘). | Example: E l char c = ‘w’; |
Express as a Unicode E U i d value l specified ifi d using i four f hexadecimal digits, preceded by \u | Example: E l char c = ‘\u0063’; |
CHAR LITERALS |
Special Characters y y y y y y y y
‘\n’ for new line ‘\r’ \r for return ‘\t’ for tab ‘\b’ for backspace p ‘\f’ for formfeed ‘\’’ for single quote ‘\”’ for double quote ‘\\’ for backslash
STRING LITERALS Is not a primitive data type; it is a class | Represent sequences of characters | Has its literal enclosed in double quotes (“ ”) | Example: |
String s = “Characters in strings are 16-bit Unicode.”; String s = “Good Morning !! \n”;
INTEGRAL LITERALS Æ BYTE, SHORT, INT AND LONG |
E Expressed d in i decimal, d i l octal, t l or hexadecimal. h d i l 2 The decimal value is 2 077 The leading 0 indicates an octal value 0xBAAC The leading 0x indicates a hexadecimal value
|
Specify a long integer by putting an 'L' or 'l' after the number. 'L' is preferred as it cannot be confused with the digit '1'. y Example: y
long g x = 25L; ; |
Has a default type of int
INTEGRAL
FLOATING-POINT LITERALS |
Floating Fl ti point i t literal lit l includes i l d either ith a decimal d i l point or one of the following: y y y
E or e (add exponential value) F or f (float) D or d (double)
3.14 Æ a simple p floating gp point value ((a double)) 6.02E23 Æ a large floating point value 2.718F Æ a simple float size value 123.4E306D Æ a large double value
Default is double | Specify a float by putting an ‘F' or ‘f' after the number. b |
y
Example:
float x = 2.5F;
NOTE: Semua tipe data primitif yang numerik (selain char dan boolean) adalah signed.
MEMBER VARIABLES INITIALIZATION
Reference variable
ARGUMENT PASSING The Java programming language only passes arguments by value | When Wh an object bj t instance i t is i passed d as an argument to a method, the value of the argument is a reference to the object | The contents of the object can be changed in the called method, but the object reference is never changed |
> java PassTest Int value is: 11 y 22-7-1964 MyDate: MyDate: 4-7-1964
GARBAGE COLLECTION | | | |
Allocated All t d memory that th t is i no longer l needed d d should h ld b be deallocated In other languages, deallocation is the programmer's responsibility The Java programming language provides a system-level y allocation thread to track memory Garbage collection: Checks for and frees memory no longer needed y Is done automatically y Can vary dramatically across JVM implementations y
|
“run the garbage collector.” y
System.gc() and Runtime.gc()
http://java.sun.com/javaee/5/docs/api/ http://java.sun.com/j2se/1.5.0/docs/api/