简体   繁体   English

为什么我们需要 System class 来调用 out.println() 方法?

[英]Why do we need System class to call out.println() method?

When we want to print line in console we write System.out.println(data), what will be difference if we use PrintStream object out and use out.println(data).当我们想在控制台打印行时我们编写 System.out.println(data),如果我们使用 PrintStream object out 和使用 out.println(data) 会有什么区别。

Thanks.谢谢。

System.out is a static reference to the PrintStream "out". System.out是 static 对PrintStream “输出”的引用。 So there is no difference.所以没有区别。

PrintStream out = System.out;
out.println("Badgers");

Why do we need System class to call out.println() method?为什么我们需要 System class 来调用 out.println() 方法?

We don't.我们没有。 It is just a convenient way to get hold of the PrintStream object for writing to standard output.这只是获取PrintStream object 以写入标准 output 的便捷方式。

... what will be difference if we use PrintStream object out and use out.println(data). ...如果我们使用 PrintStream object 并使用 out.println(data) 会有什么不同。

It depends on how you obtained the PrintStream object.这取决于您如何获得PrintStream object。

  • If you originally got the PrintStream object from System.out or System.err , then there is no difference;如果您最初从System.outSystem.err获得PrintStream object ,则没有区别; eg the following have the same effect:例如以下具有相同的效果:

     public void sayHello(PrintStream ps) { ps.println("hello"); } sayHello(System.out); System.out.println("hello");
  • If you created the PrintStream on a stream that opened yourself, the behaviour will be analogous, but the output will (most likely) go to a different place;如果您在自己打开的 stream 上创建了 PrintStream,则行为将是类似的,但 output 将(很可能)将 go 转移到不同的地方; eg例如

    sayHello(new PrintStream(new FileOutputStream("foo.txt"));

    will write "hello" to the named file.将“hello”写入命名文件。

There is nothing special about System.in/out/err . System.in/out/err没有什么特别之处。 They are just public static final variables that are initialized to specific stream objects when the JVM starts.它们只是public static final变量,在 JVM 启动时初始化为特定的 stream 对象。 (OK, there is something slightly unusual about them... but it doesn't manifest here.) (好吧,他们有一些不寻常的地方......但它并没有在这里表现出来。)

There's absolutely no difference.完全没有区别。 From ProgrammerInterview.com :来自ProgrammerInterview.com

Inside the System class is the declaration of out that looks like: public static final PrintStream out , and inside the Printstream class is a declaration of println() that has a method signature that looks like: public void println() .System class 内部是out的声明,如下所示: public static final PrintStream out ,在Printstream class 内部是一个看起来像println()的声明,它有一个public void println()方法。

PrintStream is a class which streams the data to the standard output. PrintStream是一个 class,它将数据流式传输到标准 output。 An association was made by adding the object which is static and final out in the System class.通过添加 object 进行关联,即staticfinalSystem class 中输出。 So we as programmers can use it as System the class name (System.out) out is static object of PrintStream class and println( ) is a method in PrintStream class which are overloaded methods. So we as programmers can use it as System the class name (System.out) out is static object of PrintStream class and println( ) is a method in PrintStream class which are overloaded methods. So we directly write System.out.println(data) .所以我们直接写System.out.println(data)

System.out is a static PrintStream object which is bound to a special file of the System: the standard output. System.out是一个 static PrintStream object,它绑定到系统的一个特殊文件:标准 output。 You can assign the reference of it to any other PrintStream variable.您可以将它的引用分配给任何其他PrintStream变量。

None, but out is commonly used as a variable in a program so it might be ambiguous.没有,但out通常用作程序中的变量,因此它可能不明确。

println() is a method in PrintStream class. println()PrintStream class 中的一个方法。 out is the object of PrintStream class which is defined as static constant in System class with the address of console. out 是PrintStream class 的 object 定义为 static 常数System ZA2F2ED4F8EBC2CBB14C21A29DZ40 地址。

So, in whatever way we invoke that method it will be same.因此,无论我们以何种方式调用该方法,它都是相同的。

But if you dont want to use System class, you need to create the PrintStream object by giving the standard output address.但是如果您不想使用System class,则需要通过提供标准 output 地址来创建PrintStream object。

The words System.out.println have been used so much in the past that every casual reader of Java code knows what they mean. System.out.println这个词在过去被大量使用,以至于每个 Java 代码的普通读者都知道它们的意思。 Everyone writes it this way, and people got accustomed to it.每个人都这样写,人们也习惯了。

The code would probably be harder to read if you first assigned System.out to a local variable.如果您首先将System.out分配给局部变量,则代码可能会更难阅读。 This is because in every line you would have to ask yourself: “Well, something gets printed, but where ?”.这是因为在每一行中,您都必须问自己:“好吧,有些东西被打印出来了,但是在哪里?”。 The idiom System.out.println on the other hand answers that question.另一方面,成语System.out.println回答了这个问题。 It clearly sticks out from the code as something being output (even if it is just for debugging purposes).它显然从代码中突出显示为 output(即使它只是用于调试目的)。

So yes, you can use a shortcut for System.out.println , but in most cases you shouldn't.所以是的,您可以使用System.out.println的快捷方式,但在大多数情况下您不应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM