- java.lang.Integer.toString()是Java中的內置方法,用於返回表示此Integer值的String對象。
用法:
public static String toString()
參數:該方法不接受任何參數。
返回值:該方法返回特定Integer值的字符串對象。
以下示例程序旨在說明Java.lang.Integer.toString()方法:
// Java program to illustrate the // toString() Method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer obj = new Integer(8); //It will return a string representation String stringvalue1 = obj.toString(); System.out.println("String Value= " + stringvalue1); Integer obj3 = new Integer(10); //It will return a string representation String stringvalue3 = obj3.toString(); System.out.println("String Value = " + stringvalue3); } }
輸出:String Value= 8 String Value = 10
- java.lang.Integer.toString(int a)是Java中的內置方法,用於返回String對象,該String對象表示參數中指定的整數。
用法:public static String toString(int a)
參數:該方法接受一個整數類型的參數a,表示需要轉換為字符串的整數。
返回值:該方法以特定基數返回參數的字符串表示形式。
例子:
For base 8: Input: int a = 75 Output: "75" For base 10: Input: int a = -787 Output: "-787"
以下示例程序旨在說明Java.lang.Integer.toString(int a)方法:
示例1:// Java program to illustrate the // toString(int a) method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer obj = new Integer(8); // It will return a string representation // in base 8 String stringvalue1 = obj.toString(75); System.out.println("String Value = " + stringvalue1); Integer obj2 = new Integer(8); // It will return a string representation // in base 2 String stringvalue2 = obj2.toString(6787); System.out.println("String Value = " + stringvalue2); Integer obj3 = new Integer(10); // It will return a string representation // in base 10 String stringvalue3 = obj3.toString(-787); System.out.println("String Value = " + stringvalue3); } }
輸出:String Value = 75 String Value = 6787 String Value = -787
示例2:用於十進製和字符串參數。
注意:這會導致錯誤,並且還會導致缺少合適的Integer構造函數。// Java program to illustrate the // Java.lang.Integer.toString(int a)method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer obj = new Integer(8); String stringvalue1 = obj.toString(58.5); System.out.println("String Value = " + stringvalue1); Integer obj2 = new Integer(8); String stringvalue2 = obj2.toString("317"); System.out.println("String Value = " + stringvalue2); // Empty constructor will result in an error Integer obj3 = new Integer(); String stringvalue3 = obj3.toString(-787); System.out.println("String Value = " + stringvalue3); } }
輸出:
prog.java:8: error: incompatible types: possible lossy conversion from double to int String stringvalue1 = obj.toString(58.5); ^ prog.java:12: error: incompatible types: String cannot be converted to int String stringvalue2 = obj2.toString("317"); ^ prog.java:17: error: no suitable constructor found for Integer(no arguments) Integer obj3 = new Integer(); ^ constructor Integer.Integer(int) is not applicable (actual and formal argument lists differ in length) constructor Integer.Integer(String) is not applicable (actual and formal argument lists differ in length) Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 3 errors
- java.lang.Integer.toString(int a,int base)是Java中的一個內置方法,用於返回第二個參數base指定的base中參數a的字符串表示形式。如果基數/基數小於Character.MIN_RADIX或大於Character.MAX_RADIX,則使用基數10。用作數字的ASCII字符:0到9和a到z。
用法:public static String toString(int a, int base)
參數:該方法接受兩個參數:
- a:這是整數類型,是指要轉換的整數值。
- base:這也是整數類型,是表示字符串時要使用的基數。
返回值:該方法以指定的基數返回指定參數的字符串表示形式。
例子:
Input: a = 71, base = 2 Output: 1000111 Input: a = 314, base = 16 Output: 13a
以下示例程序旨在說明Java.lang.Integer.toString(int a,int base)方法:
示例1:// Java program to illustrate the // toString(int, int) Method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer a = new Integer(10); // It returns a string representation // in base 2 String returnvalue = a.toString(5254, 2); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 8 returnvalue = a.toString(35, 8); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 16 returnvalue = a.toString(47, 16); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 10 returnvalue = a.toString(451, 10); System.out.println("String Value = " + returnvalue); } }
輸出:String Value = 1010010000110 String Value = 43 String Value = 2f String Value = 451
示例2:
// Java program to illustrate the // toString(int, int) Method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer a = new Integer(10); // It returns a string representation // in base 2 String returnvalue = a.toString(-525, 2); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 8 returnvalue = a.toString(31, 8); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 16 returnvalue = a.toString(28, 16); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 10 returnvalue = a.toString(29, 10); System.out.println("String Value = " + returnvalue); } }
輸出:String Value = -1000001101 String Value = 37 String Value = 1c String Value = 29
相關用法
- Java Overriding toString()用法及代碼示例
- Java LinkedBlockingQueue toString()用法及代碼示例
- Java CopyOnWriteArrayList toString()用法及代碼示例
- Java BigInteger toString()用法及代碼示例
- Java StringJoiner toString()用法及代碼示例
- Java TreeSet toString()用法及代碼示例
- Java MathContext toString()用法及代碼示例
- Java DecimalStyle toString()用法及代碼示例
- Java AbstractSequentialList toString()用法及代碼示例
- Java LinkedHashSet toString()用法及代碼示例
- Java Vector toString()用法及代碼示例
- Java HashSet toString()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 Integer toString() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。