2015年6月23日 星期二

5 資料型別轉換

5     資料型別轉換
資料型別轉換(Type Conversion)
變數由一種資料型態轉為另一種資料型態,是謂資料型別轉換(資料轉型)。有一定長度之基本資料型別可作資料轉型,無確定長度格式之參考資料型別則不可進行資料轉型。

【自動轉型】
是指較小範圍資料型態之變數可自動轉換成較大範圍之資料型態,亦謂放大轉型(Widening Conversion),或稱隱性轉型(Implicit Casting)
char之變數不得與shortbyte相互作自動轉型,boolean之變數不得作轉型。自動轉型於指定運算( = )時執行。

byte a = 5;
int b = 11;
b=a;   //自動轉型
byte變數a自動轉型成int變數型態。

下表(5-1)紅字部分(->)可自動轉型:




 【強迫轉型】
是指較大範圍資料型態之變數需經過強迫手段,才可轉換成較小範圍之資料型態,亦謂縮小轉型(Narrowing Conversion),或稱顯性轉型(Explicit Casting)。強迫轉型以指定轉型後新資料型態為參數強迫進行。

byte a=5;
int b=3;
a = (byte)b;

int變數b強迫轉型成byte變數型態,往往會遺失部分資料。

int a = 128;
byte b;
b = (byte)a;

int a = 128;
0000 0000 0000 0000 0000 0000 1000 0000
(27 = 128,最左端位元為0是正值)
byte b = (byte)a;
......................................................1000 0000
(-27 = -128,最左端位元為1是負值)

由較小範圍之資料型態「強迫」轉型為較大範圍之資料型態,雖然不會有錯誤,但是並沒有意義。

byte a=5;
int b=3;
b = (int)a;




[資料型別轉換例]
[自動轉型例]
1
class Conv1 {
public static void main (String[] args) {
    byte bt1;
    short sh1;
    int in1;
    long lg1;
    float ft1;
    double db1;

    bt1 = 97;
    sh1 = bt1;
    System.out.println("byte自動轉換為short"+bt1+"-->"+sh1);
    in1 = sh1;
    System.out.println("short自動轉換為int"+sh1+"-->"+in1);
    lg1 = in1;
    System.out.println("int自動轉換為long"+in1+"-->"+lg1);
    ft1 = lg1;
    System.out.println("long自動轉換為float"+lg1+"-->"+ft1);
    db1 = ft1;
    System.out.println("float自動轉換為double"+ft1+"-->"+db1);
}
}
命令提示字元下輸入C:\js>java Conv1
執行結果:
byte自動轉換為short97-->97
short自動轉換為int97-->97
int自動轉換為long97-->97
long自動轉換為float97-->97.0
float自動轉換為double97.0-->97.0




2
class Cast2 {
  public static void main(String[] args) {
    char ch11 = 'A';
    char ch22;
    byte by11 = 66;
    byte by22;
    short sh11 = 300;
    short sh22;
    ch22 = by11;  //ng
    ch22 = sh11;  //ng
    by22 = ch11;  //ng
    by22 = sh11;  //ng
    sh22 = ch11;  //ng
    sh22 = by11;  //ok
  }
}
命令提示字元下輸入C:\js>javac Cast2.java
編譯結果:上例ng部分會編譯錯誤
C:\js>javac Cast2.java
Cast2.java:9: error: incompatible types: possible lossy conversion from byte to
char ch22 = by11;  //ng
           ^
Cast2.java:10: error: incompatible types: possible lossy conversion from short t
o char ch22 = sh11;  //ng
           ^
Cast2.java:11: error: incompatible types: possible lossy conversion from char to
 byte by22 = ch11;  //ng
           ^
Cast2.java:12: error: incompatible types: possible lossy conversion from short t
o byte by22 = sh11;  //ng
           ^
Cast2.java:13: error: incompatible types: possible lossy conversion from char to
 short sh22 = ch11;  //ng
           ^
5 errors



3
class Cast3{
  public static void main(String[] args) {
    char ch12 = 'A';
    int in12;
    in12 = ch12;
    System.out.println("in12="+in12);
  }
}
命令提示字元下輸入C:\js>java Cast3
執行結果:in12=65

4
class 轉型4 {
  public static void main(String[] args) {
    char 字元15 = 'C';
    int 整數15;
    整數15 = 字元15;
    System.out.println("整數15="+整數15);
  } }
命令提示字元下輸入C:\js>java 轉型4
執行結果:整數15=67

5
class Cast5 {
  public static void main(String[] args) {
    int in11;
    long lng11 =300;
    in11 = lng11;  //較大範圍資料型態自動轉型成較小範圍之資料型態時,
  } }            //會編譯錯誤
命令提示字元下輸入C:\js>javac Cast5.java
編譯結果:
Cast5.java:5: error: incompatible types: possible lossy conversion from long to
int
    in11 = lng11;  //較大範圍資料型態自動轉型成較小範圍之資料型態時,
           ^
1 error
[強迫轉型例]
例:(double-->float-->long-->int-->short-->byte)
class Castf {
public static void main (String[] args) {
    byte btf;
    short shf;
    int inf;
    long lgf;
    float ftf;
    double dbf;

    dbf = 97.0;
    ftf = (float)dbf;
    System.out.println("double強迫轉型float"+ftf+"<--"+dbf);
    lgf = (long)ftf;
    System.out.println("float強迫轉型long"+lgf+"<--"+ftf);
    inf = (int)lgf;
    System.out.println("long強迫轉型int"+inf+"<--"+lgf);
    shf = (short)inf;
    System.out.println("int強迫轉型short"+shf+"<--"+inf);
    btf = (byte)shf;
    System.out.println("short強迫轉型bt"+btf+"<--"+shf);
}
}
命令提示字元下輸入C:\js>java Castf
執行結果:
double強迫轉型float97.0<--97.0
float強迫轉型long97<--97.0
long強迫轉型int97<--97
int強迫轉型short97<--97
short強迫轉型bt97<--97
[二進位(0~12進位)]
   27           26       25       24       23       22       21       20
0
0
0
0
0
0
0
0
0 * 20 + 0 * 21 + 0 * 22 + 0 * 23 + 0 * 24 + 0 * 25 + 0 * 26 + 0 * 27 = 0
0/2=0.....0  (20)
                                                

   27           26       25       24       23       22       21       20
0
1
1
0
0
0
0
1
1 * 20 + 0 * 21 + 0 * 22 + 0 * 23 + 0 * 24 + 1 * 25 + 1 * 26 + 0 * 27 = 97
97 / 2 = 48.....1  (20)
48 / 2 = 24.....0  (21)
24 / 2 = 12.....0  (22)
12 / 2 = 6.....0   (23)
6 / 2 = 3.....0    (24)
3 / 2 = 1.....1    (25)
1 / 2 = 0.....1    (26)  //計算至商為0

[八進位(0~78進位)]
                                      83       82      81        80




0
4
7
2
2 x 80 + 7 x 81 + 4 x 82 + 0 x 83 = 314
314 / 8 = 39.....2  (80)
39 / 8 = 4.....7    (81)
4 / 8 = 0.....4     (82)

[十六進位(0~F16進位)]
                                     163      162      161      160




0
1
2
A
10 x 160 + 2 x 161 + 1 x 162 + 0 x 163 = 298
                   298 / 16 = 18.....10  (160)
                   18 / 16 = 1.....2     (161)
                   1 / 16 = 0.....1      (162)
                  
0~910=A 11=B12=C13=D14=E15=F                   



沒有留言:

張貼留言