程序註釋
1、註釋分類
- 註釋可以對程序進行說明並且不會被編譯器編譯;
- Java一共分爲三種形式:
// 註釋:單行註釋;
/* … /:多行註釋;
/* … */:文檔註釋。
2、範例:
- 定義單行註釋
public class JavaDemo {
public static void main(String args[]) {
// 【單行註釋】下面的程序語句功能是在屏幕上打印一行輸出信息
System.out.println("www.mldn.cn") ;
}
}
- 定義多行注視
public class JavaDemo {
public static void main(String args[]) {
/* 【多行註釋】下面的程序語句功能是在屏幕上打印一行輸出信息
* 利用多行注視可以針對於代碼的功能進行更加詳細的說明
* 在實際的項目開發中適合於編寫大段的說明文字
*/
System.out.println("www.mldn.cn") ;
}
}
- 使用文檔註釋
/**
* 該類的主要作用是在屏幕上輸出信息
* @author 李興華
*/
public class JavaDemo {
public static void main(String args[]) {
System.out.println("www.mldn.cn") ;
}
}
標識符與關鍵字
1、標識符組成
在Java之中標識符定義的核心原則如下:由字母、數字、_、$所組成,其中不能使用數字開頭,不能使用Java中的保留字(或者被稱爲“關鍵字”)。
2、Java關鍵字
數據類型
1、數據類型劃分
2、Java基本數據類型的大小、範圍、默認值
3、變量定義與賦值處理格式
4、定義int型變量
public class JavaDemo {
public static void main(String args[]) {
// int 變量名稱 = 常量(10是一個常量,整數類型爲int) ;
int x = 10; // 定義了一個整型變量x,變量定義時一定要給出默認值
// int型變量 * int型變量 = int型數據
System.out.println(x * x); // 輸出計算結果
}
}
5、觀察數據溢出
public class JavaDemo {
public static void main(String args[]) {
int max = 2147483647; // 獲取int的最大值
int min = -2147483648; // 獲取int的最小值
// int型變量 + int型常量 = int型計算結果
System.out.println(max + 1); // -2147483648,最大值 + 1 = 最小值
System.out.println(max + 2); // -2147483647,最大值 +2 = 次最小值
// int型變量 - int型常量 = int型計算結果
System.out.println(min - 1); // 2147483647,最小值 - 1 = 最大值
}
}
6、解決數據溢出
public class JavaDemo {
public static void main(String args[]) {
long max = 2147483647; // 獲取int的最大值
long min = -2147483648; // 獲取int的最小值
// long型變量 + int型常量 = long型計算結果
System.out.println(max + 1); // 【正確計算結果】2147483648
System.out.println(max + 2); // 【正確計算結果】2147483649
// long型變量 - int型常量 = long型計算結果
System.out.println(min - 1); // 【正確計算結果】-2147483649
}
}
7、強制類型轉換
public class JavaDemo {
public static void main(String args[]) {
long num = 2147483649L; // 此數值已經超過了int範圍
int temp = (int) num; // 【數據溢出】long範圍比int範圍大,不能夠直接轉換
System.out.println(temp);// 內容輸出
}
}
8、byte
字節是一種存儲容量的基本單位,在Java中可以使用byte定義,並且byte也屬於整型定義,其保存的範圍是:-128 ~ 127
public class JavaDemo {
public static void main(String args[]) {
byte num = 20; // 定義byte型變量
System.out.println(num);// 輸出byte型變量
}
}
9、定義double變量
public class JavaDemo {
public static void main(String args[]) {
double x = 10.2;// 10.2是一個小數其對應的類型爲double
int y = 10;// 定義int型變量
double result = x * y;// double類型 * int類型 = double類型
System.out.println(result); // 輸出計算結果
}
}
10、定義float型變量
public class JavaDemo {
public static void main(String args[]) {
float x = (float) 10.2; // 強制類型轉換:double強制轉爲float
float y = 10.1F; // 強制類型轉換:double強制轉爲float
System.out.println(x * y); // 計算結果類型爲:float
}
}
11、觀察整型除法計算
public class JavaDemo {
public static void main(String args[]) {
int x = 10;// 整型變量
int y = 4;// 整型變量
System.out.println(x / y);// 除法計算,類型爲int(不保留小數位)
}
}
12、解決除法計算中小數點問題
public class JavaDemo {
public static void main(String args[]) {
int x = 10;// 整型變量
int y = 4;// 整型變量
// 爲保留小數位,將計算結果中的int轉爲float或double
System.out.println((double) x / y); // 除法計算,最終類型爲double
System.out.println(x / (float) y); // 除法計算,最終類型爲double
}
}
13、char
Java使用的是十六進制的UNICODE編碼,此類編碼可以保存任意的文字。
public class JavaDemo {
public static void main(String args[]) {
char c = 'A'; // 定義字符變量
System.out.println(c);// 輸出字符變量內容
}
}
14、char與int轉換
public class JavaDemo {
public static void main(String args[]) {
char c = 'A'; // 字符變量
int num = c; // 可以獲得字符的編碼樹枝
System.out.println(num);
num = num + 32 ; // 修改編碼內容,大小寫之間差32
System.out.println((char) num);// 將num轉爲char
}
}
15、觀察boolean類型
public class JavaDemo {
public static void main(String args[]) {
boolean flag = true;// 定義布爾類型變量
if (flag) { // 判斷flag的內容,如果是true就執行
System.out.println("www.mldn.cn");
}
}
}
16、String
字符串是在實際項目之中所使用的一種類型,利用字符串可以保存更多的字符內容,Java中使用“"”來實現字符串常量定義,而對應的類型爲String。
public class JavaDemo {
public static void main(String args[]) {
String str = "www.mldn.cn";// 使用“"”進行描述
System.out.println(str);// 輸出字符串變量內容
}
}
17、字符串連接
public class JavaDemo {
public static void main(String args[]) {
String str = "www."; // 使用“"”進行描述
str = str + "mldn.";// 字符串連接
str += "cn";// 字符串連接
System.out.println(str);// 輸出字符串內容
}
}
18、轉義字符
換行(\n)、製表符(\t)、\(\)、雙引號(")、單引號(’)
public class JavaDemo {
public static void main(String args[]) {
System.out.println("魔樂科技:\tMLDN\n在線學習網站:\"www.mldn.cn\"") ;
}
}