天天看點

初識Java

# 一、 本章任務

編寫HelloWorld程式

列印控制台資訊

# 二、 本章目标

JDK的安裝與配置

會使用記事本開發簡單Java程式

會使用輸出語句在控制台輸出資訊

熟悉MyEclipse開發環境

# 三、 JDK的安裝

## (一) JDK的下載下傳

下載下傳位址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

## (二) 安裝JDK

## (三) 配置JDK

- path

- JAVA_HOME

##(四) 驗證JDK

![Paste_Image.png](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-80cb59c173e17f03.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

# 四、 開發第一個Java程式

## (一) 步驟

- 源代碼

- 編譯

- 運作

## (二) 編寫HelloWorld程式

### 1. 編寫源代碼

指令行:notepad

```java

public class HelloWorld{

public static void main(String[] args){

System.out.print("Hello World");

}

```

注意:

- 檔案名必須是:HelloWorld

- 檔案的字尾名:.java

### 2. 編譯

javac 

![Paste_Image.png](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-3a444f1f0481ffdb.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

 注意點:javac 後面的檔案名必須包括字尾名(.java)

成功的辨別:

![Paste_Image.png](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-259d2ab40ae2a8f4.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 3. 運作

java

 ![Paste_Image.png](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-bfdd3f8135aebac4.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

**注意點:java 後面不需要檔案字尾名**

## (三) 了解Java程式的結構

### 1. 結構

 ![Paste_Image.png](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-42b7852be5ae350b.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 2. 輸出語句

- System.out.print() 不換行

System.out.print("今天很熱");

**運作效果:**

 ![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-9a665541c1ac4a1f.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

- System.out.println() 換行 

System.out.println("Hello World");

System.out.println("今天很熱");

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-b842753ec89057eb.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

## (四) 轉義字元:\t和\n

### 1. 含義

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-c10589c00bf64a75.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 2. 示例

- \n

System.out.print("Hello World\n");

**運作效果**

 ![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-028ab536edce944d.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

- \t

System.out.print("Hello World\t");

**運作效果** 

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-984a6794e9a7b650.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

## (五) 列印輸出的小例子

### 1. 需求

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-f3a0e5af8f7e7474.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-70d9e009bb697986.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

 ### 2. 參考代碼

public class ShowInfo{

System.out.println("姓名:張三");

System.out.println("年齡:18");

**程式運作效果**

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-2758a0489711066a.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

System.out.print("張三\t");

System.out.print("18");

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-2a030bd94594d045.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

## (六) java的注釋

為了友善他人閱讀程式

注釋是不會被執行的

### 1. 單行注釋

#### 文法

//

#### 示例

//這是換行輸出

程式運作結果

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-5c33bbebf50926e2.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

**注:注釋不會被執行**

### 2. 多行注釋

/*

注釋内容

*/

這是程式的入口

這是測試多行注釋

### 3. 文檔注釋

/**

## (七) Java編碼規範

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-44b406900b91e8fb.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-1928d72a77a2e3ac.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

# 五、 使用MyEclipse開發Java程式

## (一) 下載下傳

百度

## (二) 破解

參考教程

## (三) MyEclipse建立一個Java項目的步驟

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-f7fd91d36f0229ea.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 1. 建立一個Java項目

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-efff771442e9f842.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

建立成功的樣式

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-a89d31b6d92777b7.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 2. 建立并編寫Java源程式

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-64190bf6ff593884.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-5341aa98181b932d.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 3. 編譯和運作

編譯自動完成

運作

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-49bb8a4eb9d3c760.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

## (四) Java項目組織結構

### 1. 導航視圖

![Paste_Image.png](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-d3cc24c79754e530.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

#### 目錄說明

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-4e299ab27dda9555.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

- bin:存儲編譯後的檔案

- src:存儲源程式

### 2. 包視圖

包:相當于檔案夾,在不同的包中,可以有相同名的代碼檔案

## (五) 編寫Javat程式常見的幾種錯誤

### 1. public修飾的類名必須和檔案名稱相同

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-beb6ef044fc3e5e7.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 2. main方法作為程式的入口,void必不可少

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-1b6bb6ef3d03ecd3.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 3. java對大小寫敏感

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-0a4bba84255c6b8a.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 4. 每一個Java語句必須以分号結尾

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-7ca6e8653e3a5853.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 5. 引号必不可少

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-de7a4052ff76caee.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

## (六) 案例

![](http://upload-p_w_picpaths.jianshu.io/upload_p_w_picpaths/5818381-4ef35ff1eaee3bd9.png?p_w_picpathMogr2/auto-orient/strip%7Cp_w_picpathView2/2/w/1240)

### 2. 參考代碼

public class ShowUserInfo {

public static void main(String[] args) {

System.out.println("你好,我是青鳥的學生");

System.out.println("姓名:王五");

System.out.println("愛好:打籃球");