To start writing a Java program you could use any plain text editor — I’ll use Notepad. The file that
contains Java code must be saved in a file with its name ending in .java. Enter the following code in the text editor.
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println(“Hello World”);
}
}
Create a directory, c:\PracticalJava\Lesson1, and save the program you just created in the file HelloWorld.java (if you use Notepad, select All Files in the Save as Type drop-down to avoid auto attachment of the .txt suffix).
Keep in mind that Java is a case-sensitive language, which means that if you named the program HelloWorld with a capital H and a capital W, you should not try to start the program helloworld. Your first dozen syntax errors will be caused by improper capitalization.
Our first program contains a class, HelloWorld. Give the Java class and its file the same name. (There could be exceptions to this rule, but not in this simple program.) While writing Java programs you create classes, which often represent objects from real life. You’ll learn more about classes in coming time.
The class HelloWorld contains a method, main(). Methods in Java classes represent functions (actions) that a class could perform. A Java class may have several methods, but if one of them is called main() and is declared (if it has a method signature), as in our class, this makes this Java class executable. If a class doesn’t have a method main(), it can be used from other classes, but you can’t run it as a program.
The method main() calls the method println() to display the text “Hello World” on the screen. Here is the method signature (similar to a title) of the method main():
public static void main(String[] args)This method signature includes the access level (public), instructions on usage (static), return value type (void), name of the method (main), and argument list (String[] args).
➤➤ The keyword public means that the method main() can be accessed by any other Java class.
➤➤ The keyword static means that you don’t have to create an instance of this class to use this method.
➤➤ The keyword void means that the method main() doesn’t return any value to the calling program.
➤➤ The keyword String[] args tells us that this method will receive an array of characters as the argument (you can pass external data to this method from a command line).
nice article for beginners.thank you.
ReplyDeletejava
java