A Simple Java program: printing a line of text.


“When faced with a decision i always ask, what would be the most fun,?”- Peggy Walker
We begin by considering a simple Java application that displays a line of text. An application is a program that executes using the Java interpreter.


//Author: Franklin Coetzee


// A first program in Java
public class Welcome1 {

public static void main(String[] args){

System.out.println(” welcome to Java programming”);

}//end main

}//end class

This program illustrates several important features of the java language.



  A simple way to make money. simply send traffic to this website and earn some money every time someone clicks on it! You could earn $1 for every click! 



We consider each line of the program in detail. Line7 does the real work of the program namely displaying the phrase “welcome to Java programming”
on the screen. But let us consider each line in order. Line1
begins with “//” , indicating that the remainder of the line is a comment.  We begin every program with a comment indicating file name.

As in C++, a comment that begins with “//” is called a  single line comment because the comment terminates at the end of the current line.

Java also supports C-style multiple line comments (delimited by /* and */). A similar form of comment called a documentation comment is delimited with /** and */.
Common programming error: Forgetting one of the delimiters of a multi line comment is a syntax error.
Java programmers generally use C++ style single line comment in preference to C style comments.

Java introduced the documentation comment syntax to enable programmers to highlight portions of programs that 

the javadoc utility program can read and use to prepare documentation for your program automatically.

There are subtle issues to using javadoc style comments properly in a program.
public class welcome1

begins a class definition for class welcome1.

Every program in Java consists of at least one class definition that is defined by you – the programmer.

These classes are known as programmer defined classes or user defined classes.

The class keyword introduces a class definition in Java and is immediately followed by the class name(welcome1 in this program).

Keywords are reserved for use with Java and are always spelled with all lowercase letters.

By convention, all class names in Java begin with a capital first letter and have a capital first letter for every word in the class name.

The name of the class is called the identifier. An identifier is a series of characters consisting of letters , digits, underscores and dollar signs that does not begin with a digit and does not contain any spaces.

Some valid identifiers are Welcome1, $value, _value, m_unprofitable and button7.

 The name button7 is not a valid identifier because it begins with a digit, and the name “input field” is not valid because it contains a space.

 Java is case- sensitive, uppercase and lowercase letters are different, so a1 and A1 are different in



  A simple way to make money. simply send traffic to this website and earn some money every time someone clicks on it! You could earn $1 for every click! 



 Common programming error : Java is case sensitive, so not using the proper uppercase or lowercase letters for an identifier is normally a syntax error.


 Good programming practice:By convention you should always begin a class name with a capital first letter.

 When reading a Java program, look for identifiers that start with capital first letters.

 These normally represent Java classes.


 Software engineering observation: avoid using identifiers containing dollar signs as these are often used by the compiler to create identifier names.


 Every class we define begins with a public keyword.

 For now we will simply require this keyword.

Several times in this text, we simply ask you to mimic certain Java features we introduce as you wrote your own Java programs.

We specifically do this when it is not yet important to know all the details of a feature to use that feature in Java.

All programmers initially learn how to program  by mimicking what other programmers have done before them.
When you save your class definition in a file, the class name must be used as part of the file name.

for our application the file name is welcome1.java.

All java class definitions are stored  in files ending with the .java file name extension.
Common programming error: For a public class, it is an error if the file name is not identical to the class name in both spelling and capitalisation.

Therefore it is also an error for a file to contain two or more classes.

It is an error to not end the file name with the .java extension, for a file containing an applications class definition.

The java compiler will not be able to compile the class definition.
A left brace{, begins definitionthe body of every class definition. A

A corresponding } must end every class definition.

Notice that line 5 to 8 are indented, this is a spacing convention to make the programs more readable.

We define each spacing convention as good programming practice.
Common programming error: if braces do not occur in matching pairs, the compiler indicates an error.
Good programming practice: Wheneveryou type an opening left brace, immediately type the closing right brace. 

Then reposition the cursor between the braces to begin typing the body.

This helps prevent missing braces.


Indent the entire body of each class definition one level of indentation between the left brace and the right brace that define the body class.

This emphasises the structure of the class definition and helps make the class definition easier to read.

Set a convention for the indent size you prefer, then uniformly apply that convention.

The Tab key may be used to create indents, but tab stops may vary between editors.

We recommend using either one quarter tab stops  or three spaces to form a level of indent.
public static void main(String[] args)
is a part of every java application.

Java applications automatically begin execution at main.

The parentheses after main indicate that main is a program method, or what a C or C++ programmer would call a function.

Java calls definitions usually contain one or more methods. For a Java application class, exactly one of those methods must be called main, and must be defined as shown in line 5. 

Otherwise the java interpreter will not execute the application.

Methods are able to perform tasks and return information when they complete their tasks.

The void keyword indicates that this method will perform a task, but will notreturn any information when it completes it’s task.

We will see that many methods will return information when they complete their tasks.

For now, simply mimic main’s first line in each of your Java applications.
The left brace in line6, begins the body of the method definition. A corresponding right brace must end the method definition body in line8.

Notice that the line in the body of the method is indented between these braces.
Good programming practice: indent the entire body of each method definition one level of indentation between the left brace and the right brace that define the body of the method.

This makes the structure of the body stand out and helps make the method definition easier to read.

System.out.println(“welcome to Java programming “);
instructs the computer to print the string of characters contained between the double quotation marks.

A string is sometimes called a character string, a message or a string literal.



  A simple way to make money. simply send traffic to this website and earn some money every time someone clicks on it! You could earn $1 for every click! 



