61

这个静态println函数是out来自System命名空间的类吗?

命名空间系统{
  下课{
    静态打印...
  }

我该如何解释这个名字?这个函数在 JRE 的什么地方定义?在java.lang.System/ java.lang.Object?

4

19 回答 19

87

不。实际上outSystem类中的静态成员(不像 .NET 中那样),是PrintStream. 并且printlnPrintStream该类的普通(重载)方法。

请参阅http://download.oracle.com/javase/6/docs/api/java/lang/System.html#out

实际上,如果out//是类,由于命名约定(忽略语法),它们将使用大写字符 ( / err/ ) 命名。inOutErrIn

于 2010-08-04T14:50:10.923 回答
48

System是一个具有公共静态字段的类out。所以更像

class System 
{
    public static PrintStream out;
}

class PrintStream
{
    public void println ...
}

这有点过于简单化了,因为PrintStream类实际上是在java.io包中,但它足以显示东西的关系。

于 2010-08-04T14:52:24.083 回答
28

System.out.println()

高层次的理解

为了理解这一点,我们需要回顾一下 java 的一些基础知识:

  • java 中的点 (.) 运算符:在 java 中。(点运算符)仅用于调用方法或变量。所以我们可以说是方法或变量。
  • java中的方法:我们知道方法名称后面总是有括号'()',所以out不能是java中的方法。所以它是一个变量,而 println() 是一个方法
  • java中的类名:类名最好在java中以大写字母开头,所以 System 是一个类

现在有了java的基本知识,我们知道:

  • 系统是一个类
  • out 是一个变量
  • println() 是一种方法

让我们更详细地了解:

出变量:静态或实例?

  • 使用类名调用,所以我们知道它的 System 类的静态变量。

  • 但它调用方法 println() 方法,因此“out”是引用类型 PrintStream 的对象。

System 类属于 java.lang 包

class System {
  public static final PrintStream out;
  //...
}

Prinstream 类属于 java.io 包

class PrintStream{
public void println();
//...
}

在我的 youtube 上解释了什么是System.out.println

于 2014-11-27T10:40:09.383 回答
27

检查以下链接:

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/System.html

你会清楚地看到:

System是包中的一个java.lang

out是类的静态成员System并且是java.io.PrintStream.

println是一种方法java.io.PrintStream此方法被重载以将消息打印到输出目标,通常是控制台或文件。

于 2010-08-04T15:10:13.167 回答
7

println并且print是属于PrintStream该类的两个重载方法。

要访问它们,我们需要这个类的一个实例。

在类上创建一个称为类型的静态属性outPrintStreamSystem

因此,要访问上述方法,我们使用以下语句:

System.out.println("foo");
System.out.print("foo");
于 2011-12-21T07:06:09.737 回答
4
System.out.println("Hello World");
  1. System:它是标准类的名称,包含封装系统的标准I/O设备的对象。

它包含在包装 java.lang中。由于java.lang默认情况下每个java程序都会导入包,因此java.lang是Java API中唯一不需要导入声明的包。

  1. out对象out代表输出流(即命令窗口),是类的静态数据成员 System

所以请注意这里System.outSystem-Class & out- static object ,即为什么它只是由类名引用,我们不需要创建任何对象)。

  1. println:对象的println()is方法out将文本字符串作为参数并将其显示到标准输出,即在监视器屏幕上

注意
System -Class
out -static Object
println()-method
记住一个函数(在 java 中称为方法)总是有格式function()

于 2013-07-07T23:15:54.887 回答
2

Systemjava.lang包中的一个类

out是包中类的static对象PrintStreamjava.io

println()PrintStream类中的一个方法

于 2012-04-26T11:30:37.900 回答
2

Systemjava.lang包的一个类,out是类的对象,PrintStream也是类static的数据成员,是System类的实例方法。它在控制台上提供软输出。print()println()PrintStream

于 2014-09-04T13:12:13.143 回答
1

理解这个问题很简单,但要回答它,我们需要更深入地研究 Java 原生代码。

  • System是静态类,不能实例化
  • out是定义在System
  • println()是用于在标准输出上打印的方法。

一个简短而好的解释总是受欢迎的,因为我们可以从这句话本身中学到很多东西!

于 2012-08-18T08:56:35.263 回答
1

因为 out 是用System类名本身调用的,而不是类的实例(对象),所以out必须是属于该类的静态变量Systemout必须是类的实例,因为它正在调用方法println()

// the System class belongs to java.lang package
class System {
    public static final PrintStream out;
}
class PrintStream {
    public void println();
}
于 2014-07-17T09:22:10.893 回答
1

System是 中的一个类java.lang package。并且out是一个PrintStream对象。很好的解释@ http://lazy-geeks.blogspot.in/2015/01/what-is-systemoutprintln.html

于 2015-01-01T16:27:11.907 回答
1
System.out.println();

System是班级

outSystem类中的一个变量,它是一个static,变量类型是PrintStream

这是类中的out变量System

public final static PrintStream out = null;

System你可以在这里看到实现。

println()PrintStream类中的重载方法。

PrintStream包括三种重载打印方法,它们是:

  1. print()
  2. println()
  3. printf()

你可以在这里看到实现PrintStream

您不能实例化System类,它是每个类的子类,Object并且Object是每个类的父类(超类),包括您定义的类。

这是 oracle 文档所说的:

public final class System extends Object

该类System包含几个有用的类字段和方法。它不能被实例化。

该类提供的设施System包括标准输入、标准输出和错误输出流;访问外部定义的属性和环境变量;一种加载文件和库的方法;以及一种用于快速复制数组的一部分的实用方法。

自:JDK1.0

如果您不知道实例化的含义,请阅读此问题。这是 C# 问题,但概念是相同的。

另外,实例和对象有什么区别?

如果您不知道重载的含义,请阅读此问题

于 2017-07-20T14:56:58.797 回答
0

Systemjava.lang包中的一个类。

out是类中的静态数据成员和System类的引用变量PrintStream

Println()PrintStream类的普通(重载)方法。

于 2012-08-24T02:17:51.913 回答
0
System.out.println

Systemjava.lang包中的一个类。

out是类的static数据成员System并引用类的变量PrintStream

于 2014-03-29T16:28:28.507 回答
0

System:java.lang包的预定义类。

out:是类的static成员,printStream它与控制台连接。

Println:printstream类的方法,而不是static.

于 2014-07-09T07:54:21.720 回答
0

从 javadoc aboutSystem中,文档是这样说的:

public final class System
extends Object

The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.

Since:
JDK1.0

关于System.out

public static final PrintStream out

The "standard" output stream class Prinstream  belongs to java.io package. This stream is already open and ready to accept output data. 
When the JVM is initialized, the method initializeSystemClass() is called that does exactly what it’s name says – it initializes the System class and sets the out variable. The initializeSystemClass() method actually calls another method to set the out variable – this method is called setOut().
Typically this stream corresponds to display output or another output destination specified by the host environment or user.

关于 println();

class PrintStream{
public void println();
}

对于简单的独立 Java 应用程序,编写一行输出数据的典型方法是:

System.out.println(data);
于 2014-12-10T06:47:02.793 回答
0

系统是java类。

out是 PrintStream 的实例和静态成员。

println是 PrintStream 的方法。

于 2015-09-21T13:13:39.157 回答
0

Java 代码中的 System.out.println("...") 被翻译成 JVM。研究 JVM 让我更好地理解了幕后发生的事情。

来自Java Virtual Machine 编程一书。此代码是从https://github.com/ymasory/programming-for-the-jvm/blob/master/examples/HelloWorld.j复制的。

这是JVM源代码。

.class public HelloWorld
.super java/lang/Object

.method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 1
  getstatic java/lang/System/out Ljava/io/PrintStream;
  ldc "Hello, world"
  invokevirtual java/io/PrintStream/println
    (Ljava/lang/String;)V
  return

.end method
.end class

由于“JVM 不允许字节级访问内存”out类型为 L 的对象java/io/PrintSteramgetstatic使用JVM 命令存储在堆栈中。然后在从名为 的实例调用类的方法println之前,将参数压入堆栈。该方法的参数是 (L ;),输出类型是 void (V)。java/io/PrintStreamoutjava/lang/String

于 2016-05-08T16:29:35.727 回答
-1

System-final本质上的类。public final class System{}. 属于java.lang

out-static类型的参考变量PrintStream

println()- 课堂上的非static方法PrintStreamPrintStream属于java.io包。

要更好地理解它,您可以访问:System.out.println() 如何在 Java中工作

于 2014-09-17T09:16:02.487 回答