First Java Application

So if you are a keen follower of this section of mine, u might now be well versatile with Object Oriented Programming Concepts and Objects and Classes in Java. This week is going to be more interactive for you. This is because you are going to create your own JAVA application. 

Java programs fall into two main groups: Applets and Applications. 

Applets, as you may have experienced, are Java programs that are downloaded over the World Wide Web and executed by a Web browser on the reader's machine. Applets depend on a Java-capable browser in order to run (although they can also be viewed using a tool called the applet viewer) 

Java applications are more general programs written in the Java language. Java applications don't require a browser to run, and in fact, Java can be used to create all the kinds of applications that you would normally use a more conventional programming language to create. HotJava itself is a Java application. 

A single Java program can be an applet or an application or both, depending on how you write that program and the capabilities that program uses. Throughout this article, you'll be writing mostly applications; then you'll apply what you've learned to write applets in the later articles. If you're eager to get started with applets, be patient. Everything that you learn while you're creating simple Java applications will apply to creating applets, and it's easier to start with the basics before moving onto the hard stuff. 

Creating a Java Application 

Let's start by creating a simple Java application. 

As with all programming languages, your Java source files are created in a plain text editor, or in an editor that can save files in plain ASCII without any formatting characters. On Unix, emacs, pico, or vi will work; on Windows, Notepad or DOS Edit are both text editors. 

Fire up your editor of choice, and enter the Java program shown in List a. Type this program, as shown, in your text editor. Be careful that all the parentheses, braces, and quotes are there. 

List a) Your first Java application: 

1)  /* 

2)  This is a simple Java program 

3)  Call this file "". 

4)  */ 

5)  class Example{ 

6)  //Your program begins with a call to main( ). 

7)  Public static void main (String args [ ] ) { 

8)  System.out.println ( "This is a simple Java program."); 

9)  } 

10)  } 

Warning: The numbers before each line are part of the listing and not part of the program; they're there so I can refer to specific line numbers when I explain what's going on in the program. Do not include them in your own file. 

Entering the Program: 

In Java, unlike other programming languages, the name of the source file should be followed by the .java (dot java) extension. In Java the source of a file is officially called a compilation unit. 

It is a text file that contains one or more class definitions. The Java compiler requires that a source file use the .java filename extension. 

Compiling the program: 

To compile the example program, execute the compiler, javac, specifying the name of the source file on the command line as, shown below: 


The javac compiler creates a file called Example.class that contains the bytecode version of the program. 

To actually run the program, you must use the java interpreter, called java. It is as shown below: 

C:>java Example 

When the program is run, the following output is displayed: 

This is a simple Java program. 


LINES1) to 4): This is a comment and the compiler ignores the contents of the comment. It is a multi-line type of comment.

LINE5): This line uses the keyword class to declare that a new class is being defined. Example is an identifier that is the name of the class. Entire class definition including all of its members will be enclosed in the curly brackets.

LINE 6): It is a single line comment supported by java.

LINE7): This line begins the main ( ) method. This is the line at which the program will begin executing. All java applications begin execution by calling main ( ).The public keyword is an access specifier, which allows the programmer to control the visibility of the class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. The keyword static allows main( ) to be called without having to initiate a particular instance of the class. The keyword void simply tells the compiler that main( ) does not return a value. String args [ ] declares a parameter named args which is an array of instances of the class string. Objects of type strings store character strings.

LINE8): This line outputs the string " This is a simple Java program." Followed by a new line on the screen. In this case, println prints the string which is passed to it. Note that the println statement ends with a semicolon. All statements in Java end with a semicolon. The reason that the other lines in the program do not end in a semicolon is that they are not, technically, statements.

LINE 9): It ends main.

LINE 10): It ends the Example class definition. 

In the forthcoming articles we will have a look at some of the applets in JAVA.  

Next: Your First Java Applet


More by :  Deepak Chandrasekaran

Top | Computing

Views: 3327      Comments: 0

Name *

Email ID

Comment *
Verification Code*

Can't read? Reload

Please fill the above code for verification.