Output in Java

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 10:- Output in Java

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:

  1. Using 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!

  1. Using 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!

  1. Printing variables:

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

  1. Formatting output using 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

  1. Printing multiple lines of text:

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.

3. Input And Output Data Types

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support