2015年8月31日 星期一

Static 的用法

Java 中的 Static 用法:

import java.util.Scanner;

public class blenderDemo2{
 static int juice = 0;
 
 public static void makeJuice(int fruit){
  
  switch(fruit){
   case 1:
    juice += 150;
    break;
   case 2:
    juice += 300;
    break;
   case 3:
    juice += 400;
    break;
  }   
 }

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  //blenderDemo2 myBlender = new blenderDemo2();
  
  System.out.print("請選擇水果種類:1) 蘋果  2) 香蕉 3) 芒果");
  int fruit = scanner.nextInt();
    
  switch(fruit){
   case 1:
    blenderDemo2.makeJuice(1);
    System.out.printf("蘋果汁:%d CC", blenderDemo2.juice);
    
   case 2:
    blenderDemo2.makeJuice(2);
    System.out.printf("香蕉牛奶:%d CC", blenderDemo2.juice);
    
   case 3:
    blenderDemo2.makeJuice(3);
    System.out.printf("芒果冰沙:%d CC", blenderDemo2.juice);
    
  }
 }
}

Overload 的語法

Java 的 Overload 語法:

import java.util.Scanner;

public class blenderDemo{

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  Blender myBlender = new Blender();
  
  System.out.print("請選擇水果種類:1) 蘋果  2) 香蕉 3) 芒果");
  int fruit = scanner.nextInt();
  
  switch(fruit){
   case 1:
    System.out.printf("蘋果汁:%d CC",myBlender.makeJuice(1));
    break;
   case 2:
    System.out.printf("香蕉牛奶:%d CC",myBlender.makeJuice(2,100));
    break;
   case 3:
    System.out.printf("芒果冰沙:%.2f CC",myBlender.makeJuice(3,100,200));
    break;
  } 
 }
}

public class Blender{

 public int makeJuice(int fruit){
  int juice = 0;
  switch(fruit){
   case 1:
    juice = 150;
    break;
   case 2:
    juice = 300;
    break;
   case 3:
    juice = 400;
    break;
  }   
  return juice;
 }
 
 public int makeJuice(int fruit, int milk){
  return (this.makeJuice(fruit)+ milk);
 }
 
 public double makeJuice(int fruit, int milk, int ice){
  return (this.makeJuice(fruit,milk)+ ice)/2.0;
 }
}

2015年8月29日 星期六

Java 考試考古題

http://www.wanlibo.com/mydoc-54137169-6.html&folderId=0

http://ppp.show55.info:8090/se2-hpFj6/?redurl_ww.lzzpp.net

http://www.itpub.net/forum.php

http://www.cjsdn.net/post/page?bid=9&sty=0&tpg=2&age=0&s=-1

http://wenku.it168.com/d_001448427.shtml

http://218.14.151.180:82/wenku.it168.com/d_000575622.shtml

Java 並行 API

Java 的 java.util.concurrent.atomic 應用:
import java.util.concurrent.atomic.*;

public class Demo{

 public static void main(String[] args){
  
  AtomicInteger ai = new AtomicInteger(5);
  
  if (ai.compareAndSet(5,42)){
   System.out.println("The current value: " + ai.get());
   System.out.println("Replaced 5 with 42");
  }
 }
}

Concurrent I/O 的應用:
public class MultiThreadedClientMain {
    public static void main(String[] args){
        ExecutorService es = Executors.newCachedThreadPool();
        Map < RequestResponse,Future < RequestResponse >> callables = 
                                                     new HashMap<>();
        
        String host = "localhost";
        for (int port = 10000; port <  10010 ; port++){
            RequestResponse lookup = new RequestResponse(host, port);
            NetworkClientCallable callable = 
                                  new NetworkClientCallable(lookup);
            Future future = es.submit(callable);
            callables.put(lookup, future);
        }
        es.shutdown();
        try{
            es.awaitTermination(5, TimeUnit.SECONDS);
        } catch(InterruptedException ex){
            System.out.println("Stopped waiting early");
        }
        
        for (RequestResponse lookup : callables.keySet()){
            Future future = callables.get(lookup);
            try {
                lookup = future.get();
                System.out.println(lookup.host + ":" + lookup.port + 
                     " " + lookup.response);
            } catch (ExecutionException | InterruptedException ex){
                System.out.println("Error talking to " + lookup.host +
                        ":" + lookup.port);
            }
        }
    }
}

public class RequestResponse {
    public String host;
    public int port;
    public String response;
    
    public RequestResponse(String host, int port){
        this.host = host;
        this.port = port;
    }
}

public class NetworkClientCallable implements Callable < RequestResponse > {
    private RequestResponse lookup;
    
    public NetworkClientCallable(RequestResponse lookup) {
        this.lookup = lookup;
    }

    @Override
    public RequestResponse call() throws IOException {
        try (Socket sock = new Socket(lookup.host, lookup.port);
             Scanner scanner = new Scanner(sock.getInputStream());){
            lookup.response = scanner.next();
        }
        return lookup;
    }   
}

2015年8月28日 星期五

Java 迴圈語法

Java 迴圈語法:while 、do/while 、for 迴圈

While 迴圈簡單範例:
public class TaxDemo{

 public static void main(String[] args){

  double balance = 500;
  double taxRate = 0.07;
  int years = 0;
  
  while (balance <= 1000){
  
   balance = (balance * (1+0.07));
   years++;
  
  }
  System.out.printf("Year %d: %.2f",years,balance);
 }
} 

修改一下 Elevator 的程式:(其他部份,請參考這裡!)
public class Elevator{
...omit....

//判斷是否到達所須要的樓層
 public void goToFloor(int desiredFloor){
  while (this.currentFloor != desiredFloor){
   if (this.currentFloor > desiredFloor){
    this.goDown();
   } else {
    this.goUp();
   }
  }
  System.out.println("Arrived..");
  this.openDoor();
 }

..... omit ......


執行的程式也一併修改一下:
import java.util.Scanner;

public class ElevatorDemo{

 public static void main(String[] args){
 
  Elevator myElevator = new Elevator();
  Scanner scanner = new Scanner(System.in);
  
  System.out.print("請選擇樓層(1~10):");
  myElevator.doorOpen = true;
  myElevator.goToFloor(scanner.nextInt()); 
  
 }
}


for 迴圈用法:(99乘法表)
public class table99{

 public static void main(String[] args){
 
  for (int i = 1,j = 1; i < 10 ; i=(j==9)?(i+1):(i),j=(j==9)?(1):(j+1)){
   System.out.printf("%d*%d=%d\t",i,j,(i*j));
   if (j==9){
    System.out.println();
   }
  }
 }
}

continue 與 break 也來湊熱鬧:
import java.util.*;

public class ScoreClassDemo{

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  ArrayList pass = new ArrayList();
  ArrayList noPass = new ArrayList();
  
  int account = 0;
  int score = 0;
  
  while (true){
   System.out.printf("請輸入第 %d 位學生成績:",(account + 1));
   score = scanner.nextInt();
   if ((score > 100)||(score <= -2)){
    System.out.println("重新輸入");
    continue;
   } else if (score == -1){
    break;
   } else if ( score >= 60){
    pass.add(score);
   } else {
    noPass.add(score);
   }
   account++;
  }
  System.out.println("及格人數: " + pass.size());
 }
}

2015年8月26日 星期三

Java 陣列的使用

Java 一維陣列的應用:
import java.util.Scanner;

public class OneArrayDemo{

 public static void main(String[] args){
 
  Shirt[] myShop = new Shirt[10];
  Scanner scanner = new Scanner(System.in);
  String temp1;
  
  myShop[0] = new Shirt();
   
  System.out.print("Please input the description: ");
  temp1 = scanner.nextLine();
  myShop[0].description = temp1;
  
  System.out.print("Please input the ShirtID: ");
  myShop[0].shirtID = scanner.nextInt();
    
  System.out.print("Please input the colorCode: ");
  temp1 = scanner.next();
  myShop[0].colorCode = temp1.charAt(0);
  
  System.out.print("Please input the price: ");
  myShop[0].price = scanner.nextDouble();
  
  myShop[0].displayInformation();
  
 }
}

Shirt 請參考另一篇文章

二位陣列使用方式:
import java.util.Scanner;

public class TwoArrayDemo{

