Java Tutorials

Basics you must learn & make first Java 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++.

  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.

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

  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.

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:

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:

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:

SEMICOLON IS MISSING
DOUBLE QUOTES ARE MISSING
Arslan ud Din Shafiq

Alibaba Cloud MVP, Alibaba Cloud Technical Author, Dzone MVB, Software Engineer, Software Developer, Software Designer, Web Engineer, Web Developer, Web Designer, Database Designer, Database Developer, Cloud Computing Specialist, Linux Expert, Servers, 3D Modeling, Blogger, Facebook Map Editor, Google Map Editor

Recent Posts

I have 20 years of experience in computer security advancement. What I can recommend to regular PC users

If you are living in a digital world you must know how to protect your…

4 years ago

Software Development Life Cycle Model (SDLC)

Software Development Life Cycle Model, also known as SDLC or Software Development Process, is base…

4 years ago

How to Install Go Lang on CentOS 8

Go, often referred to as golang is a modern open-source programming language created by Google…

4 years ago

SE Ranking in [2020]. What is SEO in marketing?

Torque published an article on October 18, 2016, about WordPress statistics. According to this article,…

4 years ago

PostgreSQL Version via Command Line & SQL Shell

PostgreSQL, often known simply as Postgres, is an open-source general-purpose object-relational database management system. In…

4 years ago

Google SEO: 15 Best ways about how can I do SEO for Free?

SEO is free as well as paid. To achieve SE ranking, money is not enough.…

5 years ago