In the previous lesson, you saw an example of println() which is used to output text (information) on the screen. It is very important to wrap the message in double quotes. The following example will output “Hello, world!” to the screen.

public class Main{
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Multiple println

You can add as many println statements as needed.

public class Main{
    public static void main(String[] args) {
        System.out.println("Hello, world!");
        System.out.println("I am learning java programming.");
        System.out.println("It's really fun!");
    }
}

Each of the println statements will be printed on a separate line.

print()

If you wish to print without inserting a new line at the end of the message you’re printing, you can use print() instead of println()

public class Main{
    public static void main(String[] args) {
        System.out.print("Hello, world!");
        System.out.print("I am learning java programming.");
    }
}

“Hello, World!” and “I am learning java programming” will display next to one another as “Hello, World!I am learning java programming”. Notice that between “!” and “I” there is no space! We can fix this by inserting a blank space after “!” or before “I”.

public class Main{
    public static void main(String[] args) {
        System.out.print("Hello, world!");
        System.out.print(" I am learning java programming.");
    }
}
public class Main{
    public static void main(String[] args) {
        System.out.print("Hello, world! ");
        System.out.print("I am learning java programming.");
    }
}

Adding Sentences

You can also add sentences together within print and println methods, referred to as concatination. The following code will output to the screen “Hello, world! I am learning java programming.”

public class Main{
    public static void main(String[] args) {
        System.out.print("Hello, world! " + "I am learning java programming.");
    }
}

In this specific example, it is similar to writing the following code

public class Main{
    public static void main(String[] args) {
        System.out.print("Hello, world! I am learning java programming.");
    }
}

So why use the addition sign “+” ? We will see more useful examples in the following section.

Printing Numbers

You can also print numbers using println and print. Numbers can be within quotations or outside quotations.

public class Main{
    public static void main(String[] args) {
        System.out.println("12");
        System.out.println(12);
    }
}

You can also carry out some operations with numbers. The following example will add 5 and 2 and display 7 and then subtract 2 from 5 and display 3.

public class Main{
    public static void main(String[] args) {
        System.out.println(5 + 2);
        System.out.println(5 - 2);
    }
}

You can also mix and match numbers and words in print statements. Note that in this case, the ‘+’ sign refers to concatination and not to mathematical addition (i.e: it will put 18 next to the blank space after am and then place blank space years old. after 18).

public class Main{
    public static void main(String[] args) {
        System.out.println("Hello I am " + 18  + " years old.");
    }
}

In the following code, we will get the message “Hello I am 182 years old”. 18 and 2 will not be summed up but rather concatinated.

public class Main{
    public static void main(String[] args) {
        System.out.println("Hello I am " + 18 + 2  + " years old.");
    }
}

To force them to sum up, you should wrap 18 + 2 in parentheses (). The following code will generate “Hello I am 20 years old.”

public class Main{
    public static void main(String[] args) {
        System.out.println("Hello I am " + (18 + 2)  + " years old.");
    }
}

There is no need to add parentheses when using other operations such as multiplication, subtraction, and division. We can also print variable values. We will learn more about these in later lessons.

Other Lessons in Introduction to Programming in Java

No. Lesson Reading Time
1 Introduction to Java 5 mins
2 Setting up the environment in Java 5 mins
3 Java Hello World Program 5 mins
4 Java Output 5 mins
5 Java Escape Characters 5 mins
6 Variables & Types 5 mins
7 Arithmetic Operators in Java 5 mins

Related Subjects

or view all subjects
No realted subjects found.