Java is a general-purpose,object-oriented programming language. two types of Java Programs can be created:
- Stand-alone application
- Web applets
Stand-alone applications are programs written in Java to carry out certain tasks on a stand-alone local computer. In fact, Java can be used to develop programs for all kinds of applications, Hot Java itself is a Java application program. Executing a stand-alone Java program involves two steps.
1.Compiling source code into bytecode using javac compiler
2.Executing the bytecode program using java interpreter
Applets are small Java programs developed for Internet application. An applet located on a distant computer(Server) can be downloaded via Internet and executed on a local computer (Client) using a Java –capable browser. We can develop applets for doing everything from simple animated graphics to complex games and utilities. Since applets are embedded in an HTML (Hypertext Markup Language) document and run inside a Web page, creating and running applets are more complex than creating an application.
Stand-alone programs can read and write files and perform certain operation that applets cannot do. An applet can only run within a Web browse.Following figure shows the flow of java program for both stand alone program and applet.
Java Program Structure
A Java program may contain many classes of which only one class defines a main method. Classes contain data members and methods that operate on the data members of the class Method may contain data type declaration and executable statement. To write a Java program we first define classes and then put them together. A java program may contain one or more section
Documentation Section |
Package Statement |
Import Statement |
Interface Statement |
Class Definition |
Main Method Class { main Method Definition } |
Documentation Section
The documentation section comprises a set of comment lines giving the name of the program the author and other details, which the programmer would like to refer to at a later stage. Comments must explain why and what of classes and hoe of algorithms. This would greatly help in maintaining the program. In addition to the two styles of comments discussed earlier. Java also uses a third style of comment /**…..*/ Known as documentation comment. This form of comment is used for generating documentation automatically.
Package Statement
The first statement allowed in a Java file is a Package statement. This statement declare a Package name and informs the compiler that the classes defined here belong to this package. The package statement is optional. Example
package p1;
Import Statement
After a package statement (but before any class definition)may be a number of import statement. This is similar to the #include statement in C
Example
import java.util.Date;
This statement instructs the interpreter to load the Date class contained in the package util.
Interface Statement
An interface is like a class but includes a group of method declaration. This is also optional section and is used only when wish to implement the multiple inheritance feature in the program. Interface is a new concept in Java.
Class Definitions
A Java program may contain multiple class definition. Classes are the primary and essential elements of a Java Program. These classes are used to map the objects of real world problems. The number of classes used depends on the complexity of the problems
Main Method Class
Since every Java stand alone program required a main method as its starting point this class is the essential part of a Java program. A simple Java program may contain only this part. The main method creates objects of various classes and establishes communication between them On reaching the end of main the program terminates and the control passes back to the operating system.
Ex,
class Program
{
public static void main(String args[])
{
System.out.println(“Hello”);
}
}
Here, the class program contains the main() method. and print the message.The name of java source file and the main method class name should be same.
Save source file : Program.java
Complile source file : javac Program.java
Interpret bytecode file : java Program
main() method signature
public static void main(String args[])
{
}
The public keyword is an access specifier, which allows the programmer to control the visibility of 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 opposite of public is private, which prevents a member from being used by code defined outside of its class.) In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started.
The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.
The keyword void simply tells the compiler that main( ) does not return a value.
main( ) is the method called when a Java application begins. The Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main( ) method. But the Java interpreter has no way to run these classes. So, type Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( ) method.
Any information that needs to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. If there are no parameters required for a given method, you still need to include the empty parentheses. In main( ), there is only one parameter, although a complicated one. String args[ ] declares a parameter named args, which is an array of instances of the class String. (Arrays are collections of similar objects.) Objects of type String store character strings. In this case, args receives any command-line arguments present when the program is executed.
The code that comprises a method will occur between the method’s opening curly brace and its closing curly brace.
System.out.println("Hello.");
This line outputs the string “Hello.” followed by a new line on the screen. Output is actually accomplished by the built-in println( ) method. In this case, println( ) displays the string which is passed to it. println( ) can be used to display other types of information, too. System is a predefined class that provides access to the system, and out is the output stream that is connected to the console.
Posted by : Ruchita pandya
Posted by : Ruchita pandya
No comments:
Post a Comment