The Basic Structure of a Simple Java program

By: PriyaBabu Emailed: 1698 times Printed: 2203 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.

Now that you know how a basic program in Java looks like, you can proceed to learn Basics of Java through Sample Programs


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(59)


1. View Comment

i want to study java

View Tutorial          By: doshyo at 2007-12-13 07:41:03
2. View Comment

Hi
I am newly in java. I have one dought. Inmy program i have a list box and text box. When i selected one item in a list box, that item's related value will present in the text box. how will i programed in java. Please help me immly.

In list box, book details(eg) java, oracle, vb... I have maintained table its contains the price list of those books. (eg)java-$200,oracle-$150,....
when I pick java from list box, the price amount of java will automatically displayed in text box. how?

thanks
Dina


View Tutorial          By: Dinakar at 2008-07-26 01:13:08
3. View Comment

i m new in java plz send some programs on packages and abstract class

View Tutorial          By: zainul hasan mirza at 2009-08-31 02:28:32
4. View Comment

i m new in java plz send some programs of java using functions and pointers.................
separately ............
plz send immediately .....

thanks,
supriyo


View Tutorial          By: supriyo at 2009-09-02 02:03:38
5. View Comment

i m new in java plz send package of simple programe

View Tutorial          By: iqbal ahmad at 2009-09-07 01:26:41
6. View Comment

i want to know about simple java structure with brief discription such about documentation section, package statement ....etc.
And thank a lot for getting more idea about java.


View Tutorial          By: Rambahaz at 2009-09-07 06:41:25
7. View Comment

Hi, this blog answers my questions but what is the difference between a class file (.class extension) and a jar file (.jar extension file)

View Tutorial          By: Edu at 2009-09-20 00:19:27
8. View Comment

its gud 2 learn java sim prog

View Tutorial          By: Rakesh at 2009-10-04 03:47:01
9. View Comment

I wont to know about every thing of java

View Tutorial          By: susantha at 2009-10-11 09:35:40
10. View Comment

plz send me java prograams

View Tutorial          By: simmy at 2009-10-26 01:57:55
11. View Comment

please send me Java programs

View Tutorial          By: Jayita at 2009-11-06 08:19:59
12. View Comment

I want java

View Tutorial          By: A.S.Vtihyatharan at 2009-12-01 03:25:36
13. View Comment

The basic structure of simple program is used to learn the java environment and how to compile and run the java program.

View Tutorial          By: Muthusasi at 2009-12-17 21:10:01
14. View Comment

i wish to get some example java programs to understand more clearly

View Tutorial          By: rebecca at 2010-01-09 03:14:42
15. View Comment

i need java simple programs

View Tutorial          By: Tharani at 2010-01-22 06:39:25
16. View Comment

i want to learn the java from basics onwards.

View Tutorial          By: vinay at 2010-03-08 22:54:15
17. View Comment

Hi
I am newly in java. I have one dought. Inmy program i have a list box and text box. When i selected one item in a list box, that item\'s related value will present in the text box. how will i programed in java. Please help me immly.

In list box, book details(eg) java, oracle, vb... I have maintained table its contains the price list of those books. (eg)java-$200,oracle-$150,....
when I pick java from list box, the price amount of java will automatically displayed in text box. how?

thanks
Saman
-Sri lanka


View Tutorial          By: Saman at 2010-08-03 20:34:38
18. View Comment

hi am new to java! i need some simple java programs immediately pls..........................

View Tutorial          By: Anu at 2010-09-08 08:17:49
19. View Comment

hi iam new to java i need simple java programs pls

View Tutorial          By: naga at 2010-10-06 03:45:18
20. View Comment

can i have a piece of program so that i can improve may skills in java?

View Tutorial          By: miniboss at 2010-11-12 06:09:41
21. View Comment

i am newly in java, i want to learn about this and please send me a simple program or a basic java program

View Tutorial          By: gelen at 2010-11-23 02:57:15
22. View Comment

i am new to java, please send simple programs, and basic structures of java

View Tutorial          By: jegan at 2011-07-11 02:22:37
23. View Comment

hi
I have to learn java using NetbeansIDE . Pls send me some programs using packages......


View Tutorial          By: Ekanki at 2011-08-04 10:30:36
24. View Comment

java is new for me..... since i dont know from basic.... so kindly help and tel me how to write the simple coding

View Tutorial          By: m.padma at 2011-08-08 15:40:23
25. View Comment

hi i am studying java course.i am new to java, please send simple programs, and basic structures of java.

View Tutorial          By: Rajeshkumar at 2011-11-14 08:30:29
26. View Comment

Hi,
I'm studying core java.java is new for me..... since i dont know from basic.... so kindly help and tel me how to write the simple coding

Thank you
Shantkumar


View Tutorial          By: shantkumar at 2011-12-01 17:02:26
27. View Comment

Hi , I am mew to java please can u send detail how the program stores when typed in notepad and it creates packages and structure.... please send in detail with documents

View Tutorial          By: ramanji at 2011-12-11 05:36:41
28. View Comment

Currently am working in Perl, lik to learn Java, need basic tutorials to migrate in corporate industry.

View Tutorial          By: KUMARESAN at 2011-12-14 05:08:47
29. View Comment

Hi...i know java but little....
give simple programs of Java with details


View Tutorial          By: Murshidha at 2012-02-06 09:52:52
30. View Comment

Hi, i am new java, and i want to become gud developer so please guide me what should i do for it, and from where i should start it , please tell me immediately.


Thanks.


View Tutorial          By: amit at 2012-02-16 12:21:25
31. View Comment

i am new to java plz give any sample and simple java programs

View Tutorial          By: sham at 2012-03-20 02:30:20
32. View Comment

i am new to java programming plz please provide me any sugg. and programs with expliations plz.

View Tutorial          By: Mahammad musthaq at 2012-05-31 14:02:10
33. View Comment

i am newly in java, i want to learn about this and please send me a simple program or a basic java program

View Tutorial          By: punitha at 2012-07-21 06:16:53
34. View Comment

i have a some knowledge of java . I want some examples in a java programing in the concept of threads.

View Tutorial          By: kavitha at 2012-07-30 09:07:47
35. View Comment

hi..... i m newly in java ,i want to learn about this and please send me a programs with its structure n comments. plz...... i need it.

View Tutorial          By: ashwini at 2012-08-09 08:01:34
36. View Comment

i like java but java many program are confused i am not understand so it this keyword and super keyword and ect.

View Tutorial          By: savaliya hardik at 2012-09-11 03:30:10
37. View Comment

thanks brother

View Tutorial          By: shakeel at 2012-09-12 16:21:03
38. View Comment

i need to code for sparse matrix in java. please revert me back with the same.

View Tutorial          By: sudhi at 2012-10-01 09:34:57
39. View Comment

i am new in java so pls send insrutions in java

View Tutorial          By: shefeeja at 2012-10-09 02:22:15
40. View Comment

i need some more basic programes in java

View Tutorial          By: nivin at 2012-10-19 09:14:27
41. View Comment

how to java program run in system and how to open command prompt

View Tutorial          By: safeek at 2012-10-20 06:54:38
42. View Comment

i need some more basic java programs in all type

View Tutorial          By: jkamal at 2012-11-05 04:57:49
43. View Comment

i am newly in java. please send basic sample programs & explanation

View Tutorial          By: bhuvana at 2012-11-21 06:50:01
44. View Comment

java to learn is very easy .................but do hard work.............

View Tutorial          By: yunus khan at 2012-12-06 17:28:41
45. View Comment

java use real world object ............

View Tutorial          By: yunus khan at 2012-12-07 08:24:47
46. View Comment

Hi, i am newly in java,please send me on sample programs in brief explanation sir/madam.

thanking u sir/madam


View Tutorial          By: aruna.k at 2012-12-13 06:05:44
47. View Comment

HOW TO SET PASSWORD IN JAVA.....

View Tutorial          By: NAKUL at 2012-12-14 05:23:16
48. View Comment

Hi, i am new java, and i want to become gud developer so please guide me what should i do for it, and from where i should start it , please tell me......thankx

View Tutorial          By: reuben at 2012-12-20 14:27:08
49. View Comment

hi,i am new in java..plz send me java programmes imly....plz

View Tutorial          By: dinesh at 2012-12-30 15:58:31
50. View Comment

nice one.. i lik dis page... very informative for beginners... thanks..

View Tutorial          By: lokesh at 2013-01-10 09:06:22
51. View Comment

please send basic sample programs & explanation

View Tutorial          By: rose at 2013-01-16 04:43:38
52. View Comment

hi i am new in java please send me basic programs in java ..depends upon the class,object &method please send me imdtly

View Tutorial          By: Ganesh kumar at 2013-02-26 03:31:54
53. View Comment

please send me basic programs in java with explanation....

View Tutorial          By: Rekha at 2013-04-14 03:55:51
54. View Comment

i have to learn java

View Tutorial          By: md arif alam at 2013-04-15 18:30:27
55. View Comment

can i get some basic examples of java program

View Tutorial          By: raja at 2013-06-04 10:47:12
56. View Comment

plz send me some interview based simple java programs ...am new to java

View Tutorial          By: Abilesh at 2013-06-23 06:43:03
57. View Comment

What is the exact meaning for the symbol of java?
please explain.


View Tutorial          By: Aravind at 2013-06-24 10:52:28
58. View Comment

java program to maintain the books details like BookId, accession number; book name, author, publication in books package and keep the journal details such as journal Id; journal Id; journal name; in journal package in main class use these two packages details for staff and student classes and display the books and journals information as requested by the user

View Tutorial          By: Prabha at 2013-08-28 12:34:00
59. View Comment

tis s very useful and easy to capture all persons

View Tutorial          By: Divya at 2013-12-19 05:42:32

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by PriyaBabu
Data Types and Vairables in Java
The Basic Structure of a Simple Java program

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
XML and Java - Parsing XML using Java Tutorial
Recursion in java
How to use ArrayList in Java
FileReader and FileWriter example program in Java
Read from a COM port using Java program
The Basic Structure of a Simple Java program
Multidimensional or arrays of arrays in Java
InetAddress Example program in Java
Sample Java program shows how to write to COM port using Java.
concat(), replace(), and trim() Strings in Java
Integer: byte, short, int, and long data types in Java
Text to Speech conversion program in Java
Connect to a password protected URL from Java program
Vector example in Java
Most Emailed Articles (in Java)
Read from a COM port using Java program
How to use ArrayList in Java
Data Types in Java
Write to a COM port using Java program
How to access instance from an inner class and accessing outer class variables in java
History of Java
Method Overloading (function overloading) in Java
Sample program to demonstrate the use of ActionListener
Tomcat and httpd configured in port 8080 and 80
Save/Write/Read image file from/to a database using Java program
Java program for changeable wrapper class
Report Sections in JasperReports
Using jar utility in Java - A jar tutorial
FilenameFilter - sample program in Java
Observable class and the sample program in Java