Java Long类toString()方法及示例

Syntax:

句法:

    public String toString();
    public static String toString(long value);
    public static String toString(long value, int radix's);

长类toString()方法 (Long class toString() method)

  • toString() method is available in java.lang package.

    toString()方法在java.lang包中可用。

  • toString() method is used to represent String denoted by this Long object.

    toString()方法用于表示由此Long对象表示的String。

  • toString(long value) method is used to represent String denoted by the given argument of long type.

    toString(long value)方法用于表示由long类型的给定参数表示的String。

  • toString(long value, int radix's) method is used to represent String of the given argument of long type in the radix's given by the second argument.

    toString(long value,int radix's)方法用于表示第二个参数给定的基数中long型给定参数的String。

  • These methods don't throw an exception at the time of String representation.

    这些方法在String表示时不会引发异常。

  • toString() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    toString()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • toString(long value) and toString(long value, int radix's) are the static methods, they are accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.

    toString(long value)toString(long value,int radix's)是静态方法,它们也可以使用类名进行访问,并且,如果尝试使用类对象访问这些方法,那么也不会出错。

Parameter(s):

参数:

  • In the first case toString(), we don't pass any parameter or value.

    在第一种情况下, toString() ,我们不传递任何参数或值。

  • In the second case toString(long value), we pass only one parameter of the long type it represents the long value to be converted.

    在第二种情况下toString(long value) ,我们仅传递long类型的一个参数,它表示要转换的long值。

  • In the third case toString(long value, int radix's), we pass two parameters of long type it represents the long value to be converted and the parameter radix's represents the radix to be used in String denotation.

    在第三种情况toString(long value,int radix's)中 ,我们传递了long类型的两个参数,它表示要转换的long值,而参数radix表示要在String表示法中使用的基数。

Return value:

返回值:

In the first case, the return type of this method is String - it returns the String representation of this Long object.

在第一种情况下,此方法的返回类型为String-它返回此Long对象的String表示形式。

In the second case, the return type of this method is String - it returns the String representation of the given argument is of long type.

在第二种情况下,此方法的返回类型为String-它返回给定参数的String表示形式为long类型。

In the third case, the return type of this method is String - it returns the String representation of the given argument is of long type in the given radix.

在第三种情况下,此方法的返回类型为String-它返回给定参数的String表示形式为给定基数中的long类型。

Example:

例:

// Java program to demonstrate the example 
// of toString () method of Long class

public class ToStringOfLongClass {
    public static void main(String[] args) {
        // Object initialization
        Long ob1 = new Long(100);
        Long ob2 = new Long(200);

        // Display ob1,ob2 values
        System.out.println("ob1: " + ob1);
        System.out.println("ob2: " + ob2);

        // It represents the string of this Long object
        String value1 = ob1.toString();

        // It represents the string of the given long parameter
        String value2 = Long.toString(ob2);

        // It represents the string of the given long parameter with radix 20
        String value3 = Long.toString(ob2, 20);

        // Display result values
        System.out.println("ob1.toString(): " + value1);
        System.out.println("Long.toString(ob2): " + value2);
        System.out.println("Long.toString(ob2,20): " + value3);
    }
}

Output

输出量

ob1: 100
ob2: 200
ob1.toString(): 100
Long.toString(ob2): 200
Long.toString(ob2,20): a0


翻译自: https://www.includehelp.com/java/long-class-tostring-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值