System.out、stdout 和 cout 是完全相同的东西吗?

发布于 2024-09-26 04:30:17 字数 148 浏览 7 评论 0原文

System.out、stdout 和 cout 在 Java、C 和 C++ 中分别是完全相同的东西吗?

为什么同一事物有三个不同的名称(特别是当 C、C++ 和 Java 有很多共同点时)?

另外,我知道它们的用途,但我的意思是,它们到底是什么?

Are System.out, stdout and cout the EXACT same thing in Java, C and C++ respectively?

Why have three different names for the same thing (especially when C, C++ and Java have much in common)?

Also, I know what they are used for but what are they exactly, under the hood, I mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此岸叶落 2024-10-03 04:30:18

它们是同一件事,但类型不同。例如,stdout 是一个FILE*cout 是一个std::ostream。由于 C++ 支持两者,因此需要不同的名称。

在底层,所有这些变量都引用调用进程的标准输出。它是操作系统在生成新进程时始终打开的三个文件描述符(stdinstdoutstderr)之一。写入此文件描述符的所有内容最终都会显示在屏幕上或 stdout 重定向到的任何位置(使用 >>> shell 运算符)。

They are the same thing, but they don't have the same type. For instance, stdout is a FILE* and cout is an std::ostream. Since C++ supports both, different names are necessary.

Under the hood, all these variables reference the standard output of the calling process. It's one of the three file descriptors (stdin, stdout, stderr) that are always open by the OS when it spawns a new process. Everything written to this file descriptor ends up on the screen or wherever stdout was redirected to (using the > or >> shell operators).

叹倦 2024-10-03 04:30:18

它们是每种语言特定的写入程序“标准输出”文件的方式,这个概念起源于 C/UNIX。它们的不同之处在于它们为执行输出提供的确切函数/方法。

还值得一提的是,coutstdout 在 C++ 中都可用,因为它半途而废地试图成为 C 语言的超集,但混合使用两者可能会除非您完全禁用两者的缓冲,否则这是一个坏主意。我不知道两者有任何共享缓冲区的要求,因此如果混合它们,输出可能会出现乱序。

They are each language-specific ways for writing to the program's "standard output" file, a concept which originated with C/UNIX. They differ in the exact functions/methods they provide for performing output.

It's also worth mentioning that both cout and stdout are available in C++, since it halfway tries to be a superset of the C language, but mixing the use of the two may be a bad idea unless you disable buffering entirely on both. I'm not aware of any requirement for the two to share a buffer, so it's possible that output will come out misordered if you mix them.

夜夜流光相皎洁 2024-10-03 04:30:18

理论上它们是同一件事,都发送到标准输出。

但在 C 和 C++ 中,cout 构建在 stdout 之上,以添加 System.out 提供的一些功能,例如格式化。由于java没有指针的概念,System,out被重新设计为使用PrintStream来完成与cout类似的任务。

PritnStream 提供了一些额外的功能,例如,PrintStream 不会抛出 IOException,而是设置一个内部错误标志,然后可以使用 checkError 访问该错误标志。

我认为之所以遵循命名约定,是因为每种语言的设计者都不同。 C、C++ 与 Unix 密切相关,因此它们使用标准输出和控制台等术语。Java 被设计为更加面向对象,因此 Java 的创建者决定将其命名为有点不同。

In theory they are the same thing, all of them sent to the standard output.

But in C and C++, cout builds on top of stdout to add some features that System.out provides like formatting. Since java has no concept of pointers, System,out was redesigned to use the PrintStream to do a similar kind of task as cout.

PritnStream provides some extra features for example, PrintStream does not throw an IOException but instead sets an internal error flag that can then be accessed using checkError.

I think the naming conventions were followed because the designers of each language were different. C, C++ is closely associated with Unix and hence they used terms like standard output and console, etc. Java was designed to be more object oriented, and thus the creator of Java decided to name it a bit different.

茶色山野 2024-10-03 04:30:17

cout 本质上与 stdout 相同,但不同之处在于 coutostream 类型(这本质上意味着您可以使用 << 输入格式化数据,或使用 write 方法输入未格式化的数据

stdout 附加到文件描述符(stdout 是一个文件描述符) 。 FILE*)。stdout 文件描述符是 1,因为它返回对文件描述符的引用,因此可以在 fputs 中使用。fprintf。Java

System.out 本质上类似于 stdout(它使用 java.io.FileDescriptor)。 > 带有句柄 1),并传递到 FileOutputStream 中,最后包装在 BufferedOutputStream 中,

