out in System.out.println()
System.out is initialized to null
when the class is instantiated. This is set by the nullPrintStream()
method in System.java
, which just returns null
.
When the JVM has initialized, it calls the initializeSystemClass()
method. This method calls the native
method setOut0()
which sets the out
variable to the appropriate value.
This may seem weird but it is a necessary operation for the following reasons:
out
cannot be set statically to the value becauseSystem
needs to be one of the first loaded classes (beforePrintStream
).out
must befinal
so that its value cannot be directly overridden by a user.- Since
out
cannot be set statically, and is final, we must override the semantics of the language using anative
method,setOut0()
.
I hope that helps your understanding.
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 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());
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).
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();
}
}
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.
Related Topics
Java Naming Convention with Acronyms
Why Shouldn't Java Enum Literals Be Able to Have Generic Type Parameters
Why Is This Java Code in Curly Braces ({}) Outside of a Method
Jsoup Cookies for Https Scraping
Why Should One Use Objects.Requirenonnull()
What Is the Idiomatic Way to Compose a Url or Uri in Java
In Java 8, Why Is the Default Capacity of Arraylist Now Zero
Java Code for Getting Current Time
How to Copy the Contents of One Arraylist into Another
What Is Mutex and Semaphore in Java? What Is the Main Difference
What Is the Most Recommended Way to Store Time in Postgresql Using Java
How to Read a Text-File Resource into Java Unit Test
Export PDF Pages to a Series of Images in Java
Itext - Add Content to Existing PDF File
Differencebetween a Hashmap and a Treemap
Qr Code Encoding and Decoding Using Zxing