The Basic Structure of a Simple Java program
By: PriyaBabu in Java Tutorials on 2006-12-11
Let us see now how the simple java program will look like.
You can use any editor like notepad or any Java IDE for writing java programs.
/* This is a sample java program Save this file as Welcome.java */ class Welcome { // A java program will start from here. public static void main(String args[]) { System.out.println(" Welcome to Java-Samples!!! "); } }
For those who are using the IDE, you can follow the instructions have given by there.
Suppose if you are entering your program in notepad, then save this file as Welcome.java
Compiling the program
After we have written our program we need to compile and run the program. For that we need to use the compiler called javac which is provided by java.
Go to the command prompt and type the file name as shown here.
c:\>javac Welcome.java
The javac compiler will create a class file called Welcome.class that contains only bytecodes. These bytecodes have to be interpreted by a Java Virtual Machine(JVM) that will convert the bytecodes into machine codes. Once we successfully compiled the program, we need to run the program in order to get the output. So this can be done by the java interpreter called java. In the command line type as shown here.
c:\>java Welcome
So the output will be displayed as
Welcome to Java-Samples!!!
As we had seen above, when the source code has been compiled , it creates a class file with a extension of .class. Since this class file contains the bytecodes that can be interpreted by the JVM which can be resided at any platform. Remember that while running the program we are using only .class file but not the .java file. So once you got the class file you can run the same java program at any platform instead of writing the program again and again. This is the very special feature about java that 'Write once and Run anywhere'.
Looking into the program line by line
Let us closely examine each part of the program.
/* This is a sample java program Save this file as Welcome.java */
This is called comment. This is for us to enter the comments about the program for our own convenience. The contents of a comment will be ignored by the compiler. Actually java supports three styles of comments. The above one is called multi-line comment which may contain several lines. This type of comment must begin with /* and end with */.
The next line of the code in a program is
class Welcome {
The word class is a keyword to define a new class and Welcome is a name of the class. The class definition must begins with opening curly brace ({) and ends with closing curly brace (}). The rest of the things defined inside these braces are called member of the class. And note that all the program activities are defined inside the class.
// A java program will start from here.
This is another type of comment. This is called single line comment starts with // and ends with end of the line. Generally we use it for brief comments.
The next line of the code in a program is
public static void main(String args[])
This line begins with main method as like functions or subroutines in other languages. The program will start execute by calling this main method. Let us see briefly about the other attributes declared in main method. However we are going to discuss in detail about this in later chapters.
The keyword public is an access specifier. The keyword static is a kind of modifier. The keyword void means that the method main() does not return any value. As we had seen before all the java program will start execute by calling the main method. If we want to pass any information to a method will be received by the variables declared within the parenthesis is called parameters. In a main() method there is only one parameter ,String args[] . args[] is a name of the parameter that is an array of the objects of data type String. String store sequences of characters and args will receive the command line arguments.
All the method in java must be start with opening curly brace ({) and ends with closing curly brace (}).
The next line of the program is
System.out.println(" Welcome to Java-Samples!!! ");
Understanding of each keyword used here will be difficult now, so just take it as this System.out.println helps to display the output in the command line. As you have probably noticed, the System.out.println statement ends with ;. All statements in java must end with semicolon. And remember that java is case sensitive. So we should be very careful about cases while coding the program. Otherwise it will lead to the serious problems.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Comments