System out print in java
In Java, System.out.println()
is a method used to print output to the console. It is part of the System
class in the java.lang
package. The println()
method is commonly used to display text and values on the console, and it automatically appends a newline character at the end.
Example:
/*
* Author: Zameer Ali Mohil
* */
public class PrintExample {
public static void main(String[] args) {
// Using System.out.println() to print a string
System.out.println("Hello, World!");
// Printing variables and expressions
int x = 10;
double pi = 3.14159;
System.out.println("The value of x is: " + x);
System.out.println("The value of pi is: " + pi);
// Printing multiple lines
System.out.println("Line 1");
System.out.println("Line 2");
// Using escape characters
System.out.println("This is on line 3\nThis is on line 4");
// Printing with formatting
String name = "John";
int age = 25;
System.out.printf("Name: %s, Age: %d\n", name, age);
// Printing without newline using print()
System.out.print("This is on line 5");
System.out.print(", and this is on the same line.");
}
}
In this example:
System.out.println("Hello, World!");
prints the string “Hello, World!” to the console, followed by a newline.System.out.println("The value of x is: " + x);
prints a message along with the value of the variablex
.System.out.printf("Name: %s, Age: %d\n", name, age);
usesprintf
for formatted printing, where%s
is a placeholder for a string, and%d
is a placeholder for an integer.System.out.print("This is on line 5");
andSystem.out.print(", and this is on the same line.");
useprint()
without a newline character, so the second message is printed on the same line.
When you run this program, you’ll see the corresponding output in the console:
Hello, World!
The value of x is: 10
The value of pi is: 3.14159
Line 1
Line 2
This is on line 3
This is on line 4
Name: John, Age: 25
This is on line 5, and this is on the same line.
In Java, System.out.println()
is a method used to print output to the console. It is part of the System
class in the java.lang
package. The println()
method is commonly used to display text and values on the console, and it automatically appends a newline character at the end.
Example:
javaCopy codepublic class PrintExample {
public static void main(String[] args) {
// Using System.out.println() to print a string
System.out.println("Hello, World!");
// Printing variables and expressions
int x = 10;
double pi = 3.14159;
System.out.println("The value of x is: " + x);
System.out.println("The value of pi is: " + pi);
// Printing multiple lines
System.out.println("Line 1");
System.out.println("Line 2");
// Using escape characters
System.out.println("This is on line 3\nThis is on line 4");
// Printing with formatting
String name = "John";
int age = 25;
System.out.printf("Name: %s, Age: %d\n", name, age);
// Printing without newline using print()
System.out.print("This is on line 5");
System.out.print(", and this is on the same line.");
}
}
In this example:
System.out.println("Hello, World!");
prints the string “Hello, World!” to the console, followed by a newline.System.out.println("The value of x is: " + x);
prints a message along with the value of the variablex
.System.out.printf("Name: %s, Age: %d\n", name, age);
usesprintf
for formatted printing, where%s
is a placeholder for a string, and%d
is a placeholder for an integer.System.out.print("This is on line 5");
andSystem.out.print(", and this is on the same line.");
useprint()
without a newline character, so the second message is printed on the same line.
When you run this program, you’ll see the corresponding output in the console:
csharpCopy codeHello, World!
The value of x is: 10
The value of pi is: 3.14159
Line 1
Line 2
This is on line 3
This is on line 4
Name: John, Age: 25
This is on line 5, and this is on the same line.
System.out.println()
is a convenient method for displaying information during development and debugging and is widely used for simple console-based user interfaces.