迹忆客 专注技术分享

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

Java system.out.println() 方法

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

本文介绍了 System.out.println() 方法在 Java 中的工作原理,并列出了一些示例代码来理解该主题。

System.out.print() 是一种非常常用的打印到控制台或标准输出的方法。这种方法有时称为打印线方法。除了打印到控制台之外,println() 方法将光标移动到一个新行。

在本教程中,我们将尝试了解此方法的内部工作原理。


什么是 System.out.println() 方法

  • System.out.println() 可以分为三个部分。
  • Systemjava.lang 包的最后一个类,它在 JVM 启动时自动初始化initializeSystemClass() 用于初始化它。
  • System 类包含 PrintStream 类的实例。这个实例变量被称为 out。它使用修饰符 publicstaticfinal 定义。
class System {
  public static final PrintStream out;
  // More Code Below
}

PrintStream 类包含 print()println() 方法。这些方法是重载的。

class PrintStream {
  public void print(argument) {
    // implementation
  }
  public void println() {
    // implementation
  }
  // Overloaded print() and println() methods below
}

因此,System.out 为我们提供了 PrintStream 类的 out 实例变量。然后我们可以在这个实例变量上调用 print()println() 方法。


System.out.println() 是如何工作的

  • PrintStream 类包含多个重载的 print()println() 方法。它们在接受的参数类型上有所不同。
  • 所有这些的返回类型都是无效的。
  • 所有原始类型都存在重载方法。
  • 它还包含一个用于打印字符串的重载方法和另一个用于对象的重载方法。

下面的代码演示了重载的 println() 方法的工作。

public class PrintDemo {
  public static void main(String args[]) {
    int i = 10;
    short s = 10;
    long l = 10;
    char c = 'A';
    char[] charArr = {'A', 'B', 'C'};
    boolean bool = true;
    double d = 10.0;
    float f = 10.0f;
    String str = "hello";
    Object o = new Object();

    System.out.println(); // terminate the current line
    System.out.println(i); // print integer
    System.out.println(s); // print short
    System.out.println(l); // print long
    System.out.println(c); // print char
    System.out.println(charArr); // print char array
    System.out.println(bool); // print boolean
    System.out.println(d); // print double
    System.out.println(f); // print float
    System.out.println(str); // print String
    System.out.println(o); // print Object
  }
}

输出:

10
10
10
A
ABC
true
10.0
10.0
hello
java.lang.Object@433c675d

传递给 println() 方法的参数

  • 初学者可能认为 print()println() 方法采用可变数量的参数 (varargs),但事实并非如此。
  • 例如,在下面的代码中,我们试图打印一个整数、一个字符串和一个字符。
public class PrintDemo {
  public static void main(String args[]) {
    int i = 10;
    String s = "hello";
    char c = 'O';
    System.out.println(i + s + c);
  }
}

输出:

10helloO
  • 但我们没有传递三个不同的参数。逗号分隔方法的参数。
  • 相反,它们使用 println() 方法中的 + 运算符连接在一起。
  • 与字符串一起使用的 + 运算符将导致字符串连接并返回一个字符串。
  • 在上面的代码中,整数首先与字符串连接,结果字符串再次与 char 变量连接。

另一件需要注意的是,传递给方法的参数是从左到右计算的。因此,如果传递的前两个变量是整数,则进行正常的算术加法,并将加法结果与字符串连接起来。

public class PrintDemo {
  public static void main(String args[]) {
    System.out.println(10 + 10 + "hello"); // first integer addition and then string concatenation
  }
}

输出:

20hello

但是如果字符串后面还有两个整数,就会发生字符串连接。

public class PrintDemo {
  public static void main(String args[]) {
    System.out.println(10 + 10 + "hello" + 1 + 1); // 20hello11 not 20hello2
  }
}

输出:

20hello11

我们可以从左到右应用下面显示的规则来计算输出。如果不存在字符串,则进行正常的算术加法。

(any data type + string) = (string + any data type) = (concatenated string)

toString()print() 方法中的作用

print()println() 方法隐式调用参数的 toString() 方法。它将参数转换为字符串。如果我们想以特定方式打印用户定义的类实例,这非常有用。

我们可以覆盖我们类中的 toString() 方法,以不同的格式打印我们的对象。以下示例演示了这一点。

示例:不覆盖 toString() 方法。

class Demo {
  String field1;
  String field2;

  Demo(String f1, String f2) {
    this.field1 = f1;
    this.field2 = f2;
  }
}
public class PrintDemo {
  public static void main(String args[]) {
    Demo d = new Demo("f1", "f2");
    System.out.print(d);
  }
}

输出:

Demo@433c675d

示例:在重写 toString() 方法之后。

class Demo {
  String field1;
  String field2;
  Demo(String f1, String f2) {
    this.field1 = f1;
    this.field2 = f2;
  }
  @Override
  public String toString() {
    return field1 + " " + field2;
  }
}
public class PrintDemo {
  public static void main(String args[]) {
    Demo d = new Demo("f1", "f2");
    System.out.print(d);
  }
}

输出:

f1 f2

概括

print()println() 方法是 PrintStream 类的一部分。它们是通过使用 System.out 实例变量访问的。这些方法被重载以处理不同的参数类型。请记住,这些方法不接受可变数量的参数。在本教程中,我们学习了 System.out.print()System.out.println() 方法的工作原理。

转载请发邮件至 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

Python 中的 Pandas 插入方法

发布时间:2024/04/23 浏览次数:112 分类:Python

本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。

Pandas 中的 get_dummies 方法

发布时间:2024/04/23 浏览次数:224 分类:Python

本教程介绍了如何从带有分类列的 DataFrame 中生成带有虚拟变量或指标变量的 DataFrame。

Pandas DataFrame 基于其他列创建新列

发布时间:2024/04/23 浏览次数:105 分类:Python

在本教程中,我们将介绍如何在 Pandas DataFrame 中根据 DataFrame 中其他列的值,通过对列的每个元素应用一个函数或使用 DataFrame.apply()方法来创建新的列。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便