 public static void main(String[] args){
  
  int courses = 3;
  int students = 10;
  int[][] scores = new int[courses][students];
  
  System.out.println("請輸入數學科分數:");
  TwoArrayDemo.inputScore(1,students,scores);
  System.out.println();
  System.out.println("請輸入國文科分數:");
  TwoArrayDemo.inputScore(2,students,scores);
  System.out.println();
  System.out.println("請輸入英文科分數:");
  TwoArrayDemo.inputScore(3,students,scores);
  System.out.println();
  System.out.println("每位學生的平均分數:");
  TwoArrayDemo.avgScore(courses,students,scores);
 }
 
 public static void inputScore(int course,int students, int[][] scores){
  Scanner scanner = new Scanner(System.in);
  int sum = 0;
  for (int i = 0; i < students; i++){
   System.out.printf("請輸入第 %d 位學生成績:",(i+1));
   scores[course-1][i] = scanner.nextInt();
   sum += scores[course-1][i];
  }
  System.out.println("本科平均分數:" + ((double)sum)/students);
 }
 
 public static void avgScore(int courses,int students,int[][] scores){
  
  int temp = 0;
  for (int i = 0 ; i < students ; i++){
   for (int j = 0 ; j < courses ; j++){
    temp += scores[j][i];
   }
   System.out.printf("第 %d 位學生平均分數: %.2f\n",i, ((double)temp)/courses);
   temp = 0;
  }
 }
}


ArrayList 的使用方式:
import java.util.*;

public class ArrayListDemo{

 public static void main(String[] args){
 
  ArrayList myList = new ArrayList();

  myList.add("Peter");
  myList.add("James");
  myList.add("Piggy");
  myList.add("Janeson");
  myList.add("Tordy");
  
  myList.remove(0);
  myList.remove(myList.size()-1);
  myList.remove("Piggy");
  
  System.out.println(myList);
 }
}

2015年8月24日 星期一

Java 決策判斷語法

Java 的關係運算子:== 、!= 、 > 、 >= 、 < 、 <= 、equals() ... 判斷的語法: if 、 if/else 、switch
先來點熱身的:
public class Employees{
 
 public String name1 = "Fred Smith";
 public String name2 = "Joseph Smith";
 
 public static void main(String[] args){
  Employees myEmployee = new Employees();
  myEmployee.areNamesEqual();
 }
 
 public void areNamesEqual(){
 
  //比較兩者是否為同一物件
  if ( name1 == name2 ){
   System.out.println("Same Object.");
  } else {
   System.out.println("Different Object.");
  }
 
  //比較物件內容值是否相同
  if (name1.equals(name2)){
   System.out.println("Same Name.");
  } else {
   System.out.println("Different Name.");
  }
 }
}
以電梯的類別來示範 if/else 以及 nested if/else 用法:
public class Elevator{

 public boolean doorOpen = false;
 public int currentFloor = 1;
 public final int TOP_FLOORS = 10;
 public final int MIN_FLOORS = 1;
 
 //開門的動作
 public void openDoor(){
  System.out.println("Opening Door ... ");
  doorOpen = true;
  System.out.println("Door is opened!");
 }
 
 //關門的動作
 public void closeDoor(){
  System.out.println("Closing Door ... ");
  doorOpen = false;
  System.out.println("Door is closed!");
 }
 
 //電梯向上
 public void goUp(){
  if ( currentFloor >= TOP_FLOORS){
   System.out.println("Cannot go Up!");
  } else {
    if (doorOpen){
 closeDoor();
    }

   System.out.println("Going Up one floor !");
   currentFloor++;
   System.out.println("Floor: " + currentFloor);
  }
 }

//電梯向下
 public void goDown(){
  if ( currentFloor <= MIN_FLOORS){
   System.out.println("Cannot go Down!");
  } else {

   if (doorOpen){
 closeDoor();
   }

   System.out.println("Going Down one floor !");
   currentFloor--;
   System.out.println("Floor: " + currentFloor);
  }
 }
}
執行看看....
public class ElevatorDemo{

 public static void main(String[] args){
 
  Elevator myElevator = new Elevator();
  
  myElevator.openDoor();
  myElevator.closeDoor();
  myElevator.goDown();
  myElevator.goUp();
  myElevator.goUp();
 }
}

一種 Low Low 的範例:
import java.util.Scanner;

public class MonthDemo{

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  System.out.print("請輸入月份:");
  int month = scanner.nextInt();
  if ( month == 1 || month == 3 ||month == 5 ||
   month == 7 || month == 8 || month == 10 ||
   month == 12 ){
    System.out.println("本月份有31天!");
   } else if (month == 2){
    System.out.println("本月份有28天!");
   } else if (month == 4 || month == 6 ||
   month == 9 || month == 11){
    System.out.println("本月份有30天!");
   }else{
    System.out.println("invalid days !");
  }
  
 }
}

一種好的範例:
import java.util.Scanner;

public class SwitchDemo{

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  System.out.print("請輸入月份:");
  int month = scanner.nextInt();
  switch (month){
   case 1:
   case 3:
   case 5:
   case 7:
   case 8:
   case 10:
   case 12:
    System.out.println("本月份有31天!");
    break;
   case 2:
    System.out.println("本月份有28天!");
    break;
   case 4:
   case 6:
   case 9:
   case 11:
    System.out.println("本月份有30天!");
    break;
   default:
    System.out.println("invalid days !");
  }
  
 }
}

2015年8月23日 星期日

利用 JDBC 撈取資料庫內容

利用 JDBC 撈取資料庫的方式:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionDemo {
    public static void main(String[] args)
                  throws ClassNotFoundException{
        
        Class.forName("com.mysql.jdbc.Driver");
        
        String url = "jdbc:mysql://localhost:3306/directory"; 
        String dbaccount = "directory";
        String dbpassword = "a123456";
        
        try(Connection conn = DriverManager.getConnection(url,
                 dbaccount, dbpassword);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql)){
                          
          //列出資料表欄位資料
            int numCols = rs.getMetaData().getColumnCount();
            String[] colsName = new String[numCols];
            String[] colsType = new String[numCols];
            for (int i = 0; i < numCols; i++){
                colsName[i] = rs.getMetaData().getColumnName(i+1);
                colsType[i] = rs.getMetaData().getColumnTypeName(i+1);
            }
            System.out.println("Numbers of columns returned:  "
                               + numCols);
            System.out.println("Column names/types returned: ");
             for (int i = 0; i < numCols; i++){
                 System.out.println( colsName[i] + " : " + colsType[i]);
             }
             
             //列出資料表所裝填的內容
            System.out.println(colsName[0] +"\t\t" + colsName[1] +
                              "\t" +colsName[2]);
            while (rs.next()){
               String rsID = rs.getString(colsName[0]);
               String rsUserName = rs.getString(colsName[1]);
               String rsEmail = rs.getString(colsName[2]);
               System.out.println(rsID + "\t\t" + rsUserName +
                                                "\t\t"+rsEmail);

        } catch (SQLException ex) {
           System.out.println("資料庫連結失敗....");
        }
    }
}

JDBC 連結資料庫

利用 JDBC 與資料庫連結:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionDemo {
    public static void main(String[] args)
                  throws ClassNotFoundException{
        
        Class.forName("com.mysql.jdbc.Driver");
        
        String url = "jdbc:mysql://localhost:3306/directory"; 
        String dbaccount = "directory";
        String dbpassword = "a123456";
        
        try(Connection conn = DriverManager.getConnection(url,
                 dbaccount, dbpassword)){
           
            if (!conn.isClosed()){
                System.out.println("資料庫連結成功....");
            }
            
        } catch (SQLException ex) {
           System.out.println("資料庫連結失敗....");
        }
    }
}

MySQL 的範例資料庫 directory,可以利用下列語法建立起來:

