If you’re just getting started with Java, one of the first tasks you’ll encounter is printing output on the screen. This is done using the System.out.println()
method. In this post, we’ll write a Java program to display welcome messages such as:
- Welcome to Java
- Learning Java Now
- Programming is fun
This example is ideal for absolute beginners learning basic Java syntax and console output.
Understanding the Program
Before we look at the code, let’s break down what this Java program does:
- It uses the
main()
method, which is the entry point of any Java program. - The
System.out.println()
method is used to print text followed by a newline.
Java Program to Display Welcome Messages
public class WelcomeMessages {
public static void main(String[] args) {
System.out.println("Welcome to Java");
System.out.println("Learning Java Now");
System.out.println("Programming is fun");
}
}
Output:
Welcome to Java
Learning Java Now
Programming is fun
Explanation of the Java Program
- Class Declaration:
public class WelcomeMessages
defines the class name. - Main Method:
public static void main(String[] args)
is the starting point of Java execution. - Print Statements: Each
System.out.println()
prints one line of text to the console.
Why Learn This?
This simple Java program to display welcome messages is a great introduction to:
- Java syntax and structure
- Console output using
System.out.println
- How Java programs are compiled and executed