# How to write java program #Structure || Java Program
Structure of Java Program
A java program must contain a main method as its starting point.
(a) Documentation section
(b) Package Statement
This statement declares a package name and informs the compiler that the classes defined here belong to this package.
(c) Import Statement
This statement is similar to the #include statement in C++ Example:
import java.lang. Math;
This statement instructs the interpreter to load the Math class from the package lang. Note :- Math class contain mathematical methods like sqrt etc
(d) Interface Statements
An interface statement is used only when we wish to implement the multiple inheritance feature in the program.
(e) Class Definitions
A Java program may contain several class definitions. Define all classes .
(f) Main Method Class
First Java Program
*Where to write and run Java program?
:- Firstly, now we write a Java program on Notepad screen and run command prompt.
Program:-
class sample
{
public static void main (String args[])
{
System.out.println ("First Java Program");
}
}
Save the above program as sample.java.
Note :-Name of the class is same as the name of the file.
1. The first line of the program
class sample
declares a class. Here class is keyword and sample is the name of class
Note:- Java is a true object-oriented language, therefore, everything must be placed inside a class
2. The class definition begins with an Opening brace "{" and ends with a matching closing brace. "}" in the last line of the program.
3. The third line of the program
public static void main (String args[])
defines a method main.
Note :- In Java function is called method. Every java program contains exactly one main method.
Here public keyword is an access modifier which represents visibility, it means it is visible to all.
static is a keyword, if we declare any method as static, then there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
vold is the return type of the method, it means it doesn't return any value.
main represents startup of the program.
String args[] declares a parameter named args ,which is an array of string (text) variable.
4. The opening brace "{" indicates the beginning of the method main() and the matching closing brace }" indicates the end of the method.
5. The fifth line of the program
System.out.println ("First Java Program");
For prints (or outputs) the string First Java Program on the screen, followed by new line.
●System- predefined class
●out- object
●println- method appends a new line character to end of string.
How we can Compiling and Running the java program?
(a) Compiling the program
In order to compile the java program, open the command prompt and goto your directory and write this command.
c:\sgoel> javac sample.java
The java compiler creates a file called sample.class. This class contains the program but in bytecode form and ready to run. With the help of this diagram you properly understand this concept.
(b) Running the program
To run the program, write this command
c:\sgoel> java sample
We get output on screen
First Java program
At runtime, these steps are followed
Requirement for Java Program
For executing any java program, we need to these steps:-
(a) Install the JDK. If you do not have installed it, download the JDK from the java.sun.com website and install it.
(b) Set path of the JDK/bin directory.
(c) Create the java program using Notepad.
(d) Compile and run the java program.
Recall this content form of questions:
1.What do you mean by a Platform?
2 Write history of Java in brief?
3.Explain various features of Java?
4. Explain various steps to Compile and Run the Java Program.
5. What are the requirements for Java Program?
6. Explain various steps to set the path variable to the Java directory.
7. Explain Java Program Structure.
Very nice
ReplyDeleteUnderstandable content
ReplyDeleteKnowledge base content
ReplyDeleteGood 👍 👍
ReplyDeleteNice 👌👍👍👍
ReplyDelete👍👍👍
ReplyDeleteGood efforts 👍👍
ReplyDeleteNice work 👍❤️
ReplyDeleteU r uploading Fantastic topics
ReplyDeleteYour notes is very easy and simple ☺️☺️☺️☺️☺️☺️☺️☺️☺️☺️
ReplyDelete