CREATE DATABASE  IF NOT EXISTS `directory`;
USE `directory`;

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `no` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(100) NOT NULL,
  `userEmail` varchar(100) NOT NULL,
  PRIMARY KEY (`no`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

LOCK TABLES `users` WRITE;
INSERT INTO `users` VALUES (1,'test','test@localhost'),
       (2,'hello','hello@localhost.domain'),(3,'world','world@test.123');
UNLOCK TABLES;

2015年8月22日 星期六

volatile 與 synchronized 使用

volatile 範例:
public class ExampleRunnable implements Runnable{
    private volatile int j;
    @Override
    public void run() {
        
        for (j = 0; j < 10; j+=2){
            System.out.println("j = " + j);
        }
    }   
}

Demo 一下,看看執行結果:
public class ThreadDemo {
    public static void main(String[] args){ 

        ExampleRunnable er1 = new ExampleRunnable();

        Thread t1 = new Thread(er1);
        Thread t2 = new Thread(er1);
        Thread t3 = new Thread(er1);
        t1.start();
        t2.start();
        t3.start();
        
    }
}


syhchronized 範例:


2015年8月21日 星期五

Java 物件的利用

Java 物件的新增與使用:

Shirt 類別程式,請參考另一篇文件

public class Demo2{

  public static void main(String[] args){
  
    Shirt myShirt = new Shirt();
    Shirt yourShirt = new Shirt();
    
    myShirt = yourShirt;
    
    myShirt.colorCode = 'R';
    yourShirt.colorCode = 'G';
    
    System.out.println("Shirt color: " + myShirt.colorCode);
  
  }
}

String 類別的利用:
public class StringDemo{

 public static void main(String[] args){
 
  String myString = "Hello";
  myString = myString.concat("! This is my First program!");
  myString = myString + "\n Please fill the screen !!".trim();
  
  myString = myString + "\nHAHA..".toLowerCase();
  System.out.println(myString);
  
  myString = (myString + "\nHAHA..").toLowerCase();
  System.out.println(myString);
  
  
  int stringLength = myString.length();
  System.out.println("Length : " +  stringLength);
  
  String partString = myString.substring(6,20);
  System.out.println(partString);
  
  boolean endWord = myString.endsWith("ha..");
  System.out.println("The results: " + endWord);
 
 }
}

StringBuilder 類別的利用:

public class StringBuilderDemo{

 public static void main(String[] args){
 
  StringBuilder myString = new StringBuilder("Hello,");
  myString.append(" Java!");
  
  String h1 = "Today is Monday!";
  String h2 = "World is a good place!!";
  
  myString.insert(6, h1);
  
  System.out.println("Total length: " + myString.length());
  
  myString.delete(6,12);
  myString.replace(13,19,h2);
  
  System.out.println(myString);
 }
}

Servlet 第一支程式

Servlet 第一支程式:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="HelloServlet", urlPatterns={"/hello.view"},
            loadOnStartup=1)
