System.out.print() vs cout in Java and C++
A question I get asked often in my Java classes is “What is System.out.print() and is it similar to cout in C++?”
First, let’s look at the first half of this question. What is System.out.print()?
System.out.print() is a Java statement that prints the argument passed, into the System.out which is generally stdout (standard output – e.g. the console in simple programs).
- System is a Class
- out is a Variable
- print() is a method, there are several
The answer is to the second half is yes, it is similar to cout in C++. System.out and cout are the objects representing the stdout stream in Java and C++ respectively. System.out.print is a method so it would be more similar to printf (the function in stdio.h in C++).
Similarly, System.in and cin are the objects representing the stdin stream in Java and C++ respectively.