Beginning Java: Writing and Executing Your Java Program

Introduction

Java is a high-level (Easy to understand), object-oriented (have OOPs properties like Encapsulation, Inheritance, Polymorphism), platform-independent (can be run in any Operating System), robust (executes despite failure) and case sensitive programming language.

After you get these basic concepts of Java programming language we will start with our first basic program to calculate factorial in Java.

java

Why you should learn JAVA?

If you are already familiar with programming languages like C, a procedural language, then learning JAVA would be a cutting edge over C.

The basic differences between C and Java are:

  • C is a procedural language where as Java is an object-oriented language.
  • C is a compiled language whereas Java is a compiled and interpreted language.
  • C is a middle level language where Java is high level language.
  • C uses top-down approach while Java follows bottom-up approach.
  • In C, Memory Management is done manually using malloc, calloc, free while in Java Memory Management is done by Java itself by Garbage Collection.
  • Java supports method overloading, method overriding but C does not have these concepts.
  • In C, there is nothing called Exception handling but in Java it is.
  • For C, code acts on data but for Java, data controls access to code.
  • C does have extensive use of pointers whereas Java does have extensive use of objects.
  • C is platform dependent, while Java is platform independent.
  • As compared to C, Java is more secure and easy to learn.

Prerequisites

  1. Standard Editor (e.g. Textpad/Notepad/Wordpad)
  2. JDK must be installed in your machine in order to compile and execute the JAVA program.

Steps to write Java Program

  1. Write this Java Code to a file
public class WelcomeToJAVA{
    public static void main(String args[]){
        System.out.println(“Welcome To JAVA”);
    }
}
  1. Save the file with .java extension. Always try to save the java program file as the name of the class where main method resides. Therefore the name of our java program is “WelcomeToJAVA.java”
  2. Please remember the location where you save your java file. In this example we have saved the file in “D:\javaprograms\example\” directory.
  3. Now open the command prompt to compile and execute the java program.
  4. Navigate to the directory where you have kept your java file.
  5. Execute the following command to compile the java program using javac compiler.
    D:\javaprograms\example>javac WelcomeToJAVA.java
  6. Execute the following command to execute the java program using JVM (Java Virtual Machine) [interpreter for .class file]
    D:\javaprograms\example>java WelcomeToJAVA

1st java program

Explanation of the Java Code

  • Here “public” is an access-specifier. 3 more access-specifiers are there viz. default, private, protected. We will discuss those later.
  • “class” is a keyword in java. To define a class, this keyword has to be written.”WelcomeToJAVA” is the name of the class. Class name should not be as same as any of the java keywords.
  • main() is the starting point of any java program. It is declared as “public” and “static” so that this main() method can be called from outside of the ”WelcomeToJAVA” class without creating any object(instance of a class, will be discussed later) for the class ”WelcomeToJAVA”.Now, main() method returns void to OS that why return type of main() method is void.
  • Now parameter String args[] is a String array useful only when we pass argument through command line.
  • System.out.println is a statement which will write the output to console. “System” is a predefined class in java and “out” static field of is a PrintStream class (predefined class in java). Moreover “println()/print()” is a method of PrintStream class which is actually responsible for writing output of the program to console. The difference between println() and print() is that println() after writing the output to console, it returns control to the immediate next line whereas in case of print(), control remains in the same line.
  • After compilation of WelcomeToJAVA.java file, WelcomeToJAVA.class(bytecode) will get generated, then java interpreter will execute the class file in order to produce the output.

N.B: Before compiling and executing a Java program, you have to set class-path otherwise it will not be possible to compile and execute a Java program. In our next tutorial we will see how to set class-path for Java.

Checkout more useful tutorials and definitive guidelines on Java programming here.

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.