迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中将对象转换为字符串

作者:迹忆客 最近更新:2023/03/26 浏览次数:

本篇文章介绍了如何在 Java 中将对象转换为字符串。


使用 Java 中的 valueOf() 方法将对象转换为字符串

String 类的 valueOf() 方法可以将对象转换为字符串。请参见下面的示例。

public class SimpleTesting{
    public static void main(String[] args) {
        Object obj = "DelftStack Portal";
        System.out.println("Object value: "+obj);
        String str = String.valueOf(obj);
        System.out.println("String value: "+str);
    }
}

输出:

Object value: DelftStack Portal
String value: DelftStack Portal

使用 Java 中的+ 运算符将对象转换为字符串

在 Java 中,加号运算符+ 将任何类型的值与字符串连接起来并返回结果字符串。我们也可以使用它将对象转换为字符串。请参见以下示例。

public class SimpleTesting{
    public static void main(String[] args) {
        Object obj = "DelftStack Portal";
        System.out.println("Object value: "+obj);
        String str = ""+obj;
        System.out.println("String value: "+str);
    }
}

输出:

Object value: DelftStack Portal
String value: DelftStack Portal

在 Java 中使用 toString() 方法将对象转换为字符串

Object 类的 toString() 方法将任何对象转换为字符串。请参见以下示例。

public class SimpleTesting{
    public static void main(String[] args) {
        Object obj = "DelftStack Portal";
        System.out.println("Object value: "+obj);
        String str = obj.toString();
        System.out.println("String value: "+str);
    }
}

输出:

Object value: DelftStack Portal
String value: DelftStack Portal

在 Java 中使用 toString() 方法将对象转换为字符串

对象可以是任何类型。例如,如果我们有一个整数对象并想要获取其字符串对象,请使用 toString() 方法。请参见下面的示例。

public class SimpleTesting{
    public static void main(String[] args) {
        Integer iVal = 123;
        System.out.println("Integer Object value: "+iVal);
        String str = iVal.toString();
        System.out.println("String value: "+str);
    }
}

输出:

Hello
This
is
DelfStack

在 Java 中使用 toString() 方法将对象转换为字符串

本示例说明了如何使用 toString() 方法将用户定义的对象转换为字符串。请参见下面的示例。

class Employee{
    String fName;
    String lName;

    public Employee(String fName, String lName) {
        this.fName = fName;
        this.lName = lName;
    }

    public String getfName() {
        return fName;
    }
    public void setfName(String fName) {
        this.fName = fName;
    }
    public String getlName() {
        return lName;
    }
    public void setlName(String lName) {
        this.lName = lName;
    }

    @Override
    public String toString() {
        return "Employee [fName=" + fName + ", lName=" + lName + "]";
    }

    public String getString() {
        return toString();
    }
}

public class SimpleTesting{
    public static void main(String[] args) {
        Employee employee = new Employee("Rohan","Mosac");
        System.out.println(employee.getString());

    }
}

输出:

Employee [fName=Rohan, lName=Mosac]

使用 Java 中的 join() 方法将对象转换为字符串

在这里,我们使用 join() 方法将 ArrayList 对象转换为字符串。String 类的 join() 方法将它们连接到单个 String 对象中后返回一个字符串。请参见下面的示例。

import java.util.ArrayList;
import java.util.List;
public class SimpleTesting{
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Sun");
        list.add("Moon");
        list.add("Earth");
        System.out.println("List object: "+list);
        // list object to string
        String str = String.join(",", list);
        System.out.println("String: "+str);
    }
}

输出:

List object: [Sun, Moon, Earth]
String: Sun,Moon,Earth

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便