Our social:

Wednesday, October 5, 2016

Java Programming Tutorial-Learn Java

About Java

Java is the most popular programming language in software industry today. Originally developed by James Gosling at Sun Microsystems in 1995, now it is controlled by Oracle Corporation. Have you heard about big fight between Oracle and Google regarding android coding? Well, it was all because of Java!
So let’s start our code journey of Java.
Before diving into theoretical details, here is the first simple most program ever possible in Java for you

public class LearnJava 
{
 public static void main(String []args) 
 {
  System.out.println("In life, sooner or later, you have to learn Java");
 }
}
Surprisingly, this program gives a message in the console: In life, sooner or later, you have to learn Java.
Now assume you have a computer, and you want to do Java coding. What you need to do?

Make your machine Java-Ready!

JDK stands for Java Development Kit, which is needed if you want to code in Java (of course you will do coding in Java). Now, just in case you say, I don’t need to code, I just want to run a Java program, then you need JRE only. JRE stands for Java Runtime Environment. The above link also contains a download link for JRE, but of course, you will not download that. Ok, you are clever enough to ask, don’t I need JRE at all ? How can I run the program I have developed in my machine ? Here is your answer : JDK contains JRE. So downloading JDK is enough to both build and run Java in your machine.
JDK ->JDK JRE
Where were we? Yes, you have downloaded JDK and installed it.
  •  Now do some more setting. Set the JAVA_HOME variable.
By default, the path for installed JDK is C:\Program Files\Java\jdk1.7.0_21.  Just copy the folder path (including jdk.<version>, in my case the version was 1.7.0_21. Your version can be different from this version).
  • Right-click the My Computer and select Properties.
  • Go to ‘Advanced Systems Setting’ link in the left navigation bar. Go to Advanced Tab.
  • Click Environment Variables button. Under System Variables, click New.
  • Enter the variable name as JAVA_HOME.
  • Enter the variable value as the installation path for the Java Development Kit. ( the path you have copied)
  • Add a semicolon (;) at the end.
  • Click OK.
Environment Setup
Unfortunately, you are not done! Sometimes this changes to get reflected, you need to restart your machine.
  •  Set PATH variable (this only needs if you want to run your java program with command prompt, which is exactly we are going to do now. Later we will tell you how life can be easy with some standard java editor, like: eclipse. If you use eclipse, you don’t need to set PATH, but for now, you need this)
Go to your JDK folder again, you will see a folder named ‘bin’ , go to the folder and copy the folder path from address bar. In my case it is : C:\Program Files\Java\jdk1.7.0_21\bin
Now execute these magic steps
  • Right-click the My Computer and select Properties.
  • Go to ‘Advanced Systems Setting’ link in the left navigation bar. Go to Advanced Tab.
  • Click Environment Variables button. . Under System Variables, you can see a variable named Path
  • Select the variable and Click on ‘Edit’
  • Go till the end of already existing text, append : ‘;C:\Program Files\Java\jdk1.7.0_21\bin’ (
No space, no semicolon at the end, nothing else; Do not copy paste from here, after semicolon paste the path you have copied from your address bar)
  • Click OK.
  • Click OK
  • Click Apply.
May be you have noticed the similarities among the values of JAVA_HOME and the value we have just appended in PATH. In fact, if you add \bin in the end of JAVA_HOME (C:\Program Files\Java\jdk1.7.0_21) , you get exactly what we have appended in the PATH. So alternatively you can also append ;%JAVA_HOME%\bin to PATH.

 Environment Setup
The set up instruction given above (for JAVA_HOME and PATH) is for Windows 7, if you have different operating system in your machine ( such as UNIX) then make sure you set the ‘JAVA-HOME’ and PATH correctly.
To check if JAVA home is set properly, you need to run the following command in command prompt. If it is set, it will show this message in Command prompt.
Java Setup
Done Java Setup!
Learn Java

Now Run a Program in Java!

  • Open notepad.
  • Copy paste the code you have seen in the first page
  • Save the file as LearnJava.java at any folder location of your choice ( no, you can’t choose any other name in the world while saving the file, nothing else but ‘LearnJava.java’)
  • Let’s say you have saved the file in F:\My Java Learning Begins.
  • Open Command prompt. Type F: , then cd My Java Learning Begins
  • Type javac LearnJava.java (this will compile the program). wait till the program compiles
  • In case you are curious, you can type dir command now.  You will see that in your folder F:\ My Java Learning Begins another file has been created. The files name is LearnJava.class.
  • Now type:  java LearnJava ( this will run the program)
  • You will see a message as the program output:
In life, sooner or later, you have to learn Java.
You have just successfully written and executed a java program. May be the program is easy (and of little significance), but you have crossed the first hurdle.
What’s going on behind?
Install Java
Let’s discuss a little about javac LearnJava.java command. We have already told that this will compile the program. What does this mean actually? This will do the following things:
  • Check all the syntax used in this program
  • If any error is found in the syntax, raise an error message, pointing the line number and the type of syntactical error it has found and stop compiling
  • If everything is syntactically correct, it will create the .class file. ( In our case LearnJava.class was created )
We are intentionally using the term syntax here. By the javac command you are telling the java compiler ( javac.exe):
“Hey bro, look I have written a nice java program, can you please check if my program is syntactically correct?”
If you program is syntactically correct, javac will not send any message. But, if there is any error, it will display the message. Now, consider a different scenario.
Let’s say, in your program you trying to divide 40 by 0, you will get a run time error, i.e while running the java code using command: java LearnJava (for our example).
However, as long as you have written the program according to the correct syntax, for example as below:
int a = 40;
int b = 40/0;
the compiler will not tell you anything. For the java compiler the program is syntactically correct, it does not care whether it is possible to divide 40 by 0 (Now, these type of errors are called “Exception”, and eventually we will surely talk about that).
After successful compilation (using command javac), the compiler will create .class file for the program.
Compile Java

Platform Independence!

Now, maybe you have heard that Java is platform independent programming language.
What is meant by platform independence?
It means that you can take a .class file from any machine and take the file in any other machine it will run and give the same output.
In our case we have created the LearnJava.class (because luckily, our program was syntactically correct ) . Now if your dear friend say “Give me your program, I want to run it”, you can just pass him/her the LearnJava.class file, and that’s it. Can you tell what he/she needs to run it?
As we have learned that in order to run our java program, we need JRE, so your friend will need to install JRE and he/she is set to run your program. It does not matter whether the operating system your friend is using in his machine is different from you as long as he/she have JAVA and JRE installed in his/her machine. This feature is called Platform Independence or Write Once Run Anywhere, one of the main characteristics of Java.
That’s it for today, in the next lecture we will analyze the program in more details and will tell you how to use one of the most popular JAVA IDE for programming: eclipse. Our life will be more pleasant then!

           

Install Eclipse for Java

0 comments:

Post a Comment

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you