System.out,stdout和cout是完全相同的吗?

时间:2010-10-06 14:18:07

标签: java c++ c iostream stdio

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

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

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

4 个答案:

答案 0 :(得分:5)

coutstdout基本相同,但区别在于cout的类型为ostream(这实际上意味着您可以使用{{1}输入格式化数据使用<<方法的无格式数据。

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

Java fprintf基本上类似于System.out(它使用带有句柄stdout的{​​{1}})并传递到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);
    }

<强>来源:

  • cout
  • stdout
  • 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); System.out
  • Wikipedia

答案 1 :(得分:4)

它们是相同的,但它们没有相同的类型。例如,stdoutFILE*coutstd::ostream。由于C ++支持两者,因此需要使用不同的名称。

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

答案 2 :(得分:0)

理论上它们是相同的,它们都被发送到标准输出。

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

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

我认为遵循命名惯例是因为每种语言的设计者都不同。 C,C ++与Unix紧密相关,因此他们使用标准输出和控制台等术语.Java旨在更加面向对象,因此Java的创建者决定将它命名为有点不同。

答案 3 :(得分:0)

它们是每种语言特定的方式,用于写入程序的“标准输出”文件,这是一个源自C / UNIX的概念。它们在执行输出时提供的确切功能/方法不同。

值得一提的是,coutstdout都可以在C ++中使用,因为它试图成为C语言的超集,但混合使用这两者可能是一个坏主意除非你完全禁用缓冲。我不知道要求两者共享一个缓冲区,所以如果混合它们,输出可能会出错。

相关问题