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 -
Every Java program starts with a class.
A class describes its behaviour through its methods, where a method
is a place where the logic of a class is stored.
A Java class may have zero or more methods or functions(for those who are coming from C/C++).
Java naming conventions -
Technically, every class name in Java should start with a capital alphabet.
Every method defined in a Java class should start with a small alphabet and
follows a camelCase style.
Note :
Java compiler doesn't throw a compile error,
if a class name starts with a lowercase letter, but it's still better to follow the conventions defined by Java because it improves the readibility of your code.
Java is a case-sensitive language,
hence a class containing two variables with one named a and the other named A, are treated differently.
Creating our first Java program
We are creating a Java program which is saved in a file named A.java.
Note: 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 -
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.
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.
System.out.println("Hello from Java");
This line simple outputs the string Hello from Java on the command prompt, where :
System is a predefined class.
out is an output stream through which we are sending the output to console or command prompt, to make it visible to you.
println() is a method that prints the string of characters passed to it, at the command prompt .
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
In order to run our program, we need to execute the Java command - Java, with the name of our class(without .java or .class extension) that contains the main() method,
And, at the end of this command, we press Enter to get the output of our program.
And, this concludes a detailed description of the concepts of a simple Java program.