How does System.out.print() work?
System.out
is just an instance of PrintStream
. You can check its JavaDoc. Its variability is based on method overloading (multiple methods with the same name, but with different parameters).
This print stream is sending its output to so called standard output.
In your question you mention a technique called variadic functions (or varargs). Unfortunately that is not supported by PrintStream#print
, so you must be mistaking this with something else. However it is very easy to implement these in Java. Just check the documentation.
And if you are curious how Java knows how to concatenate non-string variables "foo" + 1 + true + myObj
, it is mainly responsibility of a Java compiler.
When there is no variable involved in the concatenation, the compiler simply concatenates the string. When there is a variable involved, the concatenation is translated into StringBuilder#append
chain. There is no concatenation instruction in the resulting byte code; i.e. the +
operator (when talking about string concatenation) is resolved during the compilation.
All types in Java can be converted to string (int
via methods in Integer
class, boolean
via methods in Boolean
class, objects via their own #toString
, ...). You can check StringBuilder's source code if you are interested.
UPDATE: I was curious myself and checked (using javap) what my example System.out.println("foo" + 1 + true + myObj)
compiles into. The result:
System.out.println(new StringBuilder("foo1true").append(myObj).toString());
Why does System.out.println() in Java print to the console?
My doubt is when we do
System.out.println()
in our code, why it ends up in writing to console?
In any POSIX compliant shell, each process gets three "standard" streams when the shell starts it:
- The "standard input" stream is for reading input.
- The "standard output" stream is for writing ordinary output.
- The "standard error" stream is for writing error output.
(The same idea is also used in many non-POSIX compliant shells as well.)
For an interactive POSIX shell, the default is for these streams to read from and write to the shell's "console" ... which could be a physical console, but is more likely to be a "terminal emulator" on the user's (ultimate) desktop machine. (Details vary.)
A POSIX shell allows you to redirect the standard streams in various ways; e.g.
$ some-command < file # read stdin from 'file'
$ some-command > file # write stdout to 'file'
$ some-command 2> file # write stderr to 'file'
$ some-command << EOF # read stdin from a 'here' document
lines of input
...
EOF
$ some-command | another # connect stdout for one command to
# stdin for the next one in a pipeline
and so on. If you do this, one or more of the standard streams is NOT connected to the console.
Further reading:
- "What are stdin, stdout and stderr on Linux?"
- "Standard Streams"
So how does this relate to the question?
When a Java program start, the System.in/out/err
streams are connected to the standard input / output / error streams specified by the parent process; typically a shell.
In the case of System.out
, that could be the console (however you define that) or it could be a file, or another program or ... /dev/null
. But where the output goes is determined by how the JVM was launched.
So, the literal answer is "because that is what the parent process has told the Java program to do".
How internally shell communicates with jvm to set standard input / output in both Windows and Linux?
This is what happens with Linux, UNIX, Mac OSX and similar. (I don't know for Windows ... but I imagine it is similar.)
Suppose that the shell is going to run aaa > bbb.txt
.
- The parent shell forks a child process ... sharing the parent shell's address space.
- The child process closes file descriptor 1 (the standard output file descriptor)
- The child process opens "bbb.txt" for writing on file descriptor 1.
- The child process execs the "aaa" command, and it becomes the "aaa" command process. The file descriptors 0, 1, and 2 are preserved by the exec call.
- The "aaa" command starts ...
When the "aaa" command starts, it finds that file descriptors 0 and 2 (stdin and stderr) refer to the same "file" as the parent shell. File descriptor 1 (stdout) refers to "bbb.txt".
The same thing happens when "aaa" is the java
command.
System.out.print vs. System.out.println (Last sentence)
The only difference between println
and print
method is that println
throws the cursor to the next line after printing the desired result whereas print
method keeps the cursor on the same line.
More information
How to use System.out.println in Java
You should use the print()
method from the System
static class. Here is the Oracle Java documentation considering the PrintStream class, which is accessed through the System.out
call (out is a PrintStream
there).
Here is a working sample:
public class Something {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
char c1,c2;
c1 = s.findWithinHorizon(".", 0).charAt(0);
c2=s.findWithinHorizon(".", 0).charAt(0);
System.out.print(c1);
System.out.print(c2);
s.close();
}
}
What is System, out, println in System.out.println() in Java
The first answer you posted (System is a built-in class...) is pretty spot on.
You can add that the System
class contains large portions which are native and that is set up by the JVM during startup, like connecting the System.out
printstream to the native output stream associated with the "standard out" (console).
What's the meaning of System.out.println in Java?
No. Actually out
is a static member in the System
class (not as in .NET), being an instance of PrintStream
. And println
is a normal (overloaded) method of the PrintStream
class.
See http://download.oracle.com/javase/6/docs/api/java/lang/System.html#out.
Actually, if out
/err
/in
were classes, they would be named with capital character (Out
/Err
/In
) due to the naming convention (ignoring grammar).
java Why we write System with out.print()
Because that's the way we access to static
members of a class. In this case, the class is System
and its static
member out
is declared as
public final static PrintStream out = null;
Since it's public
we can access to it directly, but using the notation SomeClass.someStaticMember
System.out
and if we want to call a method of that object (because that member is an object), we have to call it as
System.out.println()
Related Topics
Include External Jar When Running Java -Jar
Why Is Super Class Constructor Always Called
Writing in the Beginning of a Text File Java
How to Include External Jar on My Netbeans Project
How to Set an Environment Variable at Runtime from Java
@Valid When Creating Objects with Jackson Without Controller
When Using Mokito, Differencebetween the Actual Object and the Mocked Object
Process Thymeleaf Variable as HTML Code and Not Text
Why Doesn't Java.Util.Collection Implement the New Stream Interface
Convert Two Dimensional Array to List in Java
Whats the Use of Saying <? Extends Someobject> Instead of <Someobject>
How to Add New Methods to the String Class in Java
Get Key Press Without Pressing Enter in Console
Java List Sorting: How to Keep a List Permantly Sorted Automatically Like Treemap