Short ในภาษา Java
Short ในภาษา Java นั้นเป็นการเก็บข้อมูลชนิดตัวเลข ซึ่งมีค่าอยู่ระหว่าง –32,768 ถึง 32,767 ซึ่งในการหาค่าต่ำสุดและค่าสูงสุดของ Short นั้นหาได้จากตัวอย่างโค้ด
package info.doesystem.howto;
public class DoesystemInfo {
public static void main(String[] args) {
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
}
}
Cast to short
ในการบวกกันของ short นั้น ผลลัพธ์จะออกมาเป็น int ดังนั้นถ้าเราต้องการที่จะได้ short เราจะต้อง cast กลับมาเป็น short ดังตัวอย่างโค้ดด้านล่าง
package info.doesystem.howto;
public class DoesystemInfo {
public static void main(String[] args) {
short a, b, c;
c = 2;
b = 9;
a = (short) (b + c);
System.out.println("a is " + a);
}
}
Convert String to Short
ในการแปลง String ไปเป็น Short นั้นเราสามารถทำได้หลายวิธี ดังตัวอย่างโค้ด
package info.doesystem.howto;
public class DoesystemInfo {
public static void main(String[] args) {
Short sObj1 = new Short("100");
System.out.println(sObj1);
String str = "100";
Short sObj2 = Short.valueOf(str);
System.out.println(sObj2);
Short sObj3 = new Short(str);
System.out.println(sObj3);
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น