We refer to characters between double quotation marks generically as strings.
Whitespace characters in strings are not ignored by the compiler.
System.out is known as the standard output object.

System.out allows java applications to display strings and other types of information in the command window from which the java application is executed.

On Microsoft windows 95/98, the command window is the MS-DOS prompt.

On Microsoft windows NT, the command window is the command prompt.On UNIX, the command window is normally called a command window, command prompt, a shell tool or a shell.

On computers running an operating system that does not have a command window (such as Macintosh ) , the java interpreter normally displays a window containing the information displayed by the program.
Method System.out.println displays a line of text in the command window.

When System.out.println completes it’s task, it positions the output cursor ( the location where the next character will be displayed) to the beginning of the next line in the command window(this is similar to you pressing the Enter key when typing in an text editor- the cursor is positioned at the beginning of the next line in your file)file

 The entire line, including System.out.println, it’s argument in the parentheses and the semi colon is called a statement.

 Every statement must end with a semicolon(also known as the statement terminator).

 When this statement executes, it displays” welcome to Java programming ” on the command window.


 Common programming error: omitting the semicolon at the end of a statement is a syntax error.


 Error prevention tip: When the compiler reports a syntax error, the error may not be on the line indicated by the error messages. First check the line where the error was reported. If that line does not contain syntax errors, check the preceding several lines in the program.


  A simple way to make money. simply send traffic to this website and earn some money every time someone clicks on it! You could earn $1 for every click! 



<a href=”http://propellerads.com/?rfd=TsUQ”><img src=”http://promo.propellerads.com/160×600-popads_1.gif&#8221; alt=”Propellerads”></a>

We are now ready to compile  and execute our program.

 To compile the program we open the command window, change to the directory where the program is stored and type


 javac welcome1.Java


 If the program contains no syntax errors, the preceding command creates a file called welcome1.class containing the java bytecodes that represent our application.

 These bytecodes will be interpreted by the Java interpreter when we tell it to execute the program by typing the command


 Java welcome1


 which launches the java interpreter and indicates that it should load the .class file for welcome1. Note that the .class filename extension is omitted from the preceding command.; otherwise the interpreter will not execute the program. The interpreter automatically calls method main.

 Next the statement at line7 of main displays “welcome to java programming”.

 Although this first program displays output in the command window, most java applications that display output use windows or dialog boxes to display output.

 For example, world wide web browsers such as Netscape communicator or Microsoft internet explorer display web pages in their own window.

 Email programs typically allow you to type messages in a window provided by the email program or read messages you receive in a window provided by the email program.

 Dialog boxes are windows that are typically used to display important messages to the user of an application. 

 Java8 already includes class JOptionPane that allows you to easy display a dialog box containing information.


 One of the great strengths of java 8 is it’s rich set of predefined classes that programmers can use rather than reinventing the wheel.We use a large number of these classes in java 8. Java’s many predefined classes are grouped into categories of related classes called packages.

 The packages are referred to collectively as the java class library or the java application programming interface(API). Class JOptionPane is defined for us in a package called javax.swing.



 Let’s look at this line:


 import javax.swing.JOptionPane;


 An import statement.The compiler uses import statements to identify and load classes required to compile a program. Whenyou use classes from the java API, the compiler attempts to ensure that you use them correctly.

 The import statements help the compiler locate the classes you intend to use.

 Each piece of the package name is a directory on disk. All the packages in the java API are stored in the directory java or javax that contain many subdirectories  including swing(a subdirectory of javax)

 The preceding line tells the compiler to load the JOptionPane class from the javax.swing package. This package contains many classes that help java programmers define graphical user interfaces (GUI) for their application.

 GUI components facilitate data entry by the user of your program and formatting or presenting data outputs to the user of your program. 

 Now let’s look here:




 indicates a call to method showMessageDialog of class JOptionPane. The method requires two arguments. When a method requires multiple arguments, the arguments are separated with commas. The first argument will always be the keyword null. The second argument is the string to display.


  A simple way to make money. simply send traffic to this website and earn some money every time someone clicks on it! You could earn $1 for every click! 



Good programming practice: Place a space after each comma in an argument list, to make programs more readable.
Method JOptionPane.showMessageDialog() is a special method of class JOptionPane called a static method. Such methods are always called using their class name followed by the dot operator and the method name.

Remember that all statements in java end with a semicolon. Java allows large statements to be split over many lines. However you cannot split a statement in the middle of an identifier or in the middle of a string.
Common programming error: splitting a statement in the middle of an identifier or a string is a syntax error.

System.exit(0);//terminate the program
uses static method exit() of class System to terminate the application. This line is required  in any program that displays a graphical user interface to terminate the application.

Notice once again the syntax used to call the method – the class name (System) , a dot and the method name (exit). Remember that identifiers starting with capital first letters normally represent class names. So you can assume that System is a class. 

The argument 0 to method exit() indicates that the application terminates successfully whereas a nonzero value indicates an error has occurred. This value is passed to the command window that executed the program. 

This is useful if the program is executed from a batch file or a shell script. Batch files and shell scripts are typically used to execute several programs in sequence such that when the first program ends, the next program begins execution automatically. 

For more information on batch files or shell scripts see your operating systems documentation.

Class System is part of the package java.lang. Notice that class System is not imported with an import statement at the beginning of the program. Package java.lang is imported in every Java program.
Common programming error: Forgetting to call System.exit in an application that displays a graphical user interface prevents the program from terminating properly. This normally results in the command window preventing you from typing any other commands.



  A simple way to make money. simply send traffic to this website and earn some money every time someone clicks on it! You could earn $1 for every click! 




Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s