O
BJECTO
RIENTEDD 2
O
BJECTO
RIENTEDP
ROGRAMMINGDay 2 :
T
OPIK:
M k k t di fil b
| Menggunakan komentar di file sumber.
| Membedakan antara valid dan invalid identifiers.
| Mengetahui Java technology keywords.g gy y
| Mengetahui 8 tipe data primitif.
| Mendefinisikan literal value untuk tipe data numerik dan
tekstual tekstual.
| Mendefinisikan primitive dan reference variable.
| Mendeklarasikan variabel bertipe class.p
| Membuat obyek dengan menggunakan operator new.
| Mengetahui nilai inisialisasi default.
St t t t d t i il i d i b l b ti
| State-state pada saat assign nilai pada variabel bertipe
class
S
OURCEF
ILESJ fil d i h h
| Java source files must end with the .java
extension.
Th t l l l t k il ti
| Three top-level elements known as compilation
units may appear in a file. y Package Declaration
y Package Declaration
y Import Statements
C
LASSF
UNDAMENTALS:
MAIN METHOD| The main() Method
public static void main(String[] args)
• Public : met hod main() dapat diakses oleh apa saj a, t ermasuk j ava t echnology int erpret er.
• St at ic : keyword ini berf ungsi unt uk memberi t ahu kompiler bahwa
t h d i bi l g g dig k d l t l g met hod main bisa langsung digunakan dalam cont ex class yang bersangkut an. Unt uk mengeksekusi/ menj alankan met hod yang bert ipe st at ic, t idak diperlukan inst ance nya.
• Void : menunj ukkan bahwa met hod main() t idak mengembalikan nilai • Void : menunj ukkan bahwa met hod main() t idak mengembalikan nilai • Main : merupakan nama met hod ut ama dari program j ava
• St ring [ ] args: Menyat akan bahwa met hod main() menerima single
paramet er yait u args yang bert ipe array. Digunakan pada saat memasukkan paramet er pada saat menj alankan program.
J
AVAK
EYWORDS ANDR
ESERVEDW
ORDS| are considered as reserved keywords
| may not be used as identifiers.
| None of the reserved words have a capital letters
| 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 protected throw
break else instanceof public throws
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
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, sehinggagg tidak punya return valuesp y dan
Note: Note:
• Jika kita mendeklarasikan constructor pada suatu class yang sebelumnya tidak mempunyai constructor,
k d f l l b k hil
maka default constructor class tersebut akan hilang.
I
DENTIFIERS| Nama yang digunakan oleh programer untuk memberi nama
| 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
| Are case-sensitive and have no maximum length
| Examples:
1. foobar // legalg
2. BIGinterface // legal: embedded keywords
3. // are OK.
4. $incomeAfterExpenses // legal
5 3 d 5 // ill l t t ith
5. 3_node5 // illegal: starts with a digit
6. !theCase // illegal: must start with
P
RIMITIVET
YPES| The Java programming language defines eight
primitive types: y Logical b l
y Logical - boolean
y Textual - char
y Integral -Integral bytebyte, , shortshort, , intint, and , and longlong
L
ITERALS| is a value
L
OGICAL LITERALSTh d t t h t lit l d
| The boolean data type has two literals, true and false.
| For example the statement: | For example, the statement:
1. boolean isBig = true;
2 b l i i l f l
2. boolean isLittle = false;
T
EXTUALL
ITERALS6
| The range: 0 ~ 216 - 1.
| Java characters are in Unicode character (16-bit
CHAR LITERALS
| Expressed by enclosing the desired character in
single quotes (‘ ‘).
E l
| Example:
char c = ‘w’;
E U i d l ifi d i f
| Express as a Unicode value specified using four
hexadecimal digits, preceded by \u
E l
| Example:
CHAR LITERALS
y ‘\b’ for backspacep
y ‘\f’ for formfeed
y ‘\’’ for single quote
y ‘\”’ for double quote
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.”;
INTEGRAL
LITERALSÆ
BYTE,
SHORT,
INTINTEGRAL
LITERALSÆ
BYTE,
SHORT,
INTAND LONG
E d i d i l t l h d i l
| Expressed in decimal, octal, or hexadecimal.
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.
y 'L' is preferred as it cannot be confused with the digit '1'.
y Example:
long x = 25L;g ;
FLOATING-POINT
LITERALSFl ti i t lit l i l d ith d i l
| Floating point literal includes either a decimal
point or one of the following:
y E or e (add exponential value)
y F or f (float)
y D or d (double)
3.14 Æ a simple floating point value (a double) p g p ( )
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
b
number.
y Example:
N
OTE:
A
RGUMENTP
ASSING| The Java programming language only passes
arguments by value
Wh bj t i t i d
| When an object instance is passed as an
argument to a method, the value of the argument is a reference to the object
is a reference to the object
| The contents of the object can be changed in the
> java PassTest > java PassTest I nt value is: 11
G
ARBAGEC
OLLECTIONAll t d th t i l d d h ld b
| Allocated memory that is no longer needed should be
deallocated
| In other languages, deallocation is the programmer's
responsibility
| The Java programming language provides a system-level
thread to track memory allocation y
| Garbage collection:
y Checks for and frees memory no longer needed
y Is done automatically
y Is done automatically
y Can vary dramatically across JVM implementations
| “run the garbage collector.”