DEV Community

Cover image for A Simple Java Program
Paul Ngugi
Paul Ngugi

Posted on

A Simple Java Program

public class Welcome {
 public static void main(String[] args) {
 // Display message Welcome to Java! on the console
 System.out.println("Welcome to Java!");
 }
}
Enter fullscreen mode Exit fullscreen mode

Line 1 defines a class. Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome.

Line 2 defines the main method. The program is executed from the main method. A class may contain several methods. The main method is the entry point where the program begins execution.

A method is a construct that contains statements. The main method in this program contains the System.out.println statement. This statement displays the string Welcome to Java! on the console (line 4). String is a programming term meaning a sequence of characters. A string must be enclosed in double quotation marks. Every statement in Java ends with a semicolon (;), known as the statement terminator.

Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in this program are public, static, and void.

Line 3 is a comment that documents what the program is and how it is constructed. Comments help programmers to communicate and understand the program. They are not programming statements and thus are ignored by the compiler. In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and **/* on one or several lines, called a block comment or paragraph comment. When the compiler sees //, it ignores all text after // on the same line. When it sees /, it scans for the next */* and ignores any text between /* and **/*. Here are examples of comments:

// This application program displays Welcome to Java!
/* This application program displays Welcome to Java! */
/* This application program
 displays Welcome to Java! */
Enter fullscreen mode Exit fullscreen mode

A pair of curly braces in a program forms a block that groups the program’s components. In Java, each block begins with an opening brace ({) and ends with a closing brace (}). Every class has a class block that groups the data and methods of the class. Similarly, every method has a method block that groups the statements in the method. Blocks can be nested, meaning that one block can be placed within another, as shown in the following code.

public class Welcome {
 public static void main(String[] args) {
 System.out.println("Welcome to Java!");
}
}
Enter fullscreen mode Exit fullscreen mode
Welcome to Java!
Enter fullscreen mode Exit fullscreen mode

An opening brace must be matched by a closing brace. Whenever you type an opening brace, immediately type a closing brace to prevent the missing-brace error. Most Java IDEs automatically insert the closing brace for each opening brace.

Java source programs are case sensitive. It would be wrong, for example, to replace main in the program with Main.

Like any programming language, Java has its own syntax, and you need to write code that conforms to the syntax rules. If your program violates a rule—for example, if the semicolon is missing, a brace is missing, a quotation mark is missing, or a word is misspelled—the Java compiler will report syntax errors.

Once you understand the program, it is easy to extend it to display more messages:

public class WelcomeWithThreeMessages {
 public static void main(String[] args) {
 System.out.println("Programming is fun!");
 System.out.println("Fundamentals First");
 System.out.println("Problem Driven");
 }
}
Enter fullscreen mode Exit fullscreen mode
Programming is fun!
Fundamentals First
Problem Driven
Enter fullscreen mode Exit fullscreen mode

Further, you can perform mathematical computations and display the result on the console.

public class ComputeExpression {
 public static void main(String[] args) {
 System.out.println((10.5 + 2 * 3) / (45 – 3.5));
 }
}
Enter fullscreen mode Exit fullscreen mode
0.39759036144578314
Enter fullscreen mode Exit fullscreen mode

Top comments (0)