If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Java, you can display output to the console or command-line interface using the System.out
object, which is an instance of java.io.PrintStream
. The most commonly used method for outputting text is System.out.println()
, which prints a line of text followed by a newline character. Here are some ways to perform output in Java:
System.out.println()
:javaCopy code
public class OutputExample {
public static void main(String[] args) {
System.out.println("Hello, World!"); // Print a line of text with a newline
}
}
Output:
Copy code
Hello, World!
System.out.print()
to print without a newline:javaCopy code
public class OutputExample {
public static void main(String[] args) {
System.out.print("Hello, ");
System.out.print("World!"); // Print without a newline
}
}
Output:
Copy code
Hello, World!
javaCopy code
public class OutputExample {
public static void main(String[] args) {
int age = 25;
double height = 1.75;
System.out.println("Age: " + age + ", Height: " + height);
}
}
Output:
yamlCopy code
Age: 25, Height: 1.75
String.format()
:javaCopy code
public class OutputExample {
public static void main(String[] args) {
int number = 42;
String name = "Alice";
System.out.println(String.format("Number: %d, Name: %s", number, name));
}
}
Output:
yamlCopy code
Number: 42, Name: Alice
javaCopy code
public class OutputExample {
public static void main(String[] args) {
System.out.println("Line 1");
System.out.println("Line 2");
}
}
Output:
mathematicaCopy code
Line 1
Line 2
Remember that System.out
is just one way to perform output in Java. Depending on your application, you may also write output to files or other output streams using various Java classes. Additionally, modern Java frameworks and applications often provide more sophisticated ways of handling output, such as logging libraries. However, System.out
remains the simplest and most commonly used method for basic console output.
Comments: 0