Basics you must learn & make first Java program

0
178
java how to program

Before proceeding towards detailed programming, there are few pre-requisites and basic concepts that are really important & need to be understood. Once you will develop the understanding of these concepts, you will be able to understand them completely after working on various programming exercises.

How to create a Java file?

Creating of a Java file is the initial step for making any Java program. To do so, follow the steps below.

  1. Open your notepad or notepad++.

java tutorial how to create java file in notepad by arslan ud din shafiq

  1. If you don’t have notepad++, you can use notepad or you can download Notepad++ from https://notepad-plus-plus.org/download/v7.5.4.html
  2. To create new file, click on File then click OR use shortcut key Ctrl + n.

java tutorial how to create java program

  1. Now click Save As or use Ctrl + Alt + S to save file.

java tutorial how to create java program

  1. Name your file, add .java at end of name and don’t forget to use “”. Enclose file name in block quotes e.g. “FileName.java” and press Save to save the file.
  2. Now your Java file is ready. You can add java code in it and execute it using compile and run commands for Java.

Create your first code in Java

Before proceeding towards basic concepts, you will need to write a program in Java. The purpose of this program is to print “Hello World” text on screen. With the help of this program, some basic concepts would be explained.

 /*
  * This program prints "Hello World" on screen.
  */
 
  public class Hello{  //initialization of class
    //main method
    public static void main(String[] args){    
        //prints “Hello World”
        System.out.println("Hello World");     
                      
   }                 //end of main method block
                     
 }                 //end of class

How to compile and run Java code in command prompt ( CMD )?

  1. Open command prompt.
  2. Move to directory where you have placed java code.
  3. Use javac Hello.java command to compile code.
  4. Use java Hello command to run code.

how to compile java program

We have used file name as “Hello.java”. File extension (.java) shows that it is a file that contains java source code. In example no. 1, at line 0, “/*” is used to start block comment and at line 2 “*/” is used to end block comment. This syntax for comments is used when we have to comment multiple lines. At line 4, we have initialized class Hello. The name of class must be same as the name of our .java file, otherwise, it will give error as shown below:

how to compile java program
CLASS NAME AND FILE NAME DOES NOT MATCH

When we compile and run a Java program, it finds main method in class. The execution of code will be according to main method defined in a Java class. In absence of main method, Java code would be compiled successfully but the code will not run. It would give an error as shown below:

how to declare main method in java
MAIN METHOD IS NOT DEFINED

At line 4, public is an access control modifier, class is a keyword to define the class name, Hello is name of class which would be same as your Java file name. Left curly bracket “{” shows starting of a block that is ended at line 12 by using right curly bracket “}”. “//” is used for single comment in a code line. Comments are used to describe working of code to enhance understandability for other programmers, so that they may maintain your code. At line 6, public is access modifier, static is a keyword which means that method can be used directly with class name, there is no need to create object of class to call this method, void is return type, we will discuss return type in detail in next topics, String[] is array of type String through which we can pass String arguments from command line, args is variable name of String array, you can use any name but conventionally, we use args. At line 6, parentheses “ ( ) ” is part of method syntax in Java. Each method would be followed by parentheses which can be empty or we can define one or more arguments or parameters. The purpose of curly brackets at line 6 and line 10 is same i.e. to start and end a block. In Java to print anything on screen, we can use System.out.print() method which is ended by a semicolon ( ; ). Any text that needs to be printed should be passed in System.out.print() method and must be enclosed in double quotes ( “ “). Double quotes and semicolon are part of syntax, if you will miss semicolon or double quotes, error will occur as shown below:

error in java
SEMICOLON IS MISSING
error in java
DOUBLE QUOTES ARE MISSING

LEAVE A REPLY

Please enter your comment!
Please enter your name here