Saturday, July 4

Learn how to write a simple java program (basics)


public class MyProgram {
public static void main(String[] args) {

System.out.println("Rome wasn't built in a day!");
}
}

Hello friends....

This is your first JAVA Program to learn the basic behind the coding.... Java is approximately a Object Oriented Programming Language.

Everything in the java is in "OBJECTS"--- Real and physical world entity .... AND "CLASSES"--- logical entity...

The "MyProgram" program is extremely simple. It merely prints a message to the console.

The printed message is -

Rome wasn’t built in a day!




Every Java application contains a class that defines a method called main(). The name of this class is the name that you use as the argument to the Java interpreter when you run the application. You can call the class whatever you want, but the method which is executed first in an application is always called main(). When you run your Java application, the method main() will typically cause methods belonging to other classes to be executed, but the simplest possible Java application program consists of one class containing just the method main(). As you will see below, the main() method has a particular fixed form, and if it is not of the required form, it will not be recognized by the Java interpreter as the method where execution starts.

You can see how this works by taking a look at just such a Java program. You need to enter the program code using your favorite plaintext editor, or if you have a Java development system with an editor, you can enter the code for the example using that. When you have entered the code, save the file with the same name as that used for the class and with the extension .java. For this example the file name will be MyProgram.java

The program consists of a definition for a class I have called MyProgram. The class definition

contains only one method, the method main(). The first line of the definition for the method main() is always of the form:

public static void main(String[] args)

The code for the method appears between the pair of curly braces. This version of the method has only one executable statement:

System.out.println(“Rome wasn’t built in a day!”);

So what does this statement do? Let’s work through it from left to right:

1. System is the name of a standard class that contains objects that encapsulate the standard I/O devices for your system—the keyboard for command-line input and command-line output to the display. It is contained in the package java.lang, so it is always accessible just by using the simple class name System.

2. The object out represents the standard output stream—the command line on your display screen—and is a data member of the class System. The member, out, is a special kind of member of the System class. Like the method main() in our MyProgram class, it is static. This means that out exists even though there are no objects of type System. Using the class name, System, separated from the member name out by a period—System.out—references the out member.

3. The bit at the rightmost end of the statement, println("Rome wasn’t built in a day!"), calls the println() method that belongs to the object out, and that outputs the text string that appears between the parentheses to your display. This demonstrates one way in which you can call a class method—by using the object name followed by the method name, with a period separating them. The stuff between the parentheses following the name of a method is information that is passed to the method when it is executed. As we said, for println() it is the text we want to output to the command line.

You can compile this program using the JDK compiler with the command:

javac MyProgram.java

Run the program as :

java MyProgram

0 comments:

Followers

Archive

 

Money ,Entertainment and Education. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com