这就是 java.lang.System 的方式 。 > 初始化:

 /**
     * Initialize the system class.  Called after thread initialization.
     */
    private static void initializeSystemClass() {
    props = new Properties();
    initProperties(props);
    sun.misc.Version.init();

        // Workaround until DownloadManager initialization is revisited.
        // Make JavaLangAccess available early enough for internal
        // Shutdown hooks to be registered
        setJavaLangAccess();

        // Gets and removes system properties that configure the Integer
        // cache used to support the object identity semantics of autoboxing.
        // At this time, the size of the cache may be controlled by the
        // vm option -XX:AutoBoxCacheMax=<size>.
        Integer.getAndRemoveCacheProperties();

    // Load the zip library now in order to keep java.util.zip.ZipFile
    // from trying to use itself to load this library later.
    loadLibrary("zip");

    FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
    FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
    FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
    setIn0(new BufferedInputStream(fdIn));
    setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
    setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));

    // Setup Java signal handlers for HUP, TERM, and INT (where available).
        Terminator.setup();

        // Initialize any miscellenous operating system settings that need to be
        // set for the class libraries. Currently this is no-op everywhere except
        // for Windows where the process-wide error mode is set before the java.io
        // classes are used.
        sun.misc.VM.initializeOSEnvironment();

    // Set the maximum amount of direct memory.  This value is controlled
    // by the vm option -XX:MaxDirectMemorySize=<size>.  This method acts
    // as an initializer only if it is called before sun.misc.VM.booted().
    sun.misc.VM.maxDirectMemory();

    // Set a boolean to determine whether ClassLoader.loadClass accepts
    // array syntax.  This value is controlled by the system property
    // "sun.lang.ClassLoader.allowArraySyntax".  This method acts as
    // an initializer only if it is called before sun.misc.VM.booted().
    sun.misc.VM.allowArraySyntax();

    // Subsystems that are invoked during initialization can invoke
    // sun.misc.VM.isBooted() in order to avoid doing things that should
    // wait until the application class loader has been set up.
    sun.misc.VM.booted();

        // The main thread is not added to its thread group in the same
        // way as other threads; we must do it ourselves here.
        Thread current = Thread.currentThread();
        current.getThreadGroup().add(current);
    }

FileDescriptor.out 为:

/**
 * A handle to the standard output stream. Usually, this file
 * descriptor is not used directly, but rather via the output stream
 * known as <code>System.out</code>.
 * @see     java.lang.System#out
 */
public static final FileDescriptor out = standardStream(1);

cout is essentially the same as stdout but the difference is that cout is of type ostream (which essentially means that you can enter formatted data using << or unformatted data with the write method.

stdout is attached to a file descriptor (stdout is a FILE*). stdout file descriptor is 1. Because it returns a reference to a file descriptor, it can be used in fputs and fprintf.

Java System.out is essentially like stdout (it uses java.io.FileDescriptor with handle 1) and passed into FileOutputStream and finally wrapped inside BufferedOutputStream.

This is how java.lang.System is initialized:

 /**
     * Initialize the system class.  Called after thread initialization.
     */
    private static void initializeSystemClass() {
    props = new Properties();
    initProperties(props);
    sun.misc.Version.init();

        // Workaround until DownloadManager initialization is revisited.
        // Make JavaLangAccess available early enough for internal
        // Shutdown hooks to be registered
        setJavaLangAccess();

        // Gets and removes system properties that configure the Integer
        // cache used to support the object identity semantics of autoboxing.
        // At this time, the size of the cache may be controlled by the
        // vm option -XX:AutoBoxCacheMax=<size>.
        Integer.getAndRemoveCacheProperties();

    // Load the zip library now in order to keep java.util.zip.ZipFile
    // from trying to use itself to load this library later.
    loadLibrary("zip");

    FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
    FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
    FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
    setIn0(new BufferedInputStream(fdIn));
    setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
    setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));

    // Setup Java signal handlers for HUP, TERM, and INT (where available).
        Terminator.setup();

        // Initialize any miscellenous operating system settings that need to be
        // set for the class libraries. Currently this is no-op everywhere except
        // for Windows where the process-wide error mode is set before the java.io
        // classes are used.
        sun.misc.VM.initializeOSEnvironment();

    // Set the maximum amount of direct memory.  This value is controlled
    // by the vm option -XX:MaxDirectMemorySize=<size>.  This method acts
    // as an initializer only if it is called before sun.misc.VM.booted().
    sun.misc.VM.maxDirectMemory();

    // Set a boolean to determine whether ClassLoader.loadClass accepts
    // array syntax.  This value is controlled by the system property
    // "sun.lang.ClassLoader.allowArraySyntax".  This method acts as
    // an initializer only if it is called before sun.misc.VM.booted().
    sun.misc.VM.allowArraySyntax();

    // Subsystems that are invoked during initialization can invoke
    // sun.misc.VM.isBooted() in order to avoid doing things that should
    // wait until the application class loader has been set up.
    sun.misc.VM.booted();

        // The main thread is not added to its thread group in the same
        // way as other threads; we must do it ourselves here.
        Thread current = Thread.currentThread();
        current.getThreadGroup().add(current);
    }

FileDescriptor.out is:

/**
 * A handle to the standard output stream. Usually, this file
 * descriptor is not used directly, but rather via the output stream
 * known as <code>System.out</code>.
 * @see     java.lang.System#out
 */
public static final FileDescriptor out = standardStream(1);

Source:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文