Java Tutorial for Beginners : Part-2 First Java Program

Intro
Hope you have gone through my first tutorial on Java - Java Tutorial for Beginners : Part-1 OOP Concepts. All the points mentioned up there are very important, and should flow in you blood if you want to become a good Java programmer. But before implementing these concepts into your program, lets take a look at the basics of Java programming.

Requirements
To run C/C++ you install TurboC in your system to edit, compile and execute the program. Similarly any idea of what is required for Java? You only need to download and install the environment required for it(Of course you have tons of IDE's which will be put up later). So follow the steps to install JDK(Java Development kit) in your PC.
  • Go to this link and update the latest version of JDK - www.oracle.com
  • After downloading, double click on the setup to install it.
  • After successful installation, you need to setup a few parameters to run Java programs.
    • Right click on My Computer and select Properties
    • In Advanced, you will find a button named - Environment Variables
    • Click on it and add 3 New variables(These are not required at this point of time. It will be needed in future when you use an IDE. However, the last one(with video) is mandatory to run Java programs)
    • You have to add 3 new variables
      • CLASSPATH
      • JAVA_HOME
      • PATH
    • The above names are the Variable Names. Add the Variable Values as path to your installed JDK(Screenshot attached)
    • You also have to add the Java bin path to the PATH variable in System Variables(below the previous variables) This video will explain things the best way - 
The next thing you require is an editor. You can write down programs in Notepad as well, which helps you a lot to understand the basic syntax. I recommend you to practice it.
That's it! Your system is ready to roll Java!

First Program
Now its time to do some coding. But where? Click on Start->Accessories->Notepad. Yes, you will be coding in Notepad(as a start). Like all programming methodologies, you first program will be Hello World which simply gives an output of Hello World. Follow the steps to do this

  • Open Notepad
  • The following code will print Hello World on the console
public class HelloWorld
 {
public static void main(String[] args) {
System.out.println("Hello World");
}
 }

  • Save the file with the name HelloWorld.java(Same as the class name)
  • Now open Command Prompt(Ctrl+R - > cmd)
  • Go to the path where you saved the file HelloWorld.java
  • Type the command - javac HelloWorld.java
    If you have any syntax problems, it will be shown here.
  • Type the command - java HelloWorld to execute it. 
That was you first Java Program!

Second Program
You can try making your own changes on the above program. However, the program would only give output. Now your aim should be to take some values as input, process it, and give an output. This process is not as easy as in C/C++. The following code takes in two values, and gives its sum as output. Try it out.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; 
public class test {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num1,num2,sum;
System.out.println("Enter first number");
num1=Integer.parseInt(br.readLine());
System.out.println("Enter second number");
num2=Integer.parseInt(br.readLine());
sum=num1+num2;
System.out.println("The sum is :"+sum);
}
}
  • The first three lines imports certain inbuilt library files so that we can use it in our particular program
  • BufferedReader is used to get some value as an input. It has a concept similar to a Buffer.
  • Integer.parseInt converts a String to Integer(Everything in Java is read as a String, so everytime you read any other type, you have to do this type conversion.
Don't be Scared!
I guess a few would be totally confused reading all these, but trust me, these things would prove to be useful and powerful once you are into it. Please try doing a variety of programs like 
  • Write a program to find the square of a number
  • Write a program to find the factorial of a number
  • Write a program to generate fibonacci series etc.
I'm crafting these tutorials with a mindset that you have basic knowledge in programming. Without those you cannot absorb these. Let me know if you have any queries, and please do comment if you have any suggestions. 
In the next tutorial, you  would be applying the understanding you had on these two tutorials.

Tutorials

Java Tutorial for Beginners : Part -1 OOP Concepts
Java Tutorial for Beginners : Part -2 First Java Program