JavaRush /Java Blog /Random EN /System.out.println

System.out.println

Published in the Random EN group
Where do you start learning a programming language? Since writing the first program. Traditionally, the first program is called “Hello world”, and all its functionality consists of outputting the phrase “Hello world!” to the console. Such a simple program allows a new programmer to feel like something is working. System.out.println - 1

“Hello world” in different programming languages

The code will be different in different programming languages: Pascal “Hello world”

begin
  writeln ('Hello, world.');
end.
C “Hello world”

int main() {
   printf("Hello, World!");
   return 0;
}
C# “Hello world”

       static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
        }
Java "Hello world"

   public static void main(String[] args) {
        System.out.println("Hello World!");
    }
Despite the different code, all programs have a common command that directly outputs text to the console:
  • Pascal - writeln;
  • C - printf;
  • C# - System.Console.WriteLine;
  • Java - System.out.println.

Learn more about console output in Java

As you already understood, to output text to the console, in Java you need to use the command System.out.println(). But what does this set of characters mean? For those familiar with the Java language and basic OOP terms (for students who have taken the JavaRush course up to about level 15), the answer is obvious: “To output text to the console, we access the static field of the outclass Systemon which we call the method println(), and as argument we pass an object of the class String". If the meaning of the above is vague for you, then we’ll figure it out! This command consists of three words: System out println. Each of them represents some kind of entity that provides the necessary functionality for working with the console. System- an entity (in Java this is called a class) that acts as a “bridge” connecting your program to the environment in which it runs. out- the entity that is stored inside System. By default, refers to the console output stream. You can read more about I/O streams in Java here . println— a method that is called on the out entity to indicate the way in which information will be output to the console. Let's look at each element from this chain in more detail.

System

As already mentioned, Systemthis is a certain entity (class) that provides the developer with the ability to communicate with his environment: that is, the operating system under which the program is running. Since the console is an application installed inside the operating system (command line, Shell for Windows and Terminal for Linux), it becomes clear what the entity is used for System- to establish a connection between our program and the “outside world”. In addition to connecting to the console, it Systemhas other functionality:
  • Access to operating system environment variables:

    
    System.getenv("JAVA_HOME")
  • Returns the value of the JAVA_HOME environment variable, which is set in the OS system settings. When installing Java, you probably came across it;

  • Stopping the program immediately:

    
    System.exit(0)

    Interrupts program execution by stopping the Java Virtual Machine;

  • Get the line separator that is used on this operating system:

    
    System.lineSeparator()
  • Getting the current system time in milliseconds:

    
    System.currentTimeMillis();
    and many more useful functionality.
These examples are methods that perform specific actions. For example, they stop the program or return the requested value. In addition to methods, the class Systemcontains fields that store links to other entities:
  • out— the already familiar link to the essence of the stream of information output to the console;
  • in— a link to an entity that is responsible for reading input information from the console.
  • err- very similar out, but designed to display errors.
Knowing about these entities inside the class System, the programmer can use them for his own purposes. Java uses the “.” operator to refer to an element that is inside another element. Thus, to access the console output stream entity, you need to write the code:

System.out
Now let's figure out what this one is out.

out

outis the name of a variable that stores a reference to an object (entity) of type PrintStream. This object is configured in such a way that all information that is written to it ends up on the console. So, an object outis an instance of the class PrintStream, and you can call the corresponding methods on it:
  • print()— output of transmitted information. It can take numbers, strings, and other objects as arguments;
  • printf()- formatted output. Formats the passed text using special strings and arguments;
  • println()— output of transmitted information and line feed. It can take numbers, strings, and other objects as arguments;
  • Some other methods that we are not interested in in the context of this article.
As you can see, the difference between print()and println()is small. The only thing that is different println()is that it will add a new line instead of us, which makes it more popular print(). If we call the method three times print()with an argument of “Hello World!”, the result will be a line like this:

Hello World!Hello World!Hello World!
While the method println()will produce each output on a new line:

Hello World!
Hello World!
Hello World!
To call a method on an object, the familiar “.” operator is used. Thus, calling a method println()on the out entity looks like this:

out.println()

println

Like many other programming languages, Java's println is short for “print line.” We already know that println()this is a method that needs to be called on the entity out. If you are new to Java and programming in general, then methods are a certain set of commands that are logically combined. In our case, println()this is a block of commands that sends text to the output stream and adds a line break at the end. In Java, methods can take arguments. When we call a method, the arguments are passed inside parentheses.

println(Hello World!);
In turn, the code that is inside the method receives the text we passed and sends it to the output.

Let's build a logical chain

To output text to the console, a Java programmer needs to do the following:
  1. Contact an entity that can connect our application and the console - System;
  2. Access the console output stream - System.out;
  3. Call a method that writes information to the console - System.out.println;
  4. Send the text to be recorded -System.out.println(“Hello World!”);

Let's sum it up

Normal output to the console in Java starts a whole chain of calls to various objects and methods. Understanding what happens when calling the most used command in Java brings us a little closer to Java Guru status!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION