字符串tostring_Java对象类字符串toString()方法(带示例)

字符串tostring

对象类字符串toString() (Object Class String toString())

  • This method is available in java.lang.Object.toString().

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

  • This method is used to return the string representation of the object.

    此方法用于返回对象的字符串表示形式。

  • This method returns a string of the object that is easy to understand for the end user.

    此方法返回最终用户易于理解的对象字符串。

  • This method does not return an exception.

    此方法不返回异常。

Syntax:

句法:

    String toString(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the Object class.

我们没有在Object类的方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is String that means this method returns String representation of the object.

该方法的返回类型为String ,这意味着该方法返回对象的String表示形式。

Java程序演示对象类toString()方法的示例 (Java program to demonstrate example of Object Class toString() method)

import java.lang.Object;
import java.util.LinkedList;

public class ObjectClass {
    public static void main(String[] args) {

        // Create an object of Float type

        Float fl = new Float(10.f);

        // Create an object of linked list type

        LinkedList ll = new LinkedList();

        // Add few elements in Linked List

        ll.add(10);
        ll.add(20);

        // Display String representation of the objects

        System.out.println("The String representation of the Object fl is: " + fl.toString());
        System.out.println("The String representation of the Object ll is: " + ll.toString());
    }
}

Output

输出量

D:\Programs>javac ObjectClass.java

D:\Programs>java ObjectClass
The String representation of the Object fl is :10.0
The String representation of the Object ll is :[10, 20]


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

字符串tostring

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值