Print Text and Java Output Numbers Example

Java Print Text

Print Text and Java Output Numbers Example
Print Text and Java Output Numbers Example

Sure, I can help you with that. Here's an example Java program that prints some text and outputs some numbers:


public class PrintExample {

    public static void main(String[] args) {

        System.out.println("Hello World!"); // prints "Hello World!" to the console

        int x = 5;

        double y = 3.14159;

        System.out.println("The value of x is " + x); // prints "The value of x is 5"

        System.out.println("The value of y is " + y); // prints "The value of y is 3.14159"



When you run this program, it should output the following:


Hello World!

The value of x is 5

The value of y is 3.14159

I hope this helps! Let me know if you have any other questions.

Double Quotes

When you are the working with text, it must be the wrapped inside double quotations the marks "".

If the you forget the double quotes, an the error occurs:

system.out.println("This sentence will work!");

System.out.println(This sentence will produce an error);

The Print() Method:

There is also the a print() method, which is similar to the  println().

The only difference is that it the does not insert a new line at the end of the output:

System.out.print("Hello World! ");

System.out.print("I will print on the same line.");

Note that we the add an extra space the (after "Hello World!" in the example above), for better the readability....

In this tutorial, we will the only use println() as it makes it easier to the read the output of code.

Java Output Numbers:

Print Numbers

You can also use the println() method to the print numbers.

How ever, unlike text the, we don't put the numbers inside double the quotes:

public class Main {

  public static void main(String[] args) {






You can also the perform mathematical the calculations inside the println() method:


System.out.println(3 + 3);


System.out.println(2 * 5);

Post a Comment

Post a Comment (0)

Previous Post Next Post