WeGievU.Com
WeGievU.Com
WeGievU.Com
User :
Pass :
  WeGievU.Com
WeGievU.Com
WeGievU.Com
» E-Learning Room > Java
WeGievU.Com
การแปลงชนิดของข้อมูล 
( 14  Views )

Mon 01-09-2551 Time 12:30
Category: Java/ WeGievU.Com 0Comments
WeGievU.Com
    การแปลงข้อมูลนั้นสำคัญเหมือนกันนะครับ ถ้าเราเจอโจทย์ที่ยาวๆแล้วจะนำมาให้ด้วยกันได้นั้นจะต้องทำการแปลงข้อมูลให้เป็นชนิดเดียวกันก่อน โดยการแปลงข้อมูลนั้น แบ่งเป็น 2 ประเภท
1. Implicit type conversion เป็นการแปลงข้อมูลที่ภาษาจาวาทำแบบอัตโนมัติตามความเหมาะสม
2. Explicit type conversion (หรือ casting) เป็นการแปลงข้อมูลโดยที่ผู้เขียนโปรแกรมเป็นคนกำหนดเอง
รูปแบบ  (ชนิดข้อมูลที่ต้องการ)  นิพจน์ที่ต้องการแปลงชนิดข้อมูล
 
ชนิดข้อมูล นัยสำคัญ
double 6
float 5
long 4
int 3
short 2
byte 1
 
class TestSignificant
    {
      public static void main(String[] args)
     {
//ประกาศตัวแปร//
      float a = 1.0F , b = 2.5F ;
      int c;
      double d;
//ทำการคำนวณ a+b จะได้ 3.5 และเป็นชนิด float (3.5F) แต่ d ได้ถูกให้แปลงเป็น double ดังนั้นชนิดของ       d จึงเป็น double//
      d = a + b;
//แสดงผล//
      System.out.println("D= "+d);
//ทำการคำนวณa+b จะได้ 3.5 และเป็นชนิด float (3.5F) แต่ c ได้ถูกให้แปลงเป็น int ดังนั้นชนิดของ c จึง      เป็น int จะได้ค่า = 3 //
      c = (int) (a + b);
//แต่วิธีนี้ไม่ดีเท่าไหร่เพราะ float มีค่านัยสำคัญสูงกว่า int จะทำให้เสียค่าข้อมูลที่แท้จริงไป ทั้ที่จริงต้องได้ 3.5 แต่แปลงแล้วกลับเหลือ 3//
//แสดงผล//
      System.out.println("C= "+c);
      }
}
ผลการ Run

 
ปัญหาต่างๆที่อาจเกิด
1. byte num1 = 160;
     จากข้อความด้านบนนี้นั้น byte มีค่าสูงสุด 127 เท่านั้น
     วิธีแก้ไข
        byte num1 = (byte) 160;  จากการแปลงข้อมูลเบื้องต้นนี้จะทำให้สูญเสียค่าที่แท้จริงจาก 160 จะเป็น -96
 
2. short a=1;
     short num2 = a + 5;
      จากปัญหาข้อนี้นั้นเป็นการที่ให้ชนิดตัวแปรของ a เป็น short แต่เมื่อเข้าสู่กระบวนการทางคณิตศาสตร์แล้วจะทำให้ short  เป็น int [เพราะ int มีนัยสำคัญสูกกว่า short ] แล้วคำตอบที่ได้ก็จะเป็น int ด้วย และจะทำให้
     วิธีแก้ไข
        short num2 = (short ) a + 5;   หรือ  int num2 = a + 5;
 
3. int b = 3;
    int num3 = (float) b / 3;
     เนื่องจาก (float) b / 3 จะได้ผลลัพธ์ 1.0 ซึ่งเป็นชนิดข้อมูล float เพื่อไม่ให้เกิดความผิดพลาดจะต้องประกาศ num3 ให้เป็น float  เป็นอย่างน้อย หรือไม่ก็เป็น double
     วิธีแก้ไข
        float num3 = (float) b / 3;     b จะ กลายเป็น 3.0 เพราะได้แปลงชนิดข้อมูลเป็น float  แล้วก็ไป / 3 แต่ยังหารได้เพราะยังเป็น int ฉะนั้นก็จะเปลี่ยนเป็น float  จะได้ 3.0 / 3.0 จะได้ผลลัพธ์ 1.0
 
4 . short xx = 1 , yy = 2 , zz;
      zz = xx + yy;
       เนื่องจาก 1 , 2 เป็น int แล้วผลลัพธ์จากโจทย์ด้านบน zz เป็น stort ดังนั้นจึงทำให้ไม่สามารถเก็บค่าได้
     วิธีแก้ไข
       zz = (short) (xx + yy);   หรือ   short xx=1 , yy=2;  int  zz = xx + yy;
       
5. float yyy = 3.0;
     ชนิดข้อมูลโดยปริยายของข้อมูลแบบ floating-point 8nv double ดังนั้น ค่า 3.0 จึงเป็นชนิด double ไม่ใช่ float ซึ่ง Java ไม่อนุญาตให้นำค่า double ไปเก็บไว้ที่ float เนื่องจาก float มีนัยสำคัญต่ำกว่า double
 
เดี่ยวจะ งง กับเข้าไปใหญ่มาดู ตัวอย่าง โปรแกรมเลยดีกว่า
 
class TestCasting
    {
       public static void main(String[] args)
          {
//ข้อที่ 1byte num1 = 160;//
          byte num1 = (byte) 160;
          System.out.println("num1= "+num1);
//ข้อที่ 2 short a=1; short num2 = a + 5;//
          short a = 1; int num2 = a + 5;
          System.out.println("num2= "+num2);
//ข้อที่ 3 int b = 3; int num3 = (float) b / 3;//
          int b = 3; float num3 = (float) b / 3;
          System.out.println("num3= "+num3);
//ข้อที่ 4 int x; long y; y = 10L; x=y;//
          int x; long y; x = 20; y = x;
          System.out.println("x= "+x+" y= "+y);
//ข้อที่ 4 short xx = 1 , yy = 2 , zz; zz = xx + yy;//
          short xx = 1, yy = 2, zz; zz = (short) (xx + yy);
          System.out.println("zz= "+zz);
//ข้อที่ 5 float yyy = 3.0;//
          double xxx = 2.5F;
          System.out.println("xxx= "+xxx);
          }
}
ผล Run

 
อ่าว งง กันใหญ่ เหอะๆ อย่าเพิ่งเครียดครับ เอาไปดูอีก ตัวอย่างก็แล้วกันนะครับ
 
class TestCasting2
    {
     public static void main(String[] args)
   {
      int a = 1,b = 2 ,result_int = 0;
      long c = 3L, d = 4L, result_long = 0L;
      float e = 5.0F, f = 6.0F, result_float = 0.0F;
      double g = 7.0, h = 8.0, result_double = 0.0;

      result_long = (long) g;
      System.out.println("(long)g= "+result_long);
      result_float = (float) (g * h);
      System.out.println("(float) (g * h)= "+result_float);
      result_double = f + f * e;
      System.out.println("f + f * e= "+result_double);
      result_int = (int) (a + b + c + d + e + f + g + h);
      System.out.println("(int) (a + b + c + d + e + f + g + h)= "+result_int);
      result_float = d / b * e % c + (float) g-b;
      System.out.println("d / b * e % c + (float) g-b= "+result_float);
      }
}
ผล Run

 

แสดงความคิดเห็น “ การแปลงชนิดของข้อมูล   Comment”
WeGievU.Com
WeGievU.Com


Learning
| Tip | Knowledge
Copyright (c) 2010 by Wegiveu.com Team ( We Give Everything )