< Prev
Next >



First Java Program




To write your first 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 so much 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");
}
}





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





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





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

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






How to compile a Java program?


In order to compile our program, we need to call Java compiler with a command javac with the name of our file that contains the Java class. We have saved our Java program 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 has created a file, A.class. This .class file is a compiled bytecode form of our Java program, which is going to be required and read in order to run 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 -

Facebook Google Pinterest Reddit Tumblr Twitter




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

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



COMING SOON
-
PYTHON