public class HelloWorld extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet HelloWorld</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet HelloWorld at "
               + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    
    @Override
    protected void doGet(HttpServletRequest request, 
                         HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    
    @Override
    protected void doPost(HttpServletRequest request, 
                          HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}

index.html 可以寫成下列方式 ...
<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>TODO write content</div>
    </body>
</html>

網址:
http://localhost:8080/ServletDemo1/hello.view

2015年8月19日 星期三

Java 基本運算子的使用

Java 的算術運算子:+ - * / %
public class Person{

 public int ageYears = 32;
 
 public void calculateAge(){
 
  int ageDays = ageYears * 365;
  long ageSeconds = ageYears * 365 * 24L * 60 * 60;
 
  System.out.println("You are " + ageDays + " days old.");
  System.out.println("You are " + ageSeconds + " seconds old.");
 }
}
執行它:
public class PersonDemo{

 public static void main(String[] args){
 
  Person peter = new Person();
  peter.calculateAge();
 
 }
}

配合 java.util.Scanner 物件來做為輸入數量的參考:
import java.util.Scanner;

public class Demo{

 public static void main(String[] args){
 
  Shirt myShirt = new Shirt();
  myShirt.shirtID = 100;
  myShirt.colorCode = 'B';
  myShirt.price = 45.12;
  myShirt.description = "45周年紀念衫";
  
  myShirt.displayInformation();
 
  Scanner scanner = new Scanner(System.in);
  System.out.print("請輸入購買件數:");
  int input = scanner.nextInt();
  System.out.println("總價:" + input*myShirt.price);
 
 }
}

Java 基本資料型態

Java 變數的資料型態分兩種:基本資料型態類別資料型態

基本資料型態:
  • 整數型態:byte (8 bits)、short (16 bits)、int (32 bits)、long (64 bits)
  • 浮點數型態:float ( 32 bits) 、double (64 bits)
  • 文字型態: char (16 bits)
  • 邏輯型態:boolean 

Java 物件內的變數宣告

Java 物件內的變數分成 Fields 與 Local Variables ,兩者有不同的地方:
public class Shirt{

 //這是 Fields
 public int shirtID = 0;
 public String description = "-----";
 public char colorCode = 'U' ; //R=red,G=green,B=blue,W=white
 public double price = 0.0;
 
 //顯示 Shirt 相關資料
 public void displayInformation(){
 
  System.out.println("Shirt ID: " +  shirtID);
  displayDescription(this.description);
  System.out.println("Shirt Color: " + colorCode);
  System.out.println("Shirt's price: " + price);
 }

 public static void displayDescription(String d1){
   //這是Local Variables
   String displayString = "";
   displayString = "Shirt description: " + d1;
   System.out.println(displayString);
 } 
}
Java 物件的變數名稱,可以利用物件來指派與修改!
public class Demo{

 public static void main(String[] args){
 
  Shirt myShirt = new Shirt();
  myShirt.shirtID = 100;
  myShirt.colorCode = 'B';
  myShirt.price = 45.12;
  myShirt.description = "45周年紀念衫";
  
  myShirt.displayInformation();
 
 }
}

2015年8月17日 星期一

Java 物件概要

Java 類別的形成:
public class Shirt{

 public int shirtID = 0;
 public String description = "-----";
 public char colorCode = 'U' ; //R=red,G=green,B=blue,W=white
 public double price = 0.0;
 
 //顯示 Shirt 相關資料
 public void displayInformation(){
 
  System.out.println("Shirt ID: " +  shirtID);
  System.out.println("Description: " + description);
  System.out.println("Shirt Color: " + colorCode);
  System.out.println("Shirt's price: " + price);
 }
}



Java 物件的利用:
public class Demo{

 public static void main(String[] args){
 
  Shirt myShirt = new Shirt();
  myShirt.displayInformation();
 
 }
}


Java 基本程式撰寫

Java 基本程式寫作方式:(通常第一支程式都是 Hello World !!)

public class HelloWorld {

    public static void main (String[] args) {
        System.out.println("Hello, world!");
    }
}

  • class <類別名稱>: 因為 Java 語言是物件導向語言,所以都是以 class 開頭來撰寫程式!而類別名稱,則是給這個類別命名,方便其他程式的呼叫!
  • public ... : 表示公開的權限!Java 語言權限,是代表其安全機制的由來,權限共分四級!利用適當的權限值,可確保程式被利用時的安全性!
  • public static void main(String[] args){...} : 表示 Java 程式開始執行的進入點。
  • System.out.println(...) :表示在文字介面視窗中,印出所需要的文字!
  • ; 分號表示程式表示式結束的描述,除了使用 {} 之外,每行 Java 程式描述句結束後,都應加上此符號!

接下來,將檔案存成與<類別名稱>相同的 <類別名稱.java> 檔案,例如本程式應存成 HelloWorld.java 檔案!
之後,將該檔案進行編譯,變成可執行的 Class 檔案!例如:
C:\workspace\test1> javac HelloWorld.java

最後執行該 Java 程式,應使用 Java 執行該 class 檔案!例如:
C:\workspace\test1> java HelloWorld

JDK 安裝與設定

Java 語言的撰寫,最重要的一件事,就是準備好工具!而最重要的工具,就是 JDK(Java Development Kits) !JDK 安裝與設定如下:
  1. Oracle 官方下載 JDK!
  2. 進行「下一步」到底的安裝!
  3. 在電腦的環境設定中,設定 Path 環境項目,加入 Java bin 的目錄!
  4. 利用下列指令測試:
  • javac  -version
  • java  -version

另一種重要的工具,就寫程式的軟體了!通常,利用文字編輯器即可!以下提供常見工具:
  1. Notepad++ : 純文字編輯器!台灣人寫的,給正要入門的新手,最好的寫作平台!
  2. NetBean : Oracle 官方的 IDE (Integrated Development Environment),簡單、易用!
  3. Eclipse : 業界常用!有多方開發的整合套件,方便開發任何 Java 程式語言!

最後一種常用工具: Java API Documents !! (Java SE 8)因為,沒人可以記得住 Java 所有可用 API ,所以,查文件也是一種功力的表現!

本站重新開張

本站重新開張!

幾經思考之後,發現最原始的寫作方式,是比較適合像小弟這種老人的!
經過大幅刪了之前的東西之後,發現了新的空間與想法!有如珍瓏琪局般的巧妙,在砍除已方的棋子之後,豁然開朗的產生新局!

也是該這麼整理了!

2015-08-17