📜  Java中的整数 toString()

📅  最后修改于: 2022-05-13 01:54:58.957000             🧑  作者: Mango

Java中的整数 toString()

  1. Java.lang.Integer.toString()是Java中的一个内置方法,用于返回表示该整数值的String 对象。

    句法 :

    public static String toString()

    参数:该方法不接受任何参数。

    返回值:该方法返回特定整数值的字符串对象。

    下面的程序说明了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
    
  2. Java.lang.Integer.toString( int a )是Java中的一个内置方法,用于返回一个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) 方法:
    方案一:

    // 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
  3. Java.lang.Integer.toString( int a, int base )是Java中的一个内置方法,用于返回由第二个参数指定的base中参数a的字符串表示形式。如果基数/基数小于字符.MIN_RADIX 或大于字符.MAX_RADIX,则使用基数 10。用作数字的 ASCII字符:0 到 9 和 a 到 z。
    句法:
    public static String toString(int a, int base)

    参数:该方法接受两个参数:

    • a :这是整数类型,指的是要转换的整数值。
    • 基数:这也是整数类型,指的是在表示字符串时要使用的基数。

    返回值:该方法返回指定基数中指定参数的字符串表示形式。

    例子:

    Input: a = 71, base = 2
    Output: 1000111
    
    Input: a = 314, base = 16
    Output: 13a
    

    下面的程序说明了Java.lang.Integer.toString(int a, int base) 方法:
    方案一:

    // 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
    

    方案二:

    // 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