Search Logic Blocks

Wednesday, December 25, 2019

Simple Program in Java

In Java, we start writing the programs by creating a new class starting with keyword class and any valid class name. Class names should be mixed case with first letter capitalized (recommended but not necessary). The program runs starting with the class having the main() method (which is the entry point for the program). 

We save the code in a plain text file named as the same name as the class name ending with .java extension. When the source file is compiled, a byte code file is generated ending with extension .class. The java application launcher (java) runs the same application .class on JVM (Java Virtual Machine) on different platforms (Windows, Unix / Linux, MacOS etc.) to convert the class file into specific machine instructions. (We will discuss later more about JVM.) Standard program structure in java programs is given below - 
public class [Class Name]{
// Define some variables
public static void main(String args[]) {
// Entry code
}
// Define some methods
}
The main() method is always the entry point in any java application. Its signature is always the same as given below - 
public static void main(String args[])
public is access modifier, and it means that the main method can be accessed from anywhere. static means, that there is no need to create an object to call the method and run the application. void keyword tells the system that main() method returns nothing. args[] is the String array, which represents the command line arguments passed. (Command line arguments are passed through command prompt on the terminal.)

Welcome Program
Let's start our first program. Mostly all the beginners write their first program by writing "Hello World" program. I will start the program by welcoming all to my Logic Blocks blog. "Welcome to Logic Blocks!!"

Class Welcome (Welcome.java)
public class Welcome 
{
public static void main(String args[]) {
System.out.println("Welcome to Logic Blocks!!");
}
}
Above code has two components - class definition and the main() method. Class definition begins with the keywords public (access modifier - optional) class (mandatory). Class is named as Welcome and it has only one method main() method. main() method has only one statement, which instructs the computer to print the Welcome message. We save the source file as Welcome.java. If you are using the command prompt to compile the source file, use the javac compiler command as below -

javac Welcome.java

When the file Welcome.java is compiled, java programming language compiler (javac compiler) generates a byte code class file named as Welcome.class. To run the class file, you have to give the command below -

java Welcome

And it will print the following message -

Welcome to Logic Blocks!!

Code can be found at Github link.

NOTE: So, now we are familiar of java program structure, and ready to learn more about this powerful language, and how we can use in our daily life to solve complex problems. As I already mentioned earlier, that this blog is not any kind of tutorial. We will focus more on the concepts and to create logic for solving the problems. Gradually, we will discuss more advanced topics.

Exercise: Install java (jdk) from the Oracle website. The instructions can be found in the Java tutorials mentioned in the post Java References. Write your first java program and print your name on the console / terminal.

No comments: