Java Integer类toString()方法与示例
Syntax:
句法:
public String toString();public static String toString(int value);public static String toString(int value, int radix's);整數類toString()方法 (Integer class toString() method)
toString() method is available in java.lang package.
toString()方法在java.lang包中可用。
toString() method is used to represent String denoted by this Integer object.
toString()方法用于表示由此Integer對象表示的String。
toString(int value) method is used to represent String denoted by the given argument of int type.
toString(int value)方法用于表示由int類型的給定參數表示的String。
toString(int value, int radix's) method is used to represent String of the given argument of int type in the radix's given by the second argument.
toString(int value,int radix's)方法用于表示第二個參數給定的基數中int類型的給定參數的String。
These methods don't throw an exception at the time of String representation.
這些方法在String表示時不會引發異常。
These are non-static methods, they are accessible with the class object only and if we try to access the method with the class name then we will get an error.
這些是非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
Parameter(s):
參數:
In the first case toString(), we don't pass any parameter or value.
在第一種情況下, toString() ,我們不傳遞任何參數或值。
In the second case toString(int value), we pass only one parameter of the int type it represents the int value to be converted.
在第二種情況下, toString(int value) ,我們僅傳遞一個int類型的參數,它表示要轉換的int值。
In the third case toString(int value, int radix's), we pass two parameters of int type it represents the int value to be converted and the parameter radix's represents the radix to be used in String denotation.
在第三種情況toString(int value,int radix's)中 ,我們傳遞了兩個int類型的參數,它表示要轉換的int值,而參數radix's表示要在String表示法中使用的基數。
Return value:
返回值:
In the first case, the return type of this method is String - it returns the String representation of this Integer object.
在第一種情況下,此方法的返回類型為String-它返回此Integer對象的String表示形式。
In the second case, the return type of this method is String - it returns the String representation of the given argument is of int type.
在第二種情況下,此方法的返回類型為String-它返回給定參數的String表示形式為int類型。
In the third case, the return type of this method is String - it returns the String representation of the given argument is of int type in the given radix.
在第三種情況下,此方法的返回類型為String-它返回給定參數的String表示形式為給定基數的int類型。
Example:
例:
// Java program to demonstrate the example // of toString () method of Integer classpublic class ToStringOfIntegerClass {public static void main(String[] args) {// Object initializationInteger ob1 = new Integer(100);Integer ob2 = new Integer(200);// Display ob1,ob2 valuesSystem.out.println("ob1:" + ob1);System.out.println("ob2:" + ob2);// It represents the string of this Integer objectString value1 = ob1.toString();// It represents the string of the given integer parameterString value2 = Integer.toString(ob2);// It represents the string of the given integer parameter with radix 20String value3 = Integer.toString(ob2, 20);// Display result valuesSystem.out.println("ob1.toString(): " + value1);System.out.println("Integer.toString(ob2): " + value2);System.out.println("Integer.toString(ob2,20): " + value3);} }Output
輸出量
ob1:100 ob2:200 ob1.toString(): 100 Integer.toString(ob2): 200 Integer.toString(ob2,20): a0翻譯自: https://www.includehelp.com/java/integer-class-tostring-method-with-example.aspx
總結
以上是生活随笔為你收集整理的Java Integer类toString()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: synchronized 加锁 this
- 下一篇: java timezone_Java T