BIG
DATA

JAVA

FIRST JAVA PROGRAM

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

For executing any java program, you need to

  • Install the JDK
  • Set path of the JDK/bin directory
  • Create the java program
  • Compile and run the java program

Let us look at a simple java program.


class HelloWorld
{
	public static void main(String[] args) 
	{
		System.out.println ("Welcome to Hello World program");
	}
}

Understanding simple java program

  • class keyword is used to declare a class in java
  • public keyword is an access modifier which represents visibility, it means this function is visible to all.
  • static is a keyword, used to make a static method. The advantage of static method is that there is no need to create object to invoke the static method. The main() method here is called by JVM, without creating any object for class.
  • void is the return type of the method, it means this method will not return anything.
  • Main: main() method is the most important method in a Java program. It represents startup of the program.
  • String[] args is used for command line arguments.
  • System.out.println() is used to print statements on the console.

Creating Your First Application

Your first application, HelloWorld, will simply display the greeting " Welcome to Hello World program". To create this program, you will:

  • Create a source file

    A source file contains code, written in the Java programming language, that you and other programmers can understand. The easiest way to write a simple program is with a text editor. So, using the text editor of your choice, create a text file with the following text, and be sure to name the text file HelloWorld.java. Java programs are case sensitive, so if you type the code in yourself, pay particular attention to the capitalization.

    first-program

  • Compile the source file into a .class file

    A program has to be converted to a form the Java VM can understand so any computer with a Java VM can interpret and run the program. Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the JVM.

    The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.

    The Java compiler is invoked at the command line on Unix and DOS shell operating systems as follows:
    javac HelloWorld.java

  • Run the program

    Once your program successfully compiles into Java bytecodes, you can interpret and run applications on any Java VM, or interpret and run applets in any Web browser with built in JVM.Interpreting and running a Java program means invoking the Java VM byte code interpreter, which converts the Java byte codes to platform-dependent machine codes so your computer can understand and run the program.

    The Java application launcher tool (java) uses the Java virtual machine to run your application.

    And then when you try to run the byte code(.class file), the following steps are performed at runtime:

    • Class loader loads the java class. It is subsystem of JVM Java Virtual machine.
    • Byte Code verifier checks the code fragments for illegal codes that can violate access right to the object.
    • Interpreter reads the byte code stream and then executes the instructions, step by step.

    The Java interpreter is invoked at the command line on Unix and DOS shell operating systems as follows:
    java HelloWorld

    At the command line, you should see:
    Welcome to Hello World program

Steps to Compile and Run your first Java program

Step 1: Open a text editor and write the code as above.
Step 2: Save the file as HelloWorld.java
Step 3: Open command prompt and go to the directory where you saved your first java program.
Step 4: Type 'javac HelloWorld.java' and press Return to compile your code. This command will call the Java
Compiler asking it to compile the specified file. If there are no errors in the code the command prompt will take you to the next line. It will also create a new file called HelloWorld.class .
Step 5: Now type 'java HelloWorld' on command prompt to run your program.
Step 6: You will be able to see Welcome to Hello World program printed on your command prompt.

first-program