dasar pemograman java
Post on 26-May-2015
1.241 Views
Preview:
TRANSCRIPT
Dasar Pemrograman Java
My Collections
04/12/23 1Ah. Handoyo Collections
Topik:
Menggunakan komentar di file sumber. Membedakan antara valid dan invalid identifiers. Mengetahui Java technology keywords. Mengetahui 8 tipe data primitif. Mendefinisikan literal value untuk tipe data numerik dan
tekstual. Mendefinisikan primitive dan reference variable. Mendeklarasikan variabel bertipe class. Membuat obyek dengan menggunakan operator new. Mengetahui nilai inisialisasi default. State-state pada saat assign nilai pada variabel bertipe
class Garbage collection
04/12/23 2Ah. Handoyo Collections
Source Files
Java source files must end with the .java extension.
Three top-level elements known as compilation units may appear in a file.
Package Declaration Import Statements Class Definitions
04/12/23 3Ah. Handoyo Collections
04/12/23 4Ah. Handoyo Collections
Class Fundamentals: main method
The main() Methodpublic 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 main bisa langsung digunakan dalam contex class yang 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] …
04/12/23 5Ah. Handoyo Collections
04/12/23 6Ah. Handoyo Collections
04/12/23 7Ah. Handoyo Collections
04/12/23 8Ah. Handoyo Collections
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 gotoabstract 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 default if package synchronized
04/12/23 9Ah. Handoyo Collections
04/12/23 10Ah. Handoyo Collections
04/12/23 11Ah. Handoyo Collections
04/12/23 12Ah. Handoyo Collections
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());
}
} 04/12/23 13Ah. Handoyo Collections
Modifier : public, private, protected, dan default Constructor adalah bukan method, sehingga tidak punya return values
dan tidak diturunkan/diwariskan
04/12/23 14Ah. Handoyo Collections
Note:
• Jika kita mendeklarasikan constructor pada suatu class yang sebelumnya tidak mempunyai constructor, maka default constructor class tersebut akan hilang.
• Sehingga bila constructor yang kita buat tadi mempunyai argumen, kemudian kita buat obyek dengan cara new Xxx(), proses kompilasi akan menghasilkan error.
04/12/23 15Ah. Handoyo Collections
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. foobar // legal2. BIGinterface // legal: embedded
keywords3. // are OK.4. $incomeAfterExpenses // legal5. 3_node5 // illegal: starts with a
digit6. !theCase // illegal: must start
with7. // letter, $, or _
04/12/23 16Ah. Handoyo Collections
Primitive Types
The Java programming language defines eight primitive types: Logical - boolean Textual - char Integral - byte, short, int, and long Floating - double and float
04/12/23 17Ah. Handoyo Collections
Primitive Types
04/12/23 18Ah. Handoyo Collections
Literals
is a value cannot appear on the left side of
assignments.
04/12/23 19Ah. Handoyo Collections
Logical literals The boolean data type has two literals, true and
false. For example, the statement:
1. boolean isBig = true;2. boolean isLittle = false;
Note: boolean literal tidak boleh berharga 0 atau 1
04/12/23 20Ah. Handoyo Collections
Textual Literals
The range: 0 ~ 216 - 1. Java characters are in Unicode character
(16-bit encoding).
04/12/23 21Ah. Handoyo Collections
char literals
Expressed by enclosing the desired character in single quotes (‘ ‘).
Example:char c = ‘w’;
Express as a Unicode value specified using four hexadecimal digits, preceded by \u
Example:char c = ‘\u0063’;
04/12/23 22Ah. Handoyo Collections
char literals
Special Characters ‘\n’ for new line ‘\r’ for return ‘\t’ for tab ‘\b’ for backspace ‘\f’ for formfeed ‘\’’ for single quote ‘\”’ for double quote ‘\\’ for backslash
04/12/23 23Ah. Handoyo Collections
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”;
04/12/23 24Ah. Handoyo Collections
Integral literals byte, short, int and long
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.
'L' is preferred as it cannot be confused with the digit '1'.
Example:long x = 25L;
Has a default type of int 04/12/23 25Ah. Handoyo Collections
Integral
04/12/23 26Ah. Handoyo Collections
Floating-Point literals Floating point literal includes either a decimal
point or one of the following: E or e (add exponential value) F or f (float) D or d (double)
3.14 a simple floating 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. Example:
float x = 2.5F;
04/12/23 27Ah. Handoyo Collections
Note:
Semua tipe data primitif yang numerik (selain char dan boolean) adalah signed.
04/12/23 28Ah. Handoyo Collections
Member Variables Initialization
04/12/23 29Ah. Handoyo Collections
Reference variable
04/12/23 30Ah. Handoyo Collections
04/12/23 31Ah. Handoyo Collections
04/12/23 32Ah. Handoyo Collections
04/12/23 33Ah. Handoyo Collections
04/12/23 34Ah. Handoyo Collections
04/12/23 35Ah. Handoyo Collections
04/12/23 36Ah. Handoyo Collections
04/12/23 37Ah. Handoyo Collections
Argument Passing
The Java programming language only passes arguments by value
When an object instance is passed 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
04/12/23 38Ah. Handoyo Collections
> java PassTest
Int value is: 11
MyDate: 22-7-1964
MyDate: 4-7-196404/12/23 39Ah. Handoyo Collections
04/12/23 40Ah. Handoyo Collections
04/12/23 41Ah. Handoyo Collections
Garbage Collection 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 Garbage collection:
Checks for and frees memory no longer needed Is done automatically Can vary dramatically across JVM implementations
“run the garbage collector.” System.gc() and Runtime.gc()
04/12/23 42Ah. Handoyo Collections
http://java.sun.com/javaee/5/docs/api/http://java.sun.com/j2se/1.5.0/docs/api/
04/12/23 43Ah. Handoyo Collections
top related