Dasar pemograman java

1,838 views

Published on

Dasar pemograman java

  1. 1. Dasar Pemrograman Java My Collections 06/22/11 Ah. Handoyo Collections
  2. 2. Topik: <ul><li>Menggunakan komentar di file sumber. </li></ul><ul><li>Membedakan antara valid dan invalid identifiers. </li></ul><ul><li>Mengetahui Java technology keywords. </li></ul><ul><li>Mengetahui 8 tipe data primitif. </li></ul><ul><li>Mendefinisikan literal value untuk tipe data numerik dan tekstual. </li></ul><ul><li>Mendefinisikan primitive dan reference variable. </li></ul><ul><li>Mendeklarasikan variabel bertipe class. </li></ul><ul><li>Membuat obyek dengan menggunakan operator new. </li></ul><ul><li>Mengetahui nilai inisialisasi default. </li></ul><ul><li>State-state pada saat assign nilai pada variabel bertipe class </li></ul><ul><li>Garbage collection </li></ul>06/22/11 Ah. Handoyo Collections
  3. 3. Source Files <ul><li>Java source files must end with the .java extension. </li></ul><ul><li>Three top-level elements known as compilation units may appear in a file. </li></ul><ul><ul><li>Package Declaration </li></ul></ul><ul><ul><li>Import Statements </li></ul></ul><ul><ul><li>Class Definitions </li></ul></ul>06/22/11 Ah. Handoyo Collections
  4. 4. 06/22/11 Ah. Handoyo Collections
  5. 5. Class Fundamentals: main method <ul><li>The main() Method </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>     Public : method main() dapat diakses oleh apa saja, termasuk java </li></ul><ul><li>technology interpreter. </li></ul><ul><li>     Static : keyword ini berfungsi untuk memberi tahu kompiler bahwa </li></ul><ul><li>method main bisa langsung digunakan dalam contex class yang </li></ul><ul><li>bersangkutan. Untuk mengeksekusi/menjalankan method yang </li></ul><ul><li>bertipe static, tidak diperlukan instance nya. </li></ul><ul><li>      Void : menunjukkan bahwa method main() tidak mengembalikan nilai </li></ul><ul><li>      Main : merupakan nama method utama dari program java </li></ul><ul><li>      String [] a rgs : Menyatakan bahwa method main() menerima single </li></ul><ul><li> parameter yaitu args yang bertipe array. Digunakan pada saat memasukkan </li></ul><ul><li>parameter pada saat menjalankan program. </li></ul><ul><li>Contoh: java TestGreeting args[0] args[1] … </li></ul>06/22/11 Ah. Handoyo Collections
  6. 6. 06/22/11 Ah. Handoyo Collections
  7. 7. 06/22/11 Ah. Handoyo Collections
  8. 8. 06/22/11 Ah. Handoyo Collections
  9. 9. Java Keywords and Reserved Words <ul><li>are considered as reserved keywords </li></ul><ul><li>may not be used as identifiers. </li></ul><ul><li>None of the reserved words have a capital letters </li></ul><ul><li>2 keyword that are reserved in Java but which are not used : const dan goto </li></ul>06/22/11 Ah. Handoyo Collections
  10. 10. 06/22/11 Ah. Handoyo Collections
  11. 11. 06/22/11 Ah. Handoyo Collections
  12. 12. 06/22/11 Ah. Handoyo Collections
  13. 13. 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() ); } } 06/22/11 Ah. Handoyo Collections
  14. 14. <ul><li>Modifier : public, private, protected, dan default </li></ul><ul><li>Constructor adalah bukan method , sehingga tidak punya return values dan tidak diturunkan/diwariskan </li></ul>06/22/11 Ah. Handoyo Collections
  15. 15. <ul><li>Note: </li></ul><ul><li>Jika kita mendeklarasikan constructor pada suatu class yang sebelumnya tidak mempunyai constructor, maka default constructor class tersebut akan hilang. </li></ul><ul><li>Sehingga bila constructor yang kita buat tadi mempunyai argumen, kemudian kita buat obyek dengan cara new Xxx(), proses kompilasi akan menghasilkan error. </li></ul>06/22/11 Ah. Handoyo Collections
  16. 16. Identifiers <ul><li>Nama yang digunakan oleh programer untuk memberi nama pada variable, class, atau method. </li></ul><ul><li>Can start with a Unicode letter, underscore (_), or dollar sign ($) </li></ul><ul><li>Are case-sensitive and have no maximum length </li></ul><ul><li>Examples: </li></ul><ul><ul><li>1. foobar // legal </li></ul></ul><ul><ul><li>2. BIGinterface // legal: embedded keywords </li></ul></ul><ul><ul><li>3. // are OK. </li></ul></ul><ul><ul><li>4. $incomeAfterExpenses // legal </li></ul></ul><ul><ul><li>5. 3_node5 // illegal: starts with a digit </li></ul></ul><ul><ul><li>6. !theCase // illegal: must start with </li></ul></ul><ul><ul><li>7. // letter, $, or _ </li></ul></ul>06/22/11 Ah. Handoyo Collections
  17. 17. Primitive Types <ul><li>The Java programming language defines eight primitive types: </li></ul><ul><ul><li>Logical - boolean </li></ul></ul><ul><ul><li>Textual - char </li></ul></ul><ul><ul><li>Integral - byte , short , int , and long </li></ul></ul><ul><ul><li>Floating - double and float </li></ul></ul>06/22/11 Ah. Handoyo Collections
  18. 18. Primitive Types 06/22/11 Ah. Handoyo Collections
  19. 19. Literals <ul><li>is a value </li></ul><ul><li>cannot appear on the left side of assignments. </li></ul>06/22/11 Ah. Handoyo Collections
  20. 20. Logical literals <ul><li>The boolean data type has two literals, true and false . </li></ul><ul><li>For example, the statement: </li></ul><ul><ul><li>1. boolean isBig = true; </li></ul></ul><ul><ul><li>2. boolean isLittle = false; </li></ul></ul>Note: boolean literal tidak boleh berharga 0 atau 1 06/22/11 Ah. Handoyo Collections
  21. 21. Textual Literals <ul><li>The range: 0 ~ 2 16 - 1. </li></ul><ul><li>Java characters are in Unicode character (16-bit encoding). </li></ul>06/22/11 Ah. Handoyo Collections
  22. 22. char literals <ul><li>Expressed by enclosing the desired character in single quotes (‘ ‘). </li></ul><ul><li>Example: </li></ul><ul><li>char c = ‘w’; </li></ul><ul><li>Express as a Unicode value specified using four hexadecimal digits, preceded by u </li></ul><ul><li>Example: </li></ul><ul><li>char c = ‘u 0063 ’; </li></ul>06/22/11 Ah. Handoyo Collections
  23. 23. char literals <ul><li>Special Characters </li></ul><ul><ul><li>‘ n’ for new line </li></ul></ul><ul><ul><li>‘ r’ for return </li></ul></ul><ul><ul><li>‘ t’ for tab </li></ul></ul><ul><ul><li>‘ b’ for backspace </li></ul></ul><ul><ul><li>‘ f’ for formfeed </li></ul></ul><ul><ul><li>‘ ’’ for single quote </li></ul></ul><ul><ul><li>‘ ”’ for double quote </li></ul></ul><ul><ul><li>‘ ’ for backslash </li></ul></ul>06/22/11 Ah. Handoyo Collections
  24. 24. String literals <ul><li>Is not a primitive data type; it is a class </li></ul><ul><li>Represent sequences of characters </li></ul><ul><li>Has its literal enclosed in double quotes (“ ”) </li></ul><ul><li>Example: </li></ul><ul><li>String s = “Characters in strings are 16-bit Unicode.”; </li></ul><ul><li>String s = “Good Morning !! n”; </li></ul>06/22/11 Ah. Handoyo Collections
  25. 25. Integral literals  byte, short, int and long <ul><li>Expressed in decimal, octal, or hexadecimal. </li></ul><ul><ul><li>2 The decimal value is 2 </li></ul></ul><ul><ul><li>077 The leading 0 indicates an octal value </li></ul></ul><ul><ul><li>0xBAAC The leading 0x indicates a </li></ul></ul><ul><ul><li>hexadecimal value </li></ul></ul><ul><li>Specify a long integer by putting an 'L' or 'l' after the number. </li></ul><ul><ul><li>'L' is preferred as it cannot be confused with the digit '1'. </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>long x = 25L; </li></ul></ul></ul><ul><li>Has a default type of int </li></ul>06/22/11 Ah. Handoyo Collections
  26. 26. Integral 06/22/11 Ah. Handoyo Collections
  27. 27. Floating-Point literals <ul><li>Floating point literal includes either a decimal point or one of the following: </li></ul><ul><ul><li>E or e (add exponential value) </li></ul></ul><ul><ul><li>F or f (float) </li></ul></ul><ul><ul><li>D or d (double) </li></ul></ul><ul><ul><li>3.14  a simple floating point value (a double) </li></ul></ul><ul><ul><li>6.02E23  a large floating point value </li></ul></ul><ul><ul><li>2.718F  a simple float size value </li></ul></ul><ul><ul><li>123.4E306D  a large double value </li></ul></ul><ul><li>Default is double </li></ul><ul><li>Specify a float by putting an ‘F' or ‘f' after the number. </li></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>float x = 2.5F; </li></ul></ul></ul>06/22/11 Ah. Handoyo Collections
  28. 28. Note: <ul><li>Semua tipe data primitif yang numerik (selain char dan boolean) adalah signed . </li></ul>06/22/11 Ah. Handoyo Collections
  29. 29. Member Variables Initialization 06/22/11 Ah. Handoyo Collections
  30. 30. Reference variable 06/22/11 Ah. Handoyo Collections
  31. 31. 06/22/11 Ah. Handoyo Collections
  32. 32. 06/22/11 Ah. Handoyo Collections
  33. 33. 06/22/11 Ah. Handoyo Collections
  34. 34. 06/22/11 Ah. Handoyo Collections
  35. 35. 06/22/11 Ah. Handoyo Collections
  36. 36. 06/22/11 Ah. Handoyo Collections
  37. 37. 06/22/11 Ah. Handoyo Collections
  38. 38. Argument Passing <ul><li>The Java programming language only passes arguments by value </li></ul><ul><li>When an object instance is passed as an argument to a method, the value of the argument is a reference to the object </li></ul><ul><li>The contents of the object can be changed in the called method, but the object reference is never changed </li></ul>06/22/11 Ah. Handoyo Collections
  39. 39. > java PassTest Int value is: 11 MyDate: 22-7-1964 MyDate: 4-7-1964 06/22/11 Ah. Handoyo Collections
  40. 40. 06/22/11 Ah. Handoyo Collections
  41. 41. 06/22/11 Ah. Handoyo Collections
  42. 42. Garbage Collection <ul><li>Allocated memory that is no longer needed should be deallocated </li></ul><ul><li>In other languages, deallocation is the programmer's responsibility </li></ul><ul><li>The Java programming language provides a system-level thread to track memory allocation </li></ul><ul><li>Garbage collection: </li></ul><ul><ul><li>Checks for and frees memory no longer needed </li></ul></ul><ul><ul><li>Is done automatically </li></ul></ul><ul><ul><li>Can vary dramatically across JVM implementations </li></ul></ul><ul><li>“ run the garbage collector.” </li></ul><ul><ul><li>System.gc() and Runtime.gc() </li></ul></ul>06/22/11 Ah. Handoyo Collections
  43. 43. http://java.sun.com/javaee/5/docs/api/ http://java.sun.com/j2se/1.5.0/docs/api/ 06/22/11 Ah. Handoyo Collections

×