2. คำนวณ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 2-19  การใช้ byte, short, long, float มีเรื่องจุกจิก
public class NumericDataTypes {
  public static void main(String[] args) {
    byte b = 3;
    short h = 7;
    int i = 100;
    long g = 1234567891234L;  // ค่ามากแบบ long ต้องปิดท้ายด้วยตัวแอล 
    float f = 1.5F;           // ค่าแบบ float ต้องปิดท้ายด้วยตัวเอฟ 
    double d = 2.1;
    b = (byte)(b + i);  // byte+int เป็น int เก็บใส่ byte ต้องเปลี่ยน 
    h = (short)(h + i); // short+int เป็น int เก็บใส่ short ต้องเปลี่ยน 
    g = g + 1;          // long+int เป็น long เก็บไส่ long ได้เลย  
    f = (float)(f + d); // float+double เป็น double เก็บใส่ float ต้องเปลี่ยน 
  }
}
©2009 S.Prasitjutrakul