1. 變數介紹
變數是程式的基本組成單位
概念
- 變數相當於記憶體中一個數據儲存空間的表示,可以通過變數名可以訪問到變數(值)。
變數使用
宣告變數 int a;
賦值 a = 20;
public class Var01 { //編寫一個main方法
public static void main(String[] args) { //宣告變數
int a;
a = 100;
System.out.println(a); //還可以這樣使用
int b = 800;
System.out.println(b);
}
}
2. 變數快速入門
public class Var02 {
//編寫一個main方法
public static void main(String[] args) {
//記錄人的資訊
int age = 30;
double score = 88.9;
char gender = '男';
String name = "king";
//輸出資訊, 快捷鍵
System.out.println("人的資訊如下:");
System.out.println(name);
System.out.println(age);
System.out.println(score);
System.out.println(gender);
}
}
3. 變數使用注意事項
- 變量表示記憶體中的一個儲存區域,不同的變數,型別不同,佔用的空間大小不同
- 該區域有自己的名稱(變數名)和型別(資料型別)
- 變數必須先宣告,後使用,即有順序
- 該區域的資料/值可以在同一類型範圍內不斷變化
- 變數在同一個作用域內不能重名
- 變數三要素:變數名、值、資料型別
public class VarDetail {
//編寫一個main方法
public static void main(String[] args) {
//變數必須先宣告,後使用, 即有順序
int a = 50;//int
System.out.println(a);//50
//該區域的資料/值可以在同一類型範圍內不斷變化
//a = "jack"; //error
a = 88; //ok
System.out.println(a);//88
//變數在同一個作用域內不能重名
//int a = 77;//error
}
}
class Dog {
public static void main(String[] args) {
int a = 666;//對
}
}
4. 程式中 “+” 的使用
當左右兩邊都是數值型時,則做加法運算
當左右兩邊有一方為字串,則做拼接運算
運算順序,從左到右
System.out.println(100 + 98); //198
System.out.println("100" + 98); //10098
System.out.println(100 + 3 + "hello"); //103hello
System.out.println("hello" + 100 + 3); //hello1003
5. 資料型別
- 基本資料型別
- 數值型
- byte[1] short[2] int[4] long[8] float[4] double[8]
- 字元型
- char[2]
- 布林型
- boolean[1]
- 數值型
- 引用資料型別
- 類(class)
- 介面(interface)
- 陣列([])
6. 基本資料型別轉換
自動型別轉換
char----->int----->long----->float----->double
byte----->short----->int----->long----->float----->double
public class AutoConvert { //編寫一個main方法
public static void main(String[] args) {
//演示自動轉換
int num = 'a';//ok char -> int
double d1 = 80; //ok int -> double
System.out.println(num);//97
System.out.println(d1);//80.0 }
}
//自動型別轉換細節
public class AutoConvertDetail { //編寫一個main方法
public static void main(String[] args) {
//細節1: 有多種型別的資料混合運算時,
//系統首先自動將所有資料轉換成容量最大的那種資料型別,然後再進行計算
int n1 = 10; //ok
//float d1 = n1 + 1.1;//錯誤 n1 + 1.1 => 結果型別是 double
//double d1 = n1 + 1.1;//對 n1 + 1.1 => 結果型別是 double
float d1 = n1 + 1.1F;//對 n1 + 1.1 => 結果型別是 float //細節2: 當我們把精度(容量)大 的資料型別賦值給精度(容量)小 的資料型別時,
//就會報錯,反之就會進行自動型別轉換。
//
//int n2 = 1.1;//錯誤 double -> int //細節3: (byte, short) 和 char之間不會相互自動轉換
//當把具體數賦給 byte 時,(1)先判斷該數是否在byte範圍內,如果是就可以
byte b1 = 10; //對 , -128-127
// int n2 = 1; //n2 是int
// byte b2 = n2; //錯誤,原因: 如果是變數賦值,判斷型別
//
// char c1 = b1; //錯誤, 原因 byte 不能自動轉成 char
//
// //細節4: byte,short,char 他們三者可以計算,在計算時首先轉換為int型別 byte b2 = 1;
byte b3 = 2;
short s1 = 1;
//short s2 = b2 + s1;//錯, b2 + s1 => int
int s2 = b2 + s1;//對, b2 + s1 => int //byte b4 = b2 + b3; //錯誤: b2 + b3 => int
// //boolean 不參與轉換
boolean pass = true;
//int num100 = pass;// boolean 不參與型別的自動轉換 //自動提升原則: 表示式結果的型別自動提升為 運算元中最大的型別
//看一道題 byte b4 = 1;
short s3 = 100;
int num200 = 1;
float num300 = 1.1F; double num500 = b4 + s3 + num200 + num300; //float -> double
}
}
強制型別轉換
自動型別轉換的逆過程, 將容量大的資料型別轉換為容量小的資料型別。使用時要加上強制轉換符 ( ),但可能造成精度降低或溢位,格外要注意。
public class ForceConvert { //編寫一個main方法
public static void main(String[] args) { //演示強制型別轉換
int n1 = (int)1.9;
System.out.println("n1=" + n1);//1, 造成精度損失 int n2 = 2000;
byte b1 = (byte)n2;
System.out.println("b1=" + b1);//造成 資料溢位
}
}
public class ForceConvertDetail { //編寫一個main方法
public static void main(String[] args) { //演示強制型別轉換
//強轉符號只針對於最近的運算元有效,往往會使用小括號提升優先順序
//int x = (int)10*3.5+6*1.5;//編譯錯誤: double -> int
int x = (int)(10*3.5+6*1.5);// (int)44.0 -> 44
System.out.println(x);//44 char c1 = 100; //ok
int m = 100; //ok
//char c2 = m; //錯誤
char c3 = (char)m; //ok
System.out.println(c3);//100對應的字元, d字元
}
}
7. 基本資料型別和String型別的轉換
public class StringToBasic {
//編寫一個main方法
public static void main(String[] args) {
//基本資料型別->String
int n1 = 100;
float f1 = 1.1F;
double d1 = 4.5;
boolean b1 = true;
String s1 = n1 + "";
String s2 = f1 + "";
String s3 = d1 + "";
String s4 = b1 + "";
System.out.println(s1 + " " + s2 + " " + s3 + " " + s4);
//String->對應的基本資料型別
String s5 = "123";
//會在OOP 講物件和方法的時候回詳細
//解讀 使用 基本資料型別對應的包裝類,的相應方法,得到基本資料型別
int num1 = Integer.parseInt(s5);
double num2 = Double.parseDouble(s5);
float num3 = Float.parseFloat(s5);
long num4 = Long.parseLong(s5);
byte num5 = Byte.parseByte(s5);
boolean b = Boolean.parseBoolean("true");
short num6 = Short.parseShort(s5);
System.out.println("===================");
System.out.println(num1);//123
System.out.println(num2);//123.0
System.out.println(num3);//123.0
System.out.println(num4);//123
System.out.println(num5);//123
System.out.println(num6);//123
System.out.println(b);//true
//怎麼把字串轉成字元char -> 含義是指 把字串的第一個字元得到
//解讀 s5.charAt(0) 得到 s5字串的第一個字元 '1'
System.out.println(s5.charAt(0));
}
}
注意事項
在將 String 型別轉成 基本資料型別時, ,比如 我們可以把 "123" , 轉成一個整數,但是不能把 "hello" 轉成一個整數
如果格式不正確,就會 丟擲異常,程式就會終止, 這個問題在異常處理章節中,會處理
/**
* 演示字串轉基本資料型別的細節
*/
public class StringToBasicDetail { //編寫一個main方法
public static void main(String[] args) { String str = "hello";
//轉成int
int n1 = Integer.parseInt(str);
System.out.println(n1);
}
}
8. HomeWork
1.程式閱讀,判斷輸出結果Homework01
public class Homework01 {
//編寫一個main方法
public static void main(String[] args) {
int n1;
n1 = 13;
int n2;
n2 = 17;
int n3;
n3 = n1 + n2;
System.out.println("n3 = " + n3);//30
int n4 = 38;
int n5 = n4 - n3;
System.out.println("n5 = " + n5);//8
}
}
2.程式編寫 Homework02
public class Homework02 {
//編寫一個main方法
public static void main(String[] args) {
//程式設計,儲存兩本書名,用+拼接,看效果。儲存兩個性別,
//用加號拼接,看效果。儲存兩本書價格,用加號拼接,看效果
String book1 = "天龍八部";
String book2 = "笑傲江湖";
System.out.println(book1 + book2);//天龍八部笑傲江湖
//性別應該使用char儲存
char c1 = '男';
char c2 = '女';
System.out.println(c1 + c2);//得到 男 字元碼值 + 女 字元碼值
//儲存兩本書價格
double price1 = 123.56;
double price2 = 100.11;
System.out.println(price1 + price2);//就是 123.56+100.11
}
}
3.程式編寫Homework03
public class Homework03 {
//編寫一個main方法
public static void main(String[] args) {
/*
姓名 年齡 成績 性別 愛好
xx xx xx xx xx
要求:
1) 用變數將姓名、年齡、成績、性別、愛好儲存
2) 使用+
3) 新增適當的註釋
4) 新增轉義字元, 使用一條語句輸出
*/
//姓名
String name = "jack";
int age = 20;
double score = 80.9;
char gender = '男';
String hobby = "打籃球";
//輸出了資訊, 可以使用換行
System.out.println("姓名\t年齡\t成績\t性別\t愛好\n" + name + "\t"
+ age + "\t" + score + "\t" + gender + "\t" + hobby);
}
}