Getting Started with Java
Saturday 27 February 2016, by Shahid Khan
Java is one of the most popular programming languages for building all types of software programs. It comes in three different flavours:
- Standard Edition: It is the entry point to the Java programming. You will learn all the building blocks of Java and how you can develop standalone applications.
- Enterprise Edition: Once you get familiarise with the Standard Java features and smoothly develop programs and applications, you can dive into the Java Enterprise Edition. Here you learn to develop scalable and robust web applications.
- Micro Edition: Using this technology you can develop applications for embedded and mobile devices.
TIOBE uses a complex algorithm and different search engines to determine the popularity of programming languages. As you can see in the image below, as of September 2015, Java is at the top of all the programming languages. TIOBE index can be used in making decisions on which computer language to adopt and developing software systems. For more information, you can visit at the link below:
http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
Where to start?
Step 1:
First you need to download and install Java SE Software Development Kit (JDK) from Oracle here http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
The Java Development Kit has all the necessary tools to compile and execute the Java code.
Step 2:
Next you need an editor to write the Java code. There are many free editors are available, they are also known as Integrated Development Environment (IDE).
List of some free Java IDEs:
- Eclipse
- IntelliJ
- Netbeans
- BlueJ
If you are a beginner learner, I suggest you download and install BlueJ IDE from http://www.bluej.org/. It is simple, easy and you can focus more on learning and writing Java code instead of learning to use the IDE.
After installing the BlueJ, you can start the IDE from the desktop icon.
Step 3:
After starting the BlueJ, you can create a project and start writing the Java programs.
Writing your first Java program
In BlueJ first create a project by the name of "BasicPrograms" by using the menu on the top left side of the frame.
After creating a project a new window appears where you will create a class to write the Java code.
In Java, a class is the basic component of a program. All the Java code in written inside the class. The file name and the class name must be same.
Now write this code in the class:
public class HelloWorld {
}
The line public class HelloWord is the declaration of the class and all the code will be written inside the curly brackets or braces. The opening brace is the start of the class and the closing brace ends the class. All the code will be written inside these braces.
The word public is a Java keyword which means that the class is public within the Java program or system and can be assessed by any other class. More on this will be explained and discussed later. The class is another keyword used just to announce that you are going to declare the class. HelloWorld is the name of the class and your program.
Remember both keywords are in lowercase and will create a compilation error if written in different combination of cases.
The above code will compile successfully but it will not execute or run and do nothing as there are no instructions in it.
The other thing you should remember that to execute the Java code we need a special standard block, which is called main.
In programming terms it is called as method. A main method in Java is a special block which will act as execution or entry point of a Java program. If you don’t write the main method, you will not able to run the Java program. It does not matter how many classes you have in a Java program or application, you only need one main method in order to trigger and start the program. The main method has its own declaration and will remain same regardless of the complexity of programs. The main method must be declared inside the class. Now the code is modified by adding the main methods to the class as follows:
public class HelloWorld {
public static void main (String [] args) {
System.out.println(“Hello World!”);
}
}
main method has its own open and close braces which makes it body and will be executed every time a Java program is run.
The main method uses three different keywords in its declaration. We will dissect the main method later and learn about these keywords. For now just remember that you need a special method which is called main method inside a class and you write all further instructions inside main.
Inside the main method it includes a statement to print a message "Hello World!".
The statement System.out.println() refers to the screen of the device you are writing the program. When you execute the program, you will see the Hello World! message printed in the command line of the computer. Also note that the statement in Java is terminated by a semicolon (;). It simply says that the instruction is complete and there is no more code and the line is terminated here.
This is the simplest program you can write in Java. All it does is to print the message "Hello World" on the command line interface of the machine you are running the Java program.
But it clearly shows how to use some of the most important building block of the Java program.
So you first need to declare a class, inside the class you then declare the main method and within the main method you write the instruction to print a message.
You have successfully understood and written your first Java program and remember you can learn anything just keep reading, practise and ask for help.