初識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() 不換行


```

public class HelloWorld{

public static void main(String[] args){

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

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() 換行 


```

public class HelloWorld{

public static void main(String[] args){

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


```

public class HelloWorld{

public static void main(String[] args){

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

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

}

}

```

**運行效果**

 ![](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

```

public class HelloWorld{

public static void main(String[] args){

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

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

}

}

```

**運行效果** 

 

![](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{

public static void main(String[] args){

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)

```

public class ShowInfo{

public static void main(String[] args){

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. 單行註釋

#### 語法

```

//

```

#### 示例

```

public class HelloWorld{

public static void main(String[] args){

//這是換行輸出

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

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

}

}

```

程序運行結果

 

![](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. 多行註釋

#### 語法

```

/*

註釋內容

*/

```

#### 示例

```

public class HelloWorld{

/*

這是程序的入口

這是測試多行註釋

*/

public static void main(String[] args){

//這是換行輸出

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

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

}

}

```

### 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)


## (六) 案例

### 1. 需求

 

![](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("年齡:18");

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

}

}

```




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章