> Database 實驗室專用程式語言 Java
Language - 2 Outline Programming language chaos 一小時精通 Java 不是夢 Java 環境設定
Language - 3 Laguage 的戰國時代 Delph i C Basic C++ Pascal C Java Delph i
Language - 4 基本型態 & 運算式 boolean char 16-bit byte 8-bit short 16-bit int 32-bit long 64-bit float 32-bit double 64-bit void if…else while do…while for loop switch…case ++ 、 -- 、 >= == 、 != && 、 || 、
Language - 5 Say Hello in C // Hello.c #include int main(int argc, char* args[]) { printf(“Hello!\n”); return 0; }
Language - 6 Class 類別 // Hello.java public class Hello { // … } Class 的宣告方式 檔案名稱必須和有 public 修飾字宣告的類別名稱一樣。
Language - 7 Say Hello in Java // Hello.java import java.lang.*; public class Hello { public static void main(String[] args) { System.out.println(“Hello!”); }
Language - 8 Java 編譯器 javac.exe % ls Hello.java % javac Hello.java % ls Hello.class Hello.java % ls Hello.java % javac Hello.java % ls Hello.class Hello.java
Language - 9 Java 直譯器 java.exe 啟動虛擬機器 執行應用程式 % ls Hello.class % java Hello Hello! % % ls Hello.class % java Hello Hello! %
Language - 10 Fibonacci Sequence … … … …
Language - 11 Solution in C // Fibonacci.c #include int fib(int n) { if (n < 2) return n; else return fib(n-2) + fib(n-1); }
Language - 12 Fibonacci (Cont.) // Fibonacci.c (cont.) int main(int argc, char* args[]) { printf(“%d\n”, fib(10)); // 55 return 0; }
Language - 13 Solution in Java // Fibonacci.java import java.lang.*; public class Fibonacci { static int fib(int n) { if (n < 2) return n; else return fib(n-2) + fib(n-1); } // cont…
Language - 14 Fibonacci (Cont.) // Fibonacci.java (cont.) public static void main(String[] args) { System.out.println(fib(10)); // 55 } } // end class
Language - 15 Global Variable in C // Global.c #include int myScore = 0; void increment() { myScore = myScore + 1; } void incrementByTwo() { myScore = myScore + 2; }
Language - 16 Global in C (Cont.) // Global.c (cont.) int main(int argc, char* args[]) { increment(); printf("myScore = %d\n", myScore); // myScore = 1 incrementByTwo(); printf("myScore = %d\n", myScore); // myScore = 3 }
Language - 17 Global Variable in Java // Global.java import java.lang.*; public class Global { static int myScore = 0; static void increment() { myScore = myScore + 1; } static void incrementByTwo() { myScore = myScore + 2; } // cont…
Language - 18 Global in Java (Cont.) // Global.java (cont.) public static void main(String[] args) { increment(); System.out.println("myScore = " + myScore); incrementByTwo(); System.out.println("myScore = " + myScore); } } // end class
Language - 19 struct in C // Person.c #include struct Person { int height; int weight; }; // cont…
Language - 20 Struct in C (Cont.) // Person.c (cont.) int main(int argc, char* args[]) { struct Person Jordan; Jordan.height = 198; Jordan.weight = 90; printf("Height is %d\n", Jordan.height); // Height is 198 printf("Weight is %d\n", Jordan.weight); // Weight is 90 return 0; }
Language - 21 class in Java // PersonTest.java import java.lang.*; class Person { int height = 0; int weight = 0; } // cont…
Language - 22 class in Java // PersonTest.java (cont.) public class PersonTest { public static void main(String[] args){ Person Jordan = new Person(); Jordan.height = 198; Jordan.weight = 90; System.out.println("Height is " + Jordan.height); // Height is 198 System.out.println("Weight is " + Jordan.weight); // Weight is 90 } } // end class
Language - 23 Types in Java 基本型態 (primitive type) 參考型態 (reference type) 物件 陣列 int grade = 89; double PI = ;
Language - 24 建立與操控物件 You must create all objects. String son = new String(“Peter”); You manipulate objects with reference. String son = new String(“Peter”); int i = son.indexOf(“t”); // i = 2 物件和 reference 之間的關係,好比電視和遙控器之間的 關係一樣。 reference String
Language - 25 基礎型別的 Array C int grades[10]; grades[0] = 99; grades[1] = 72; int[] grades = new int[10]; grades[0] = 99; grades[1] = 72; Java Java 中陣列的名稱只是一個 reference
Language - 26 Array of Objects String bird = new String(“bird”); String[] pets = new String[4]; pets[0] = “dog”; pets[1] = new String(“fish”); pets[2] = bird; // pets[3] = null 物件陣列的 element 是指向該物件的 reference ,而不是物 件的真正實體,每個 element 的預設值都是 null 。
Language - 27 Array of String 圖示 pets String[] names[0] names[1] names[2] names[3] String null
Language - 28 陣列的建立與初始化 int[] primes = {2, 3, 5, 7, 7+4}; // primes[4] = 11 String[] children = {“Peter”, new String(“Sunny”)}; 匿名陣列 (anonymous array) void setPets(String[] pets) { // … } String dog = “Dog”; setPets( new String[] {“Bird”, new String(“Fish”), dog} );
Language - 29 陣列的長度 // ArrayLength.java import java.lang.*; public class ArrayLength { public static void main(String[] args) { showLength(new String[][] { {"a", "b", "c"}, {"d", "e", "f"} } ); } // cont…
Language - 30 陣列的長度 (Cont.) // ArrayLength.java (Cont.) static void showLength(String[][] letters) { System.out.println("letters is " + letters.length); // 2 System.out.println("letters[0] is " + letters[0].length); // 3 System.out.println("letters[1] is " + letters[1].length); // 3 } } // end class
Language - 31 陣列終極洗禮 Java 視 array 為一個特殊的物件 Java 使用 new 來配置陣列的實體 Java 的 array 有一個專用的 field – length ,用 來表達陣列的長度
Language - 32 Java 環境設定 下載 Java Development Kit (JDK) 設定 Java_Home 環境變數 更新 PATH 環境變數 PATH=%PATH%;%JAVA_HOME%\bin;