Compile and Run First Java Program





In this section we will learn how to write and execute a simple Java program.

  • To write a Java program you can use any text editor like notepad, notepad++ on windows, gedit on linux.etc..

  • You can also use popular IDE, in order to write and execute your java programs.
    Like :- BlueJ,NetBeans, Eclipse etc...

  • To compile your Java source code, you need a Java compiler, and to run an Java interpreter.
    You will get these in JDK. So, before proceeding make sure you have installed and configured the JDK in your system.


Programming in Java: We break the process of programming in Java into 3 steps:
  1. Create the source code, using a text editor and saving the file with extension .java.
    For example: MyFirstProgram.java or Hello.java etc..
  2. Compile the source file by typing
    "javac SourceFileName" in the terminal window (command prompt).
    e.g. javac MyFirstProgram.java
    After successful compilation it will generate a class file (bytecode) with extension .class.
    Name of this bytecode will be same as your class-name in your source code with extension .class.
    This .class file is meant for jvm. prowessapps.in
  3. Execute (or run) it by typing
    "java classFileName" in the terminal window (command prompt).
    e.g. java MyFirstProgram
    prowessapps.in NOTE: don't write extension .class at the time of execution.

1. Create the source code:

Open the text editor.

Type the following code and save the file with name "HelloWorld.java"

You can save your file at any location in your system, for this example file is saved at Desktop.


//HelloWorld.java
class HelloWord
{
 public static void main(String [] args)
 {
  System.out.println("Hello World");
 }
}           

prowessapps.in

2. Compile & Run the code :

Open the terminal ( Command Prompt) and change the working directory to that loacation where you have saved your source file.

For compilation type
javac HelloWorld.java

To run type
java HelloWorld

It will give output
Hello World

prowessapps.in

Understanding first java program :

Let's understand the code and meaning of class, public, static, void, main , String, and System.out.println().

  • class is a keyword used to declare a class.
  • public is a keyword and access specifiers which says it is visible to all.
  • static is a keyword, if you declare any method as static, it is known as static method. The benefit of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method.
  • void is a keyword, is the return type of the main method, it means it doesn't return any value.
  • main method is entry point of program.
  • String[] args refers to 1-D array of String type, it's generally used to retrieve command-line arguments.
  • println() is a function that is used to print (output) on terminal.

Source Code Facts

  • Source code create by programmer.

  • Source file contains high level language code.

  • Source code may contain one or more class declaration.

  • Source code cannot have more than one public class.

  • Source file must be save with .java extension e.g. FileName.java.

  • If source code contains a public class then the source file name must be same as public class name.

  • Source Code Structure :
    package declaration;
    import statements;
    class ClassName
     {
       //class members
     }              

Byte Code Facts

  • Compiler generates byte code from source code.

  • Byte code contains intermediate code.

  • Only JVM can understand byte code.

  • Compiler generates one byte code per class.

    prowessapps.in
  • Byte code saves with .class extension e.g. ClassName.class.

  • If you want to pass a class direct to JVM, must contain main method, else you will get error no such main method found.

Next topic is Java Tools & Runtime




 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in