在宣告變數的資料型態其儲存範圍比指定的數值之資料型態為大時,
則會發生數值型態晉升的動作
簡單的說,就是較小的資料型態會自動提昇為符合變數之資料型態
例:
long money = 10;
二、轉型轉型(Casting)
如果確定要將資料型態大的值指定給資料型態小的變數,
則您必須明確進行轉型(Casting)的動作
使用括號()包括目標資料型態的關鍵字
例:
long money = 10;
int smallMoney = (int) money;
其它文章
本站Java鬥陣仔甘仔店為學徒程式開發教學學習,這裡有Java程式開發相關技術等學習交流。 主要提高開發程式效率同時分享一些開發程式經驗、深入探討開發程式體驗等技術和教學。 相關技術等學習交流語言(Languages)及框架工具(Software)/環境(Environment)/資料庫(Database)
public class HelloWorld{//類別區塊開始 public static void main(String[ ] args){//方法區塊開始 //程式區塊 System.out.println("我第一支Java程式!!"); }//方法區塊結束 }//類別區塊結束
public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } }
public class TestDemo { public TestDemo() {//Constructor //這是基本Constructor } }
public class TestDemo { }
public static void main(String[] args) { TestDemo testDemo = new TestDemo(); }
package test; public class TestConstructorDemo { int age; String name; private TestConstructorDemo() {//Constructor System.out.println("創建TestConstructorDemo"); } public TestConstructorDemo(int age) {//Constructor this.age = age; System.out.println("創建TestConstructorDemo,初值建立 ,age : "+ age); } public TestConstructorDemo(String name) {//Constructor this.name = name; System.out.println("創建TestConstructorDemo,初值建立 ,name : "+ name); } public TestConstructorDemo(int age,String name) {//Constructor this.age = age; this.name = name; System.out.println("創建TestConstructorDemo,初值建立, age : "+ age+", name : "+ name); } public void msg(){ System.out.println("age : " + age + ", name : " + name); } public static void main(String[] args) { System.out.println("創建物件測試1 開始:"); TestConstructorDemo testDemo1 = new TestConstructorDemo(); testDemo1.msg(); System.out.println("創建物件測試1 結束----------------"); System.out.println("創建物件測試2 開始:"); TestConstructorDemo testDemo2 = new TestConstructorDemo(25); testDemo2.msg(); System.out.println("創建物件測試2 結束----------------"); System.out.println("創建物件測試3 開始:"); TestConstructorDemo testDemo3 = new TestConstructorDemo("levin"); testDemo3.msg(); System.out.println("創建物件測試3 結束----------------"); System.out.println("創建物件測試4 開始:"); TestConstructorDemo testDemo4 = new TestConstructorDemo(25,"levin"); testDemo4.msg(); System.out.println("創建物件測試4 結束----------------"); } }
package com.test; public class TestSpilt1 { public static void main(String[] args) { String strData = "a|b|c|d"; String[] strArr = strData.split("|"); for(String str: strArr){ System.out.println(str); } } }
package com.test; public class TestSpilt1_1 { public static void main(String[] args) { String strData = "a|b|c|d"; String[] strArr = strData.split("\\|"); for(String str: strArr){ System.out.println(str); } } }
package com.test; public class TestSpilt1_2 { public static void main(String[] args) { String strData = "a|b|c|d"; String[] strArr = strData.split("[|]"); for(String str: strArr){ System.out.println(str); } } }
package com.test; public class TestSpilt2 { public static void main(String[] args) { String strData = "a.b.c.d"; String[] strArr = strData.split("."); for(String str: strArr){ System.out.println(str); } } }
package com.test; public class TestSpilt2_1 { public static void main(String[] args) { String strData = "a.b.c.d"; String[] strArr = strData.split("\\."); for(String str: strArr){ System.out.println(str); } } }
package com.test; public class TestSpilt2_2 { public static void main(String[] args) { String strData = "a.b.c.d"; String[] strArr = strData.split("[.]"); for(String str: strArr){ System.out.println(str); } } }
"/var/tmp"
private File tempFile;
public void createFile(InputStream is) throws IOException {
tempFile = File.createTempFile("tempFile", ".scv");
OutputStream os = new FileOutputStream(tempFile);
int n;
byte[] buffer = new byte[1024];
while ((n = is.read(buffer)) > -1) {
os.write(buffer, 0, n); // Don't allow any extra bytes to creep in,
// final write
}
os.close();
}
其它文章
跳脫字元
|
說明
|
\b
|
倒退字元(backspace)
|
\t
|
水平定位(tab)
|
\n
|
換行
|
\f
|
換頁 (form feed)
|
\r
|
歸位(return)
|
\’
|
單引號
|
\”
|
雙引號
|
\\
|
倒鈄線
|
\uhhhh
|
十六進制 hhhh 字元碼(h 是 0~7 的數字與 A~F 的英文字母)
所表示的文字碼(Unicode)
System.out.println("\u0041"); A
|
\ooo
|
八進制 ooo 字元碼(o 是 0~7 的數字)
|
// if() score = 70; if (score == 70) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score:" + score); } else {// 條件不成立 進入此程式區塊 System.out.println("不條件成立 score:" + score); } //判斷式?成立程式區塊:不成立程式區塊 score = 70; System.out.println(score == 70 ? "條件成立 score:" + score : "不條件成立 score:" + score);
// if() {} Test 1 -- 條件成立 int score = 80; if (score >= 70) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score >= 70:" + score); } // if() {} Test 2 -- 條件不成立 score = 50; if (score >= 70) {// 不成立 條件 不進入此程式區塊 System.out.println("條件成立 score >= 70:" + score); } // 不成立 條件 會跳過上述程式區塊 // if() {} else {} Test 1 -- 條件成立 score = 60; if (score >= 60) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score >= 60:" + score); } else {// 條件不成立 進入此程式區塊 System.out.println("不條件成立 score:" + score); } // if() {} else {} Test 2 -- 條件不成立 score = 59; if (score >= 60) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score >= 60:" + score); } else {// 條件不成立 進入此程式區塊 System.out.println("不條件成立 score:" + score); } // if() {} else if() {} else {} Test score = 59; if (score >= 60) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score >= 60:" + score); } else if (score == 59) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score == 59:" + score); } else { // 條件都不成立 進入此程式區塊 System.out.println("不條件成立 score:" + score); } // if() {} else if() {} else {} 可以很多個else if() Test score = 70; if (score >= 90) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score >= 90:" + score); } else if (score >= 80) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score >= 80:" + score); } else if (score >= 70) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score >= 70:" + score); } else if (score >= 60) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score >= 60:" + score); } else if (score >= 50) {// 條件成立 進入此程式區塊 System.out.println("條件成立 score >= 50:" + score); } else { // 所有條件都不成立 進入此程式區塊 System.out.println("不條件成立 score:" + score); }
優先值
|
運算子
|
同等順序
|
1
|
括號:()、[]
|
由右至左
|
2
|
遞增++、遞減--、負號-、NOT!、補數~
|
由左至右
|
3
|
乘*、除/、取餘數%
|
由左至右
|
4
|
加+、減-
|
由左至右
|
5
|
位元左移<<、位元右移>>、
無正負性位元右移>>>
|
由左至右
|
6
|
小於<、大於>、小於等於<=、大於等於>=
|
由左至右
|
7
|
等於==、不等於!=
|
由左至右
|
8
|
AND:
&
|
由左至右
|
9
|
XOR:^
|
由左至右
|
10
|
OR:|
|
由左至右
|
11
|
簡化比較次數的AND:&&
|
由左至右
|
12
|
簡化比較次數的OR:||
|
由左至右
|
13
|
條件選擇?:
|
由右至左
|
14
|
指定運算
=
|
由右至左
|
15
|
+=、-=、*=、/=、%=、&=、|=、^=
|
由右至左
|
優先值:1代表最高優先值 ,15代表最低優先值 。
同等順序:指運算式中遇到同等級優先值時的運算處理。
|
指定運算子
|
用途
|
例子:A=5
|
相等式子
|
運算結果
|
註解
|
+=
|
加
|
A+=3+2
|
A=A+(3+2)
|
A=10
|
先運算+=右邊的式子,再和A相加
|
-=
|
減
|
A-=5-4
|
A=A-(5-4)
|
A=4
|
先運算-=右邊的式子,再和A相減
|
*=
|
乘
|
A*=2*3
|
A=A*(2*3)
|
A=30
|
先運算*=右邊的式子,再和A相乘
|
/=
|
除
|
A/=10/5+3
|
A=A/(10/5+3)
|
A=1
|
先運算/=右邊的式子,再和A相除
|
%=
|
求餘數
|
A%=15%4
|
A=A%(15%4)
|
A=2
|
先運算%=右邊的式子,再和A求餘數
|
指定運算子
|
用途
|
例子:A=5
|
相等式子
|
運算結果
|
註解
|
&=
|
AND
|
A
&= 5-3
|
A
=A&(5-3)
|
A=0
|
先運算&=右邊的式子,再和A作AND運算
|
|=
|
OR
|
A
|= 2
|
A=A|2
|
A=7
|
先運算|=右邊的式子,再和A作OR運算
|
^=
|
XOR
|
A^=2+1
|
A=A^(2+1)
|
A=6
|
先運算^=右邊的式子,再和A作XOR運算
|
撰寫一個JAVA程式 public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } } ...