Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

How to create and run a simple Java Program ?.

Hello friends, Lets begin our hub with a simple Java program that prints or displays a message on Console as "Welcome Java Programs on Hubpages!". If we go in basics a Console is nothing but a simple display device of computer, where we can even put text that is input to program. Lets look at the program code that displays the text we want.

public class WelcomeJavaPrograms {
  public static void main(String[ ] args){
    System.out.println("Welcome Java Programs on Hubpages!");   

Output of the program
Line 1 :- It starts by defining a class by the name WelcomeJavaPrograms. In Java programming language every program whether small or large must start with atleast one class. If we follow the naming convention of classes in Java, we have to name the class starting with an Uppercase letters. Important thing to note here is class name should match with the source file name which you will be using to store the code. So in our case above the source file name where above code goes must be The entire class definition should come between opening and closing curly braces that is between { }.  Line 2 :- It starts by defining a method named as main. The main method is the entry point for all Java programs. Each program small or large, if you want to execute you should have a method named main in there. The execution point starts from there, as JVM (Java Virtual Machine) first searches the main method. If we look into what is a method ? . We can analyze it as block of statements that needs to be executed step-by-step. When we look into our example program we see that main method has only statement to execute that is System.out.println statement. Generally not going into much deep as of now just have an idea that this statement displays message "Welcome Java Programs on Hubpages!" on the console window. After each statement in Java we usually place a semi-colon to indicate compiler that particular statement is ended there. In above simple program many keywords are been used as class, public, main, static and void. These keywords have a special or significance meaning to compiler. Compiler understand what a particular keyword does in a program. Here if we look into the details of public keyword, it is an modifier which generally controls class or method visibility to other classes. When we declare public infront of class or method than that class or method are accessible by other classes which are outside. If we use keyword as private than its working is quite opposite to public keyword and it makes class or method private to code which is been defined outside the class. In our program above main method is been declared as public because it is been accessed by the code outside of the class. If we use static keyword before main method, we mean that main method is static and gets called without having to instantiate or making objects of the class. Since main method is been called by Java Virtual Machine no objects are needed for calling the main method. The last void keyword just gives the compiler information that main method would not return any value to the Java Virtual machine. Generally when we call any method we might have to pass some information to the method, which it uses to execute some statements. We usually pass this information as a parameters within the angle brackets of the method. If there are no parameters we define method with empty angle brackets i.e (). In our above program main method requires a argument which is String array named as String[] args having name of the parameter as args. This String of array receives information of the command line which it needs when executing the main method.   Line 3:- Brief understanding about the System.out.println() method, generally println method prints the string passed to it. System is a class which has access to the system and out is the output stream which connects the console and calls println method which prints the message to the console passed to it as argument.   Line 4:- It starts with a Java comment. Comments are very much important in each and every Java program. Comments allow us to place information about program in code. Generally its been avoided by the compiler and has no effect on execution of Java program. Usually it helps in debugging the code and helps other programmers to understand the code without going through whole code. In Java there are single-line comment and multi-line comment. Single-line comment usually starts with a two forward slashes (//). Multi-line comments are in between /* and */. Whenever compiler upon compilation of program sees // it avoids all written text after that on the same line. Whenever compiler sees /*, it tries to find */ and ignores all the code between them. T Let us look at examples of single-line comment and multi-line comment: // I am new to Hubpages, please help me!. /* I am writing my first Java program on Hubpages kindly have a look and comment. */ In Java block generally is a group of statements and starts by { and ends by }. Every program in Java has a block which encloses a class and also methods have there own blocks that to starts and ends with { and }. In every Java program each and every opening braces must have its closing brace. Java is a case-sensitive language, so writing class keyword as Class would result in compilation error. There are many syntax rules that Java follows and if you fail to obey those rules than on compiling program you will get compilation errors. Generally, missing of braces, missing of semi-colons, mistyping of keywords would result in compilation errors.
© 2021 Learn Java by Examples Template by Hubberspot