Tuesday, 7 August 2018

SIMPLE JAVA PROGRAM

Class Example
{
public static void main(String args[])
{
System.out.println(“Welcome to Java”);
}
}

Save this file as Example.java same as class name
Class Example
It defines a class named Example using a keyword class. After that, class definition is
specified within curly braces.

public static void main(String args[])
This is the point from where the program will start the execution. The public keyword
is used to control the access of various class members. If member is public it can be accessed
outside the class. So we have to declare main( ) as public because it has to be invoked by the
code outside the class when program is executed. Static keyword allows the main( ) method
to be executed without creating an object of that class, and void means main( ) method does
not return any value.

String args[]
String is a predefined class and it takes any type of variable. [ ] represents free array.
args represents reference variable of string type.
System.out.println(“Welcome to Java”);
System is a class from java.lang package that contains all I/O related reference variables. Out
is a variable of system class. println( ) function is used to display this line.
Running the program
First set the path
location
C:\ set path=c:\jdk 1.3\bin;%path%
Compile the program
C:\> javac Example.java
The javac compiler creates a file called Example.class that contains the bytecode version of
the program.
Run the Program
C:\> java Example
Output: Welcome to java

No comments: