Advertisement



< Prev
Next >



Simple Java Program




To write your first simple Java program, please open the Notepad application(comes preinstalled in Windows Operating System) and type in your first Java program, as explained in this article.

You may even use Integrated Development Environment(IDE) application like Eclipse and NetBeans to create your first Java program but for a beginner, we would recommend using Notepad, because you learn a lot when you work to find your own mistakes to fix them, as compared to an IDE finding them for you.




Important points about a class in Java -







Java naming conventions -







Note :







Creating our first Java program


We are creating a Java program which is saved in a file named A.java.




Why .java extension for Java program?


It is because every Java program is saved with a .java extension.

A.Java
public class A
{
public static void main(String[] ar)
{	
System.out.println("Hello from Java");
}
}



Advertisement




Understanding your first Java program


Let's see what we actually did in our first Java program by dividing it into a few segments -



  1. public class A

    • Every Java program is contained within a class. Hence, we have created a class named, A.

    • According to the Java convention, every class name in Java should start with a capital alphabet, hence, we have named our class A.

    • Any class in Java can be created with an access modifier like public, this access modifier makes this class accessible to all other classes in Java. Hence, our class A is accessible to all other Java classes.

    • The name of the Java file with .java extension should match with the name of public class defined in it. Hence, our Java program with class A is saved in a file named A.java.

    Note : We can only define only one public class in one .java file.



  2. public static void main(String[] ar)

    • A Java program begins with a call to the main() method of the class, because it is an entry point to our program. Hence, this main() method is always defined with a public access modifier, which makes it accessible to the code outside the class in which it is defined, i.e class A and to execute it.

    • The main() method is static in nature, which means that this method can be directly accessed/called, without having to instantiate or create an object of class in which it is defined, i.e. class A.

    • The return type of main method is void, which means that this method will not return any value after it finishes its execution.

    • The main method has one parameter named ar, which is an array of class type String, it means we can pass one or more objects of String class to the main method.

      If you don't understand much of what is explained so far in this program, don't worry, you will learn more about array, static keyword and String class in the upcoming articles.




  3. System.out.println("Hello from Java");

This line simple outputs the string Hello from Java on the command prompt, where :



Advertisement



How to compile a Java program?


In order to compile our program, we need to call the Java compiler with a command - javac, with the name of our file that contains the Java class. We have saved our Java program in a file A.java in the D: Drive location. Hence, in order to compile our A.java file from command prompt, first we need to reach the location where this Java file is saved and type in the command -

D:\>javac A.Java

This will create a file, A.class. This .class file is a compiled bytecode form of our Java program, which is going to be required and read to execute our Java program.




Note :


There is a space between the command javac and name of the file.




How to run a Java program?



D:\>Java A
Hello from Java





Please share this article -





< Prev
Next >
< Java Environment Setup
Primitive Data Types >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement