System.out.println() in Java26 Oct 2024 | 4 min read System.out.println() is a main method applied in the console output, provided when debugging and for user instructions purposes. It is part of the System class of the java.lang package, and it is inherited by all Java programs without importing any packages. Here's a breakdown of each component. System: This class is part of the Java standard library (java.lang package) that provides the standard input, standard output, error output data streams, and similar system resources. out: This is a regular and class System attribute, being the system output stream. It's an example of PrintStream (Interface providing the capability for putting data into various output streams). println(): This means the PrintStream object is doing the task. Here is an example that outputs "Hi, World!" to System.out. The println() method tries the data along with a newline character that comes by default, so it can be used to display messages in a readable way. Usually, a method is invoked by objectname.methodname(). But we cannot create the object to the PrintStream class directly, as above. So, Java provides an alternative way to create the object of PrintStream class that is System.out. Where System is the class name, it is declared as final. The out is an instance of the System class and is of type PrintStream. Its access specifiers are public and final. It is an instance of java.io.PrintStream. When we call the member, a PrintStream class object creates internally. So, we can call the print() method, as shown below: It creates the PrintStream class object. This object, by default, represents the output device, i.e., the monitor. ![]() ExampleIn the following example, we have used two print() methods, which gives the result in one line. It means the first print() method displays the string "Hello!" and retains the cursor at the same line. The second print() method also displays the string "Java" at the same line adjacent to the previous string. PrintExample.java Output Hello!Java Java println() methodThe println() method is similar to print() method except that it moves the cursor to the next line after printing the result. It is used when you want the result in two separate lines. It is called with "out" object. If we want the result in two separate lines, then we should use the println() method. It is also an overloaded method of PrintStream class. It throws the cursor to the next line after displaying the result. ExampleThe following example, the println() method display the string in two separate lines. Demo.java Output Hello! Java Difference Between print() and println() MethodsBoth methods are used to display the results on the monitor. The print() method displays the result on the console and retains the cursor in the same line. It works only with an argument. The println() method also displays the result on the console but moves the cursor to the next line. It can also work without arguments. PrintExample.java Output: Hello, world!How are you? I'm fine, thank you! Just like System.out for standard output, there are two other standard streams: System.in and System.err, known as standard input and standard error, respectively. System.in:System.in is an instance of the InputStream class. It usually corresponds to the input from the keyboard. It enables you to get data from a standard input stream, for example, user inputs during runtime. You can use classes such as Scanner or BufferedReader together with System.in to read input from the user. System.err:System.err is a PrintStream object like System.out. It is the symbol for the standard error output stream. It is used to print error messages, diagnostics, and any other related information that connotes an error. Unlike System.out, System.err is usually utilized for reporting errors and is not redirected by default when you redirect standard output. Next TopicJava Tutorial |
Merge Two Sorted Linked Lists in Java
Combining two sorted linked lists is one of the fundamental problems that must be solved when learning about algorithms. It is a process of uniting two sorted lists so that once integrated, the resulting list still remains sorted. This problem is usually presented as a coding challenge...
5 min read
Multithreading Hierarchy in Java
The multithreading capability of Java is an effective tool that enhances program performance and resource economy by enabling many threads to run simultaneously. The idea of thread hierarchies, which offer an organized method of managing concurrent work, is the foundation of Java's threading paradigm. This section...
5 min read
Cast Generic Type to Specific type Java
Java is a programming language that supports the development of generic classes and methods. Java's generics feature enables programmers to design code that can operate on several object types without the use of typecasting. A generic type must occasionally be changed into a specific type, though....
4 min read
Immutable List in Java
In Java, an immutable list is a list that cannot be modified once it is created. An attempt to add, remove, or modify elements in the List after it is created will throw an exception. The primary benefit of using immutable lists is that they provide thread...
11 min read
Goal Stack Planning Program in Java
Goal Stack Planning is a popular artificial intelligence technique used for planning and problem-solving tasks. It involves breaking down complex tasks into a series of sub-goals and actions to achieve those goals. In this article, we will explore the concept of Goal Stack Planning and implement...
12 min read
DecimalStyle toString() Method in Java with Examples
The java.time.format.DecimalStyle class toString() method. To obtain this DecimalStyle's String value in Java, utilize the DecimalStyle class. The String value is represented by a String that is returned by this function. Syntax: public String toString() Parameter: No parameters are taken by this main method. Return Value: The...
2 min read
Image Processing in Java - Contrast Enhancement
Digital image analysis and computer vision both heavily rely on image processing. In order to obtain the intended results, this calls for image alteration. Contrast enhancement is a fundamental method of image processing that highlights the things in the image so that they are more...
8 min read
CollationElementIterator setText(CharacterIterator) method in Java with Example
The java.text.CollationElementIterator classes contain setText() function. The new source string for the CollationElementIterator object to iterate is set using the CollationElementIterator class. For locale-sensitive string sorting, the CollationElementIterator and RuleBasedCollator work together to extract collation elements for text comparison. We can add new text to the collation...
3 min read
ArrayIndexOutOfBoundsException
in Java The occurs whenever we are trying to access any item of an array at an index which is not present in the array. In other words, the index may be negative or exceed the size of an array. The is a subclass of...
2 min read
Web crawler Java
The web crawler is basically a program that is mainly used for navigating to the web and finding new or updated pages for indexing. The crawler begins with a wide range of seed websites or popular URLs and searches depth and breadth to extract hyperlinks. The web...
10 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
