System.out,stdout和cout分別是Java,C和C++中的完全相同的東西嗎?System.out,stdout和cout完全一樣嗎?
爲什麼同一件事物有三個不同的名字(特別是當C,C++和Java有很多共同點時)?
另外,我知道它們用於什麼,但它們究竟是什麼,在引擎蓋下,我的意思是?
System.out,stdout和cout分別是Java,C和C++中的完全相同的東西嗎?System.out,stdout和cout完全一樣嗎?
爲什麼同一件事物有三個不同的名字(特別是當C,C++和Java有很多共同點時)?
另外,我知道它們用於什麼,但它們究竟是什麼,在引擎蓋下,我的意思是?
cout
是基本上相同stdout
但不同的是,cout
是ostream
型的(基本上意味着可以使用<<
或未格式化數據與write
方法輸入格式的數據。
stdout
附着到文件描述符(stdout是FILE*
)。stdout
文件描述符是1
。因爲它返回到一個文件描述符的引用,它可以在fputs
和fprintf
使用。
爪哇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);
來源:
「文件描述符」不是C語言的一部分,而是POSIX。說'stdout'「返回一個文件描述符」是語言的不正確使用(一個變量怎麼可能「返回」什麼?)和誤導。 – 2010-10-06 15:31:06
@R。謝謝,更新... – 2010-10-06 15:37:08
它們是相同的東西,但它們不具有相同的類型。例如,stdout
是FILE*
而cout
是std::ostream
。由於C++支持兩者,因此需要不同的名稱。
在引擎蓋下,所有這些變量引用調用過程的標準輸出。它是三個文件描述符中的一個(stdin
,stdout
,stderr
),它們在產生新進程時總是由OS打開。寫入此文件描述符的所有內容都會在屏幕上或stdout
被重定向到的任何位置結束(使用shell操作符>
或>>
)。
從理論上講,他們是同樣的事情,他們都發送到標準輸出。
但是在C和C++中,cout建立在標準輸出的基礎之上,以增加一些System.out提供的格式化功能。由於java沒有指針的概念,System,out被重新設計爲使用PrintStream來執行類似cout的任務。
PritnStream提供了一些額外的功能,例如,PrintStream不會拋出IOException,而是設置一個內部錯誤標誌,然後可以使用checkError訪問它。
我認爲遵循命名約定是因爲每種語言的設計者都是不同的。 C,C++與Unix密切相關,因此他們使用諸如標準輸出和控制檯等術語.Java被設計爲更面向對象,因此Java的創建者決定命名它有點不同。
它們是用於寫入程序「標準輸出」文件的各種特定於語言的方式,這個概念源於C/UNIX。它們在執行輸出的確切功能/方法上有所不同。
另外值得一提的是,這兩個cout
和stdout
在C++用的,因爲它中途試圖是C語言的超集,但混合使用這兩個可能是一個壞主意,除非你禁用這兩個完全緩衝。我沒有意識到兩者共享一個緩衝區的需求,所以如果你混合使用輸出結果可能會出錯。
C,C++和Java只在語法上相似,但語義有很大差異。 – Philipp 2010-10-06 15:44:24
那麼他們是不同的計算機語言.. – 2010-10-06 15:53:02
這些都不是語言的一部分。這些是語言附帶的圖書館的一部分。它們不是關鍵字,不屬於語言規範的一部分。 – 2010-10-08 23:06:11