toString() วิธีการของ java.lang.Integer ส่งกลับวัตถุสตริง คลาส Integer มีสามวิธี toString() มาดูกันทีละตัว −
String toString()
ตัวอย่าง
java.lang.Integer.toString() วิธีการส่งกลับวัตถุสตริงที่แสดงค่าของจำนวนเต็มนี้ เรามาดูตัวอย่างกัน −
import java.lang.*;
public class Demo {
public static void main(String[] args) {
Integer i = new Integer(20);
// returns a string representation of the integer value in base 10
String retval = i.toString();
System.out.println("Value = " + retval);
}
} ผลลัพธ์
Value = 20
สตริงคงที่ toString(int i)
java.lang.Integer.toString(int i) วิธีการส่งกลับวัตถุสตริงที่แสดงถึงจำนวนเต็มที่ระบุ ในที่นี้ i คือจำนวนเต็มที่จะแปลง
ตัวอย่าง
เรามาดูตัวอย่างกัน −
import java.lang.*;
public class Demo {
public static void main(String[] args) {
Integer i = new Integer(10);
// returns a string representation of the specified integer in base 10
String retval = i.toString(30);
System.out.println("Value = " + retval);
}
} ผลลัพธ์
Value = 30
สตริงคงที่ toString(int i, int radix)
java.lang.Integer.toString(int i, int radix) วิธีการส่งกลับการแสดงสตริงของอาร์กิวเมนต์แรก i ในฐานที่ระบุโดย Radix อาร์กิวเมนต์ที่สอง หาก Radix น้อยกว่า Character.MIN_RADIX หรือใหญ่กว่า Character.MAX_RADIX จากนั้นจึงใช้ฐาน 10 แทน
ในที่นี้ i คือจำนวนเต็มที่จะแปลง ในขณะที่ radix คือฐานที่จะใช้ในการแสดงสตริง
ตัวอย่าง
เรามาดูตัวอย่างกัน −
import java.lang.*;
public class Demo {
public static void main(String[] args) {
Integer i = new Integer(10);
// returns a string representation of the specified integer with radix 10
String retval = i.toString(30, 10);
System.out.println("Value = " + retval);
// returns a string representation of the specified integer with radix 16
retval = i.toString(30, 16);
System.out.println("Value = " + retval);
// returns a string representation of the specified integer with radix 8
retval = i.toString(30, 8);
System.out.println("Value = " + retval);
}
} ผลลัพธ์
Value = 30 Value = 1e Value = 36