100 likes | 122 Views
Java Tutorial 1. 2004/10/21. Java Resource. Java SDK Download from http://www.javasoft.com Install Jdk-1.5.0-windows-i586.exe 設定環境變數 Set classpath=c:program filesjavajdk-1.5.0libtools.jar;c;examplelibexample.jar;. Set Path=c:program filesjavajdk-1.5.0 測試 Java Javac.
E N D
Java Tutorial 1 2004/10/21
Java Resource • Java SDK • Download from http://www.javasoft.com • Install • Jdk-1.5.0-windows-i586.exe • 設定環境變數 • Set classpath=c:\program files\java\jdk-1.5.0\lib\tools.jar;c;\example\lib\example.jar;. • Set Path=c:\program files\java\jdk-1.5.0 • 測試 • Java • Javac
Java Program • Require • Program Editor • Notepad • UltraEdit • 可以編輯文字的編輯器都可以 • 檔案名稱需要與 Class 名稱相同(注意大小寫) • 副檔名需儲存為 .java • J2SE 5.0 Documentation • Java 2 Platform API Specification
Example • Like C Example --------------------------------------------------------------- public class forLoop { public static void main(String args[]) { for (int count=0;count<10;count++) { Systm.out.println (“count=“+count); } } } ---------------------------------------------------------------
Example(1) • forLoop.java • Class name is “forLoop”, 將 forLoop class 儲存成 forLoop.java • public static void main(args[])是程式執行的進入點 • for (int count=0;count<10;count++) { • 需要使用變數時才宣告 • Compile • Javac forLoop.java • 產生 forLoop.class 的可執行 byte code • Run • Java forLoop
Java Example(2) public class forLoop2 { public static void main(String args[]) { for (int count=0;count<10;count++) { Systm.out.println (“count=“+count); } System.out.println (“”); for (int count=0;count<10;count++) { Systm.out.println (“count=“+count); } } } • Save class as “forLoop2.java”
Java Standard Output • System.out • System.out.print(); • System.out.println(); • System.out.printf() • J2SE 5.0支援, 可寫跟 C 一樣的printf方式輸出
Java Input public class JavaInput { public static void main(String args[]) { If (args.length>0) { System.out.println (args[0]); System.out.println (args[1]); System.out.println (args[2]); } } } • Java使用args陣列來接收外部的輸入參數 • Javac JavaInput.java • Java JavaInput I am here • 輸出 I am here • 小於三個輸入會有錯誤
Course Example public class StringMatch { private String source; public void match(String taget) { for (int count=0;count<source.length();count++) { If (target.equals(source.substring(count,count+target.length()) { System.out.println (target); } } } public void setSource(String source) { this.source=source; } public static void main(String args[]) { StringMatch stm=new StringMatch(); stm.setSource(“abcabcabcabc”); stm.match(“cab”); } }
Course Example(2) public class StringMatch { private String source; public void match(String taget) { for (int count=0;count<source.length();count++) { If (target.equals(source.substring(count,count+target.length()) { System.out.println (target); } } } public void setSource(String source) { this.source=source; } public static void main(String args[]) { StringMatch stm=new StringMatch(); stm.setSource(“abcabcabcabc”); stm.match(args[0]); } }