The Basic Structure of a Simple Java program

By: PriyaBabu Viewed: 153135 times  Printer Friendly Format    


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



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. i want to study java
View Tutorial          By: doshyo at 2007-12-13 07:41:03

2. Hi
I am newly in java. I have one dought.

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

3. i m new in java plz send some programs on packages
View Tutorial          By: zainul hasan mirza at 2009-08-31 02:28:32

4. i m new in java plz send some programs of java usi
View Tutorial          By: supriyo at 2009-09-02 02:03:38

5. i want to know about simple java structure with br
View Tutorial          By: Rambahaz at 2009-09-07 06:41:25

6. i m new in java plz send package of simple program
View Tutorial          By: iqbal ahmad at 2009-09-07 01:26:41

7. Hi, this blog answers my questions but what is the
View Tutorial          By: Edu at 2009-09-20 00:19:27

8. its gud 2 learnjava simple prog wit this link.....
View Tutorial          By: Rakesh at 2009-10-03 08:48:32

9. its gud 2 learn java sim prog
View Tutorial          By: Rakesh at 2009-10-04 03:47:01

10. I wont to know about every thing of java
View Tutorial          By: susantha at 2009-10-11 09:35:40

11. plz send me java prograams
View Tutorial          By: simmy at 2009-10-26 01:57:55

12. please send me Java programs
View Tutorial          By: Jayita at 2009-11-06 08:19:59

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

14. The basic structure of simple program is used to l
View Tutorial          By: Muthusasi at 2009-12-17 21:10:01

15. nice one.. i lik dis page... very informative for
View Tutorial          By: lokesh at 2013-01-10 09:06:22

16. i wish to get some example java programs to unders
View Tutorial          By: rebecca at 2010-01-09 03:14:42

17. i wish to get some example java programs to unders
View Tutorial          By: rebecca at 2010-01-09 03:15:06

18. i need java simple programs
View Tutorial          By: Tharani at 2010-01-22 06:39:25

19. i want to learn the java from basics onwards.
View Tutorial          By: vinay at 2010-03-08 22:54:15

20. please send me simple java program
View Tutorial          By: sangeetha at 2010-07-16 00:45:52

21. please send me simple java program
View Tutorial          By: sangeetha at 2010-07-16 00:47:17

22. i want to know about simple java structure with br
View Tutorial          By: Phodiso Fly at 2010-08-02 04:06:37

23. Hi
I am newly in java. I have one dought. I

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

24. hi am new to java! i need some simple java program
View Tutorial          By: Anu at 2010-09-08 08:17:49

25. i m new in java plz send some programs of java usi
View Tutorial          By: RUPESH RANJAN at 2010-09-15 14:27:39

26. hi iam new to java i need simple java programs pls
View Tutorial          By: naga at 2010-10-06 03:45:18

27. can i have a piece of program so that i can improv
View Tutorial          By: miniboss at 2010-11-12 06:09:41

28. i am newly in java, i want to learn about this and
View Tutorial          By: gelen at 2010-11-23 02:57:15

29. i want to know about everthink of java.....

View Tutorial          By: sandhya at 2011-03-03 03:01:46

30. i am new to java, please send simple programs, and
View Tutorial          By: jegan at 2011-07-11 02:22:37

31. hi
I have to learn java using NetbeansIDE

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

32. pls send how to write java program in easily and e
View Tutorial          By: sabura at 2011-08-08 09:59:28

33. java is new for me..... since i dont know from bas
View Tutorial          By: m.padma at 2011-08-08 15:37:14

34. java is new for me..... since i dont know from bas
View Tutorial          By: m.padma at 2011-08-08 15:40:23

35. I am new in java.write some basic program.plz send
View Tutorial          By: ravi at 2011-09-27 01:41:46

36. hi padma.M
so U new for java right??/

View Tutorial          By: Murali at 2011-11-06 14:42:21

37. hi padma.M
so U new for java right??/

View Tutorial          By: Murali at 2011-11-06 14:43:04

38. hi padma.M
so U new for java right??/

View Tutorial          By: Murali at 2011-11-06 14:43:45

39. hi i am studying java course.i am new to java, ple
View Tutorial          By: Rajeshkumar at 2011-11-14 08:30:29

40. Hi,
I'm studying core java.java is new for

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

41. Hi , I am mew to java please can u send detail how
View Tutorial          By: ramanji at 2011-12-11 05:36:41

42. Currently am working in Perl, lik to learn Java, n
View Tutorial          By: KUMARESAN at 2011-12-14 05:08:47

43. Hi...i know java but little....
give simple

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

44. Hi, i am new java, and i want to become gud develo
View Tutorial          By: amit at 2012-02-16 12:21:25

45. can we write structural programs using java..??
View Tutorial          By: sanjaya at 2012-02-24 19:26:41

46. can we write structural programs using java..??
View Tutorial          By: sanjaya at 2012-02-24 19:27:28

47. i am new to java plz give any sample and simple ja
View Tutorial          By: sham at 2012-03-20 02:30:20

48. pls forword collection with programs n clear expla
View Tutorial          By: swetha at 2012-05-12 19:16:13

49. i am new to java programming plz please provide me
View Tutorial          By: Mahammad musthaq at 2012-05-31 14:02:10

50. i am really desperate to learn java.. please help
View Tutorial          By: meliza at 2012-06-26 18:27:21

51. unmaiyavey unga yaarkum english theriyatha..ivlo c
View Tutorial          By: surya sathish at 2012-06-27 08:07:02

52. hai i am newly in java ...i need some simple java
View Tutorial          By: suren at 2012-07-10 09:40:32

53. i am newly in java, i want to learn about this and
View Tutorial          By: punitha at 2012-07-21 06:16:53

54. i have a some knowledge of java . I want some exam
View Tutorial          By: kavitha at 2012-07-30 09:07:47

55. hii..........I want simle java programs.
IM

View Tutorial          By: arti singh at 2012-08-06 14:50:51

56. hi..... i m newly in java ,i want to learn about t
View Tutorial          By: ashwini at 2012-08-09 08:01:34

57. pls send some basic simple examples on core java p
View Tutorial          By: sushma at 2012-08-21 06:31:00

58. hi.... i need basic programs in java .and also exa
View Tutorial          By: gokulavani at 2012-08-24 23:20:00

59. i like java but java many program are confused i a
View Tutorial          By: savaliya hardik at 2012-09-11 03:30:10

60. thanks brother
View Tutorial          By: shakeel at 2012-09-12 16:21:03

61. i need to code for sparse matrix in java. please
View Tutorial          By: sudhi at 2012-10-01 09:34:57

62. i am new in java so pls send insrutions in java
View Tutorial          By: shefeeja at 2012-10-09 02:22:15

63. i need some more basic programes in java
View Tutorial          By: nivin at 2012-10-19 09:14:27

64. how to java program run in system and how to open
View Tutorial          By: safeek at 2012-10-20 06:54:38

65. i need some more basic java programs in all type
View Tutorial          By: jkamal at 2012-11-05 04:57:49

66. i am newly in java. please send basic sample progr
View Tutorial          By: bhuvana at 2012-11-21 06:50:01

67. java to learn is very easy .................but do
View Tutorial          By: yunus khan at 2012-12-06 17:28:41

68. java use real world object ............
View Tutorial          By: yunus khan at 2012-12-07 08:24:47

69. Hi, i am newly in java,please send me on sample pr
View Tutorial          By: aruna.k at 2012-12-13 06:05:44

70. HOW TO SET PASSWORD IN JAVA.....
View Tutorial          By: NAKUL at 2012-12-14 05:23:16

71. Hi, i am new java, and i want to become gud develo
View Tutorial          By: reuben at 2012-12-20 14:27:08

72. hi,i am new in java..plz send me java programmes i
View Tutorial          By: dinesh at 2012-12-30 15:58:31

73. please send basic sample programs & explanatio
View Tutorial          By: rose at 2013-01-16 04:43:38

74. hi i am new in java please send me basic programs
View Tutorial          By: Ganesh kumar at 2013-02-26 03:31:54

75. please send me basic programs in java with explana
View Tutorial          By: Rekha at 2013-04-14 03:55:51

76. i have to learn java
View Tutorial          By: md arif alam at 2013-04-15 18:30:27

77. Awesome tutorial... Really useful for a programmin
View Tutorial          By: Jithin Johny George at 2013-04-28 09:55:13

78. can i get some basic examples of java program
View Tutorial          By: raja at 2013-06-04 10:47:12

79. plz send me some interview based simple java progr
View Tutorial          By: Abilesh at 2013-06-23 06:43:03

80. What is the exact meaning for the symbol of java?<
View Tutorial          By: Aravind at 2013-06-24 10:52:28

81. java program to maintain the books details like Bo
View Tutorial          By: Prabha at 2013-08-28 12:34:00

82. i didnt get any shit fron this!!!
View Tutorial          By: mohammed jasim at 2013-11-28 07:04:25

83. tis s very useful and easy to capture all persons
View Tutorial          By: Divya at 2013-12-19 05:42:32

84. lakshya*world international school pls give a simp
View Tutorial          By: lakshya at 2014-12-28 06:21:41

85. These comments are hilarious!

Plz c

View Tutorial          By: JonnyChangWang at 2016-10-27 16:13:31

cannot update times viewed