Are System.out, stdout and cout the same?

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

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

In addition, I know what they are used for, but what is it, under the hood, I mean?

+3
source share
4 answers

coutessentially the same as stdout, but the difference is what the couttype is ostream(which essentially means that you can enter formatted data using <<or unformatted data using the writemethod.

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);

+4

, . , stdout - FILE*, cout - std::ostream. ++ , .

. (stdin, stdout, stderr), , . , , stdout ( > >>).

+4

- , .

C ++ cout stdout, , System.out . java , System, out , PrintStream cout.

PritnStream , , PrintStream IOException, , checkError.

, , . C, ++ Unix , , , .. Java - , , , Java .

0

" " , , C/UNIX. /, .

Also worth mentioning is that, as coutwell as stdoutare available in C ++, as it is half way trying to be a superset of the C language, but mixing the use of the two can be a bad idea if you are not completely disable buffering on both. I do not know any requirements for them to share the buffer, so it is possible that the output will be erroneous if you accept them.

0
source

Source: https://habr.com/ru/post/1768249/


All Articles