The main Method
In Java,
everything goes in a class. The source code is typed into a file (with a .java extension). This is then compiled into a new class file
(with a .class extension)
When you run
your program, you are really running a class.
Running a
program means telling the Java Virtual Machine (JVM) to “Load the class” (Book class for example), then starting to execute its
main() method. Keep running till all the code in main() method is finished.
The main()
method is where a Java application starts running.
Every Java
application has to have at least one class, and at least one main() method.
The main()
method will be written as shown in the following code:
public static void main (String[] args) {
// your code goes here
}
Uses of
main()method:
oTo test your real class
oTo launch or start your Java application
The System.out.println (SOP) Method
The System class
is available in the java.lang package.
The “out” is a
field of the System class representing the “standard” output stream.
Typically this
stream corresponds to display output or another output destination
specified by the host environment or user.
For simple
stand-alone Java applications, a typical way to write a line of output data is
System.out.println(data) .
System.out.println inserts a new line while System.out.print keeps
printing to the same line.
Code Structure in Java
Put a class in a
source file
Put methods in a
class
Put statements
in a method
Example
/**
*
* Sample program to print This is my first Java program
*
*/
class MyFirstProgram { // Declare class MyFirstProgram
void displayMessage() {
System.out.println(“This is my first Java program”);
}
public static void main(String[] args) {
MyFirstProgram myFirstProgram = new MyFirstProgram();
myFirstProgram.displayMessage();
}
}
Save the preceding contents in a file called
MyFirstProgram.java
Compile and Run a Java Program
Compile:To compile MyFirstProgram.java source file in a
MS-DOS prompt, give the following
command:
javac MyFirstProgram.java
After successfully compiling the earlier source file, the
compiler generates MyFirstProgram.class
file, which is made up of bytecodes. The compiled
bytecode is platform independent.
Run: To run the earlier Java program in a MS-DOS prompt,
give the following command:
java MyFirstProgram
The JVM translates the bytecode into something that the
underlying platform understands, and runs your program.
The following output will be displayed in the command
prompt after running the earlier command:
This is my first Java program
Try It Out
Problem Statement:
Illustrate the basic structure ofa Java application
program.
Code:
/**
* A Simple application.
*/
public class SimpleApp1 {
public static void main(String args[]) {
// A method to output to the console.
System.out.println("Simple program");
}
}
Refer File Name: SimpleApp1.javato obtain soft copy of
the program code
How It Works:
/**
* A Simple application.*/:A comment describing the
program
public class
SimpleApp1 {:Begin with the class specification
public static
void main(String args[]) {:Required method for
application programs
// A method to
output to the console.:Comment using two slashes
System.out.println("Simple program");:Print to console
}:Curly braces
span the code for a class. They also bracket the code of a method.
Tips and Tricks
Does a Java program need to have a main() method in every
class written?
Solution:No, a Java program might use dozens of classes (even
hundreds),but you might have
only one with a main() method, the one that starts the
running of the program. You might write test
classes, though, that have main methods for testing your
other classes.
Summary
The method
signature for the main method in a Java application is as follows:
public static void main(String[] args)
Briefly explain
the reason that the main method in a Java application is declared
public.
The keyword
public indicates that the method can be called by any object.
Explain the
reason that the main method in a Java application must be declared static.
The keyword
static indicates that the method is a classmethod which can be called without the requirement to instantiate an object of the
class. This is used by the Java interpreter to launch the program by invokingthe main
method of the class identified in the command to start the program.
Describe the
purpose of the keyword void when used as the return type for the main method.
The void keyword
when used as the return type for any Java methods indicates that the method does not return anything.
No comments:
Post a Comment