在 Java 中打印对象
本文介绍了如何在 Java 中打印对象并列出了一些示例代码来理解该主题。
对象是类的实例,我们可以使用它来访问类的属性和方法。但是如果我们尝试使用 System.out.println()
方法打印一个对象,我们可能不会得到预期的输出。我们经常打印对象属性以进行调试并确保一切正常。在本教程中,我们将学习如何在 Java 中打印对象属性。
了解对象输出
关于对象
-
让我们试着理解当我们打印一个对象时会发生什么。当我们调用 System.out.print() 方法时,就会调用 Object 类的
toString()
方法。 -
正如我们所知,Java 中的所有类都扩展了
Object
类。因此,toString()
方法可以应用于任何类的任何实例。 - 该方法返回一个由类名和对象的哈希码组成的字符串。这两个由@符号连接。
Java 中的打印对象
让我们创建一个新类并尝试打印其对象。
class Student
{
private String studentName;
private int regNo;
private Double gpa;
Student(String s, int i, Double d)
{
this.studentName = s;
this.regNo = i;
this.gpa = d;
}
}
public class Demo
{
public static void main(String[] args)
{
Student s1 = new Student("Justin", 101, 8.81);
Student s2 = new Student("Jessica", 102, 9.11);
System.out.println(s1);
System.out.println(s2);
}
}
输出:
Student@7a81197d
Student@5ca881b5
输出的第一部分显示类名(在本例中为 Student
),第二部分显示对象的唯一哈希码。每次运行上述代码时,我们都会得到不同的哈希码。
数组对象
数组在 Java 中也是一个对象,当我们尝试将数组打印到控制台时,我们没有得到它的元素。让我们运行以下代码并查看其输出。
public class Demo
{
public static void main(String[] args)
{
int[] integerArr = {5, 10, 15};
double[] doubleArr = {5.0, 10.0, 15.0};
char[] charArr = {'A', 'B', 'C'};
String[] stringArr = {"Justin", "Jessica"};
int[][] twoDimArray = {
{1,2,3},
{4,5,6}
};
System.out.println("Integer Array:" + integerArr);
System.out.println("Double Array:" + doubleArr);
System.out.println("Char Array:" + charArr);
System.out.println("String Array:" + stringArr);
System.out.println("2D Array:" + twoDimArray);
}
}
输出:
Integer Array:[I@36baf30c
Double Array:[D@7a81197d
Char Array:[C@5ca881b5
String Array:[Ljava.lang.String;@24d46ca6
2D Array:[[I@4517d9a3
- 方括号表示数组的维度。对于一维数组,将打印单个左方括号。对于二维数组,我们有两个括号。
- 括号后的下一个字符表示数组中存储的内容。对于整数数组,打印一个 I。对于 char 数组,打印字母 C。
- 字符串数组的 L 表示该数组包含一个类的对象。在这种情况下,接下来打印类的名称(在我们的例子中是 java.lang.String)。
- 在@ 符号之后,打印对象的哈希码。
如何打印对象
如果我们想以不同的格式打印对象及其属性,那么我们需要覆盖我们类中的 toString()
方法。此方法应返回一个字符串。让我们在我们的 Student 类中覆盖这个方法并理解这个过程。
class Student
{
private String studentName;
private int regNo;
private Double gpa;
Student(String s, int i, Double d)
{
this.studentName = s;
this.regNo = i;
this.gpa = d;
}
//overriding the toString() method
@Override
public String toString()
{
return this.studentName + " " + this.regNo + " " + this.gpa;
}
}
现在,当我们打印这个类的对象时,我们可以查看学生的姓名、注册号和 GPA。
public class Demo
{
public static void main(String[] args)
{
Student s1 = new Student("Justin", 101, 8.81);
System.out.print(s1);
}
}
输出:
Justin 101 8.81
打印数组对象
我们需要使用 Arrays.toString()
方法来查看数组中的元素。请注意,如果我们有一个用户定义的类对象数组,那么用户定义的类也应该有一个重写的 toString()
方法。这将确保正确打印类属性。
import java.util.Arrays;
class Student
{
private String studentName;
private int regNo;
private Double gpa;
Student(String s, int i, Double d)
{
this.studentName = s;
this.regNo = i;
this.gpa = d;
}
//overriding the toString() method
@Override
public String toString()
{
return this.studentName + " " + this.regNo + " " + this.gpa;
}
}
public class Demo
{
public static void main(String[] args)
{
Student s1 = new Student("Justin", 101, 8.81);
Student s2 = new Student("Jessica", 102, 9.11);
Student s3 = new Student("Simon", 103, 7.02);
//Creating Arrays
Student[] studentArr = {s1, s2, s3};
int[] intArr = {5, 10, 15};
double[] doubleArr = {5.0, 10.0, 15.0};
String[] stringArr = {"Justin", "Jessica"};
System.out.println("Student Array: " + Arrays.toString(studentArr));
System.out.println("Intger Array: " + Arrays.toString(intArr));
System.out.println("Double Array: " + Arrays.toString(doubleArr));
System.out.println("String Array: " + Arrays.toString(stringArr));
}
}
输出:
Student Array: [Justin 101 8.81, Jessica 102 9.11, Simon 103 7.02]
Intger Array: [5, 10, 15]
Double Array: [5.0, 10.0, 15.0]
String Array: [Justin, Jessica]
打印多维数组对象
对于多维数组,使用 deepToString()
方法而不是 toString()
方法并将所需的输出发送到控制台。
import java.util.Arrays;
class Student
{
private String studentName;
private int regNo;
private Double gpa;
Student(String s, int i, Double d)
{
this.studentName = s;
this.regNo = i;
this.gpa = d;
}
//overriding the toString() method
@Override
public String toString()
{
return this.studentName + " " + this.regNo + " " + this.gpa;
}
}
public class Demo
{
public static void main(String[] args)
{
Student s1 = new Student("Justin", 101, 8.81);
Student s2 = new Student("Jessica", 102, 9.11);
Student s3 = new Student("Simon", 103, 7.02);
Student s4 = new Student("Harry", 104, 8.0);
Student[][] twoDimStudentArr = {
{s1, s2},
{s3, s4}
};
System.out.println("Using toString(): " + Arrays.toString(twoDimStudentArr));
System.out.println("Using deepToString(): " + Arrays.deepToString(twoDimStudentArr));
}
}
输出:
Using toString(): [[LStudent;@7a81197d, [LStudent;@5ca881b5]
Using deepToString(): [[Justin 101 8.81, Jessica 102 9.11], [Simon 103 7.02, Harry 104 8.0]]
在 Java 中打印集合对象
像 Lists
、Sets
和 Maps
这样的集合不需要像 Arrays.toString()
这样的任何添加方法。如果我们正确地覆盖了我们类的 toString()
方法,那么简单地打印集合就会给我们所需的输出。
import java.util.ArrayList;
import java.util.HashSet;
class Student
{
private String studentName;
private int regNo;
private Double gpa;
Student(String s, int i, Double d)
{
this.studentName = s;
this.regNo = i;
this.gpa = d;
}
//overriding the toString() method
@Override
public String toString()
{
return this.studentName + " " + this.regNo + " " + this.gpa;
}
}
public class Demo
{
public static void main(String[] args)
{
Student s1 = new Student("Justin", 101, 8.81);
Student s2 = new Student("Jessica", 102, 9.11);
Student s3 = new Student("Simon", 103, 7.02);
//Creating an ArrayList
ArrayList<Student> studentList = new ArrayList<>();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
//Creating a Set
HashSet<Student> studentSet = new HashSet<>();
studentSet.add(s1);
studentSet.add(s2);
studentSet.add(s3);
System.out.println("Student List: " + studentList);
System.out.println("Student Set: " + studentSet);
}
}
输出:
Student List: [Justin 101 8.81, Jessica 102 9.11, Simon 103 7.02]
Student Set: [Simon 103 7.02, Justin 101 8.81, Jessica 102 9.11]
使用 Apache 公共库
如果你正在使用 Apache 公共库,那么使用 Apache 公共库的 ToStringBuilder
类以不同的方式格式化我们的对象。我们可以使用这个类的 reflectionToString()
方法。
我们可以使用此方法简单地打印对象类和哈希码以及为属性设置的值。
import org.apache.commons.lang3.builder.ToStringBuilder;
class Student
{
private String studentName;
private int regNo;
private Double gpa;
Student(String s, int i, Double d)
{
this.studentName = s;
this.regNo = i;
this.gpa = d;
}
@Override
public String toString () {
return ToStringBuilder.reflectionToString(this);
}
}
public class Demo
{
public static void main(String[] args)
{
Student s = new Student("Justin", 101, 8.81);
System.out.print(s);
}
}
输出:
Student@25f38edc[gpa=8.81,regNo=101,studentName=Justin]
如果我们想省略哈希码,那么我们可以使用 SHORT_PREFIX_STYLE 常量。请参考下面的示例。
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
class Student
{
private String studentName;
private int regNo;
private Double gpa;
Student(String s, int i, Double d)
{
this.studentName = s;
this.regNo = i;
this.gpa = d;
}
@Override
public String toString () {
return ToStringBuilder.reflectionToString(this,ToStringStyle.SHORT_PREFIX_STYLE);
}
}
public class Demo
{
public static void main(String[] args)
{
Student s = new Student("Justin", 101, 8.81);
System.out.print(s);
}
}
输出:
Student[gpa=8.81,regNo=101,studentName=Justin]
如果我们的类包含嵌套对象,我们可以使用 RecursiveToStringStyle()
方法来打印对象。
@Override
public String toString()
{
return ToStringBuilder.reflectionToString(this, new RecursiveToStringStyle());
}
总结
Object
类是 Java 中所有类的超类。在打印对象时调用的 toString()
方法在 Object
类中实现。但是这个实现没有提供关于用户定义的类属性的任何信息。为了正确查看这些属性,我们需要覆盖我们类的 toString()
方法。对于数组,我们可以直接使用 toString()
方法或 deepToString()
。
相关文章
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
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。