System.out、stdout、および cout は、それぞれ Java、C、および C++ でまったく同じものですか?
同じものに 3 つの異なる名前があるのはなぜですか (特に、C、C++、および Java に多くの共通点がある場合)。
また、それらが何に使用されているかは知っていますが、ボンネットの下で正確に何を意味するのでしょうか?
cout
は基本的にと同じですstdout
が、違いはcout
タイプです(つまり、メソッドを使用してフォーマットされたデータを入力したり、メソッドostream
を使用してフォーマットされていないデータを入力したりできることを意味します。<<
write
stdout
ファイル記述子に添付されます(stdoutはFILE*
)。stdout
ファイル記述子は1
です。ファイル記述子への参照を返すため、fputs
およびで使用できますfprintf
。
JavaSystem.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);
ソース:
それらは同じものですが、同じタイプはありません。たとえば、stdout
は でFILE*
ありcout
、std::ostream
です。C++ は両方をサポートするため、異なる名前が必要です。
内部的には、これらの変数はすべて呼び出しプロセスの標準出力を参照します。これは、新しいプロセスを生成するときに OS によって常に開かれている3 つのファイル記述子 ( stdin
、stdout
、 ) の 1 つです。stderr
このファイル ディスクリプタに書き込まれたものはすべて、最終的stdout
に画面またはリダイレクト先 (>
または>>
シェル オペレータを使用) に表示されます。
これらはそれぞれ、C / UNIXに端を発した概念である、プログラムの「標準出力」ファイルに書き込むための言語固有の方法です。それらは、出力を実行するために提供する正確な関数/メソッドが異なります。
cout
両方ともC++で使用できることにも言及する価値stdout
があります。これは、途中でC言語のスーパーセットになろうとするためですが、両方でバッファリングを完全に無効にしない限り、2つの使用を混在させることはお勧めできません。両者がバッファを共有する必要があることを私は知らないので、それらを混合すると出力が誤った順序で出力される可能性があります。
理論的には、それらは同じものであり、すべてが標準出力に送信されます。
ただし、CおよびC ++では、coutはstdoutの上に構築され、System.outが提供するフォーマットなどの機能を追加します。javaにはポインタの概念がないため、System、outは、PrintStreamを使用してcoutと同様の種類のタスクを実行するように再設計されました。
PritnStreamは、いくつかの追加機能を提供します。たとえば、PrintStreamはIOExceptionをスローしませんが、代わりに、checkErrorを使用してアクセスできる内部エラーフラグを設定します。
それぞれの言語のデザイナーが異なっていたので、命名規則に従ったと思います。C、C ++はUnixと密接に関連しているため、標準出力やコンソールなどの用語を使用していました。Javaはよりオブジェクト指向になるように設計されていたため、Javaの作成者は少し異なる名前を付けることにしました。