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.
Creating of a Java file is the initial step for making any Java program. To do so, follow the steps below.
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
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:
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:
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:
If you are living in a digital world you must know how to protect your…
Software Development Life Cycle Model, also known as SDLC or Software Development Process, is base…
Go, often referred to as golang is a modern open-source programming language created by Google…
Torque published an article on October 18, 2016, about WordPress statistics. According to this article,…
PostgreSQL, often known simply as Postgres, is an open-source general-purpose object-relational database management system. In…
SEO is free as well as paid. To achieve SE ranking, money is not enough.…