Saturday, July 30, 2016

Setup JAVA Environment and Code your First JAVA Program



Welcome to the second part of JAVA tutorial series. In this post you will be learning how to setup your own JAVA environment and start with coding in JAVA.

Should I really install a software on to the system to learn coding in JAVA ?

The answer to this question is both YES as well as NO. Are you surprised about 'NO' ? Well yes, you can actually start coding in JAVA. Thanks to the online IDEs (Integrated Development Environments), where in which some websites help us to code, compile and execute the JAVA code or any other programming languages code online Example: http://ideone.com/. But, installing the IDE offline i.e. installation of the IDE on our system will be a great option. In order to work with JAVA, we need something called JAVA environment installed in our system. You can get and install JAVA SE from the following link: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. The JDK and JRE will be dependent upon the operating system, architecture and a few other parameters. Download from the link and then install the file by following the instructions shown during installation.

Sometimes the JAVA environment variable will be installed by the JAVA software itself, if not then please do check whether the JAVA environment variables are set right. There are some situations where you won't get JAVA working in your system. In order to check if JAVA is working in your system, open command prompt/terminal and type the command java -version. If everything is alright, the JAVA version installed will be displayed. 


Note: If you get any error message or warnings, then please do refer this link: https://docs.oracle.com/javase/tutorial/essential/environment/paths.html, where you'll be shown how to set environment path and variables.

Everything is setup ! Now where to write the code ?

If you have reached this step, congratulations for you ! Now you are ready to write you first code in JAVA. There are many IDEs or editors for writing the JAVA code. There are many sophisticated IDEs too, but as of now you can consider the following editors:
  1. Text Editor (Notepad): You can use any of the text editors to write the code.
  2. Eclipse: It is a JAVA IDE developed by the eclipse open-source community and it can be downloaded from http://www.eclipse.org/.
  3. NetBeans: Even it is a JAVA IDE which is open-source and free. It can be downloaded from http://www.netbeans.org/index.html.
Hurray ! Finally you are ready to write the first JAVA code. Open Notepad or any text editor, write the following code:

1
2
3
4
5
6
7
class first
{
 public static void main(String[] args)
 {
  System.out.println("Welcome to JAVA 2 Digest");
 }
}

Now save the file as first.java (Note: Not first.txt, it is first.java). Open the command prompt and navigate to the directory where you have saved your file (first.java). Notice that the file name and the class name are same. It is a convention to follow, where we are notifying the compiler that it is the class which is the entry point of the program. The entry point here means the containment of main(String[] args) method. Assuming that you have opened the command prompt and navigated to the folder where you have saved the file. Run the command
> javac first.java

If everything is fine then the prompt will successfully go to the next line and the first.class file will be created in the same folder where first.java file is present. If you get any statement like 'javac' is not recognized as an internal or external command, operable program or batch file. It is the problem of not setting the environment variable or path.

This a common problem which occurs if you have not installed the JSE/JDK or if you have not set the environment variables. The solution for this is copy the path C:\Program Files\Java\jdk1.7.0_71\bin (Assuming that JSE/JDK is installed and OS installation directory is assumed to be C: ). After you have copied the path, open Control panel and click advanced system settings.


Once you have opened the edit window, click the variable value and click End key on your keyboard, it will navigate towards end of the line. Don't make any changes for the existing path variables. After the last semi-colon, paste the copied path (C:\Program Files\Java\jdk1.7.0_71\bin).


Click ok->ok->ok. Now assuming that you have followed the steps, close the command prompt which was open. Open the command prompt again, navigate to the java file and type the commands
> javac first.java

> java first

Congratulations you have executed your first JAVA program. If you get any errors while executing, then please do check the code for spelling mistakes and note that JAVA is case sensitive.

Explanation of the code:

1
2
3
4
5
6
7
class first
{
 public static void main(String[] args)
 {
  System.out.println("Welcome to JAVA 2 Digest");
 }
}

In JAVA a class is the collection of data members and member functions/methods. In the given code, 'class' is the keyword and 'first' is the class name. The JAVA code is written in blocks, so each block of code is enclosed within { } - Flower brackets. In the class named first, we have a method called 'main'. The 'main' method is the entry point for the program. 'public' is the access modifier, 'static' means the method belongs to the class and to all objects formed but not accessible by the objects (Don't get confused :P for now consider static as a reserved word). 'void' is the return type of the method 'main', which means 'main' method won't return anything, so it is 'void'. 'String' is a class in JAVA and whatever which is written to the console and taken as input from the console is considered as of String type and it is array of String. 'System' is a class in the java.lang package, out is a static member of the System class, and is an instance of java.io.PrintStream. 'println' is a method of java.io.PrintStream. This method is overloaded to print message to output destination, which is typically a console or file. 

That's it for now ! Hope this article was informative and knowledgeable. Your comments, queries and suggestions are most welcome. See you soon in the next tutorial :)

Wednesday, July 20, 2016

JAVA - Overview



Welcome to the first post of JAVA tutorial series. In this first post I will discuss about the overview of JAVA. This JAVA tutorial series is prepared for the beginners in order to help them understand the basic concepts of JAVA, towards the advanced concepts of JAVA. In the due course of the series, it will also be taught how to consider JAVA programming as a sport and take it to the other level into competitive programming. The target audience are expected to have prior knowledge about basics of computer programs and computer programming languages. Please pardon me if I go wrong at any step and do correct me, because I am still a student. Okay, now it is time to start with the introductory tutorial. "Welcome to the world of JAVA !"

Motivation to learn JAVA or any programming language

The first question which might rise is why to learn a programming language ? and how to learn ?

The answer to this is very simple. Learning a programming language is same as learning how to speak or write a regional language. The advantageous part in the process of learning a programming language is that, if you know how to speak and write in global language (English), then you can master any programming language. In any language, the set of alphabets arranged in particular order form a word, set of words in proper order form a sentence with certain meaning. The grammar specifies the rules of writing in a language. Similarly a programming language also has set of rules to be followed and they are called syntax and semantics. So learning a programming language is not at all difficult if you follow these basic rules. If you really have interest in learning a programming language, then you will definitely learn it. Have the faith and trust in yourself and also on the programming language which you are going to learn. I guarantee you that you will find JAVA an interesting subject and you will enjoy learning JAVA. All the best dear friends.
    
History of JAVA

Let us keep this section of the tutorial as simple as possible, because a little knowledge about the programming language background will help us to understand the language easily. Here are a few important points to be remembered about the history of JAVA,

1. JAVA was originally developed by Sun Micro-systems.
2. It was initiated by James Gosling who initiated Java language project in June 1991 for use in one of his many set-top box projects. The language, initially called ‘Oak’ after an oak tree that stood outside Gosling's office, also went by the name ‘Green’ and ended up later being renamed as Java, from a list of random words.
James Gosling

Oak Tree
3. Billions of devices run JAVA (Including ATM machines, smart phones etc.,)

These points are enough to remember about JAVA's history :P

The latest version of JAVA is JAVA Standard Edition (JSE) 8. For the support of JAVA on various platforms multiple configurations of JAVA are available such as J2EE (JAVA 2 Enterprise Edition), J2ME (Java 2 Micro Edition) for mobile applications. JAVA is called WORA (Write Once Run Anywhere). In Indian national language (Hindi) there is a joke about the versions of JAVA, have a look at that Meme :D 

LOL Moment
OOP (Object Oriented Programming) Concepts

JAVA is object oriented programming language. Now you might think what is this new thing called object oriented programming -_-

Object oriented programming language is the one which can be easily mapped to real world concepts. So, using JAVA we can code anything which actually exists in real world. The major OOPS concepts are as follows,

1. Object
2. Class 
Class and Objects

3. Data Abstraction
Data Abstraction and Encapsulation

4. Polymorphism
Polymorphism

5. Inheritance
Inheritance

6. Encapsulation
Encapsulation


Real life scenarios with major OOPs concepts,
Real Life Scenarios
Don't worry, if you don't know about these concepts. It will be covered in further tutorials.

Note: JAVA is not pure object oriented programming language, since it include some primitive data types.

Now don't start worrying about what data type is ! It will be taught in further tutorials while we code together.

Features of JAVA

Let us look at some features of JAVA,
1. Simple: JAVA is simple to learn as well as to code. The basic understanding of the syntax and semantics is enough to code anything in JAVA.
2. Object oriented: In JAVA, everything is an object. Since JAVA is based on object model, it can be extended easily.
3. Platform Independent: The JAVA application can run on any operating system. But,  it is JVM (JAVA Virtual Machine) dependent. So, if JVM is installed in the operating system, then JAVA applications can run. When JAVA code is compiled, it is not converted into platform specific machine code, instead it will be converted into platform independent byte code.
4. Secure: Coming to the security concern, here are two concepts to be followed,
         a. The security feature of JAVA enables us to create virus-free and tamper free systems.                  Authentication is based on public key encryption.
            b. JAVA is secure since it does not have any pointers. The memory leak is prevented here.
5. Robust: JAVA is said to be robust by its feature of eliminating the errors which might arise in some situations by checking the compile time and run time errors (Sometimes robustness is also witnessed due to the presence of Garbage Collector in JAVA).
6. Multi-threaded: With this feature of JAVA, it enables us to write programs which can perform multiple tasks simultaneously.
7. High Performance: The just in time (JIT) compilers enables high performance in JAVA.
8. Distributed: JAVA is designed for the distributed environment of the internet.
9. Architecture Neutral: JAVA compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.
10. Interpreted: Java byte code is translated rapidly to native machine code and it is not stored anywhere.

These are some of the major features of JAVA. In the next tutorial we'll see how to setup the environment to start with coding in JAVA.

Note: There is no full form of JAVA. JAVA is named after an island in Indonesia. That island is famous for its coffee. That's the reason why Java has a logo of a "Coffee Mug".

Hope this article was informative and knowledgeable. Your comments, queries and suggestions are most welcome. See you soon in the next tutorial :)  
     

Welcome to JAVA 2 Digest


Hello friends,

        My name is Narendra Kamath and I am currently pursuing Master of Computer Application at Amrita Vishwa Vidyapeetham, Mysuru, India. The main purpose of this blog is to educate people about programming in JAVA. JAVA is one of the most popularly used programming languages. Learning JAVA is very easy and is of fun. If you are a beginner in JAVA and have the urge in you to learn JAVA, then this blog is suitable for you to learn how to code in JAVA. Your comments, queries and suggestions are most welcome. Hope you all enjoy and learn JAVA.

Acknowledgements to,
My parents, teachers and friends.

Best Regards
Narendra Kamath G