Hello World Java

First, let's see how to run a Java Program using a text editor like Notepad and command prompt and after that let's move on to the NetBeans I.D.E.

Java is an Object Oriented, High-level programming language and a computing platform which was released by Sun Microsystems in 1995. An important feature of Java is, it is platform independence. (Platform Independence means once a program is compiled then it can be run on any platform such as Windows, Mac OS X and Linux. )

To run Java on your machine you have to install Java Development Kit  (JDK). You can download JDK and NetBeans  from this link . When you download JDK, you get the Java Runtime Environment(JRE) which consists of Java Virtual Machine (JVM), Java core classes and Java libraries.

And we have to set the Java bin path to the path variable.

1st Step :

  C Drive -> Program Files -> Java -> jdk -> bin-> copy the path .
(when you click on the bar with the folder icon which contains the folder path you can copy the path)

2nd Step :

Computer -> Properties -> Advanced system settings -> Environment variables... -> select 'path' variable in System variables -> Edit... -> type a semicolon(;) and paste the path and then type a backslash(\) ->OK

Now let's write our first code.

1. Type this code in your text editor.

public class MyFirstJavaProgram {

    public static void main(String[] args) {
        System.out.print("Hello world Java");
    }
}

Here I am going to briefly explain the code.
The method is declared inside a class. When we run a Java code ,the main method body will be to execute from top to bottom. We can't run a Java code without the main method, but we can compile without the main method. In the run time, it seeks the main method.

To give an idea about the keywords used here,

  • 'public' is an Access Modifier and when it is public any other class can access it. 
  • 'void' means this method does not return any value. 
  • 'System' is a pre-defined class in Java. ( To learn more about java.lang.System visit here.)
  • 'out' is a static variable in System class. - public static final PrintStream out.
  • 'print' is a method in PrintStream class which is a pre-defined class in Java. ( To learn more about Java.io.PrintStream visit here. )

2. Save this as MyFirstJavaProgram.java.

File -> Save As... ->  File name    : MyFirstJavaProgram.java -> Save
                                   Save as type: All Files

The name of the source file should be our class name with .java file extension.

3. Open the Command prompt and go to the place where you have saved your source file.

Shift + Right click -> Open command window here

4. type javac MyFirstJavaProgram.java in the command prompt and press enter.

If there aren't any errors it will move onto the next line.

When we type our file name(class name ) with .java extension in front of javac command we can compile our code. In our text editor, we can see our code in a human readable format. When we compile the code, a class file will be automatically generated and it is in computer readable format. If you go to the place where you have saved your source file you can see MyFirstJavaProgram.class file.



5. Now type java MyFirstJavaProgram in the command line and press enter.

If there aren't any errors the output will be displayed.

When we type our file name(class name) with the java command and press enter we can run our code.




Now let's see how to use NetBeans I.D.E. to run our first program.


NetBeans -> File -> New Project -> from Categories select 'Java' and from Projects select 'Java Application' -> Next -> give the 'Project Name ' as MyFirstJavaProgram -> Give a Location and a Folder to save the project -> tick the box in front of 'create Main class'  ->Finish



NetBeans make our work easy. With this program, we only have to type our print statement inside the main method body.



Here we can use several ways to compile a project.
1. Main Menu -> Run -> Build Project
2.Use f11 keyboard shortcut.
3.Click on this icon in the icon menu.



If there aren't any errors we can see  BUILD SUCCESSFUL  in the output window.                                                                                                                            

Here we have several ways to run a program also.
1. Main Menu -> Run -> Run Project
2.Use f6 keyboard shortcut.
3. Click on this icon in the icon menu.



After running the project we can see 'Hello world Java' in the output window.


Comments

Popular posts from this blog

UML - Use Case Diagrams

How I faced to OCJP exam...

My Experience at Women Techmakers Leads Mini-summit APAC at Google Singapore