Powered by Blogger.

Java:: Simple Java Program for beginners

This short example shows how to write first java application and compile and run it, which prints a String Welcome in Java World in output:

1    public class Welcome{
2       public static void main ( String[ ]args ){
3          System. out.println(”Welcome in Java World”);
4       }
5    }


Write this code in Notepad for Windows or Text Editor for Linux and save the file as: Welcome.java (Source files must be named after the public class). Now you are ready to compile and run the program. To compile the program, open a command window or terminal, and change to the directory where the program is stored and type javac Welcome.java If the program contains no errors then a new file Welcome.class will be created in the same directory. Now you can run this byte codes file Welcome. class by typing the command java Welcome

Line 1: declares the user-defined class named Welcome. Every Java program consists at least one public class. The Welcome.java file creates a Welcome.class file when the source file is being complied.

Line 2: declares the main method, where the program starts to execute.

The following describes each element of Line 2:

public The access modifier of main() method is public, because the main() method can be accessed by anything including the Java technology interpreter.

static The main() is static method because on instance of the class is needed to execute static methods and static method is a single copy.

void The return type of the main() method is void, because the main() method does not return any value.

String[] args The main() method declares the single parameter of String array named args. If the program is given any arguments on its command line, these are passed into the main() method
through this args array of String type.

Line 3: displays or prints a line of text in the command window. System.out is known as the standard output object.

Line 4: end of the main() method.

Line 5: end of the Welcome class.

1 comment:

  1. I really enjoy the blog.Much thanks again. Really Great.
    Very informative article post. Really looking forward to read more. Will read on…


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete