Introduction to JAVA
What is JAVA?
JAVA is a high level programming language and a platform to run many software applications . Like many other programming languages, java has its own way of compilation of a program code. Java is a widely used programming language right from it’s inception. Nearly 8 billion applications are developed using java as a platform.
Why to use JAVA?
Java is a simple and easy to learn programming language. It is most usable when compared with other programming languages because of it’s reliable nature and Robustness. It is also called as platform independent language as it holds a statement ‘ write once, use any where’, justifying that we write program once and can run it across multiple systems (to know more about platform independency, Read this). Java is built with a special compilation technique using JRE which stands for JAVA RUNTIME ENVIRONMENT and requires JVM (JAVA VIRTUAL MACHINE) to run.
Java combines both the approaches of compilation and interpretation. First, java compiler compiles the source code into bytecode. At the run time, Java Virtual Machine (JVM) interprets this bytecode and generates machine code which will be directly executed by the machine in which java program runs. So java is both compiled and interpreted language.
As the image shows, the source code written in java is first compiled to generate the byte code. JVM uses this byte code and interpreter interprets the code and runs it on a java machine.
Difference between C,C++,JAVA :
- It is a procedure oriented language.
- It is a top-bottom approach.
- No re-usability of code.
- Less reliable when compared to java.
- No concept of inheritance.
- No Exceptions are handled.
- C is a platform dependent language.
- Data hiding and Data Binding is not possible.
- C++ supports both procedural and Object Oriented programming concepts.(often its called semi object oriented as its main method is outside of class)
- Bottom to top approach.
- Code can be re-used.
- Better security when compared to C.
- Inheritance is supported.
- Exception can be handled well.
- It is a pure Objected Oriented Language.
- It is a platform independent language.
- It is interpreted and can be run on any java installed machine.
- It is highly secured language.
- Data hiding and Data Binding is possible.
- Inheritance is possible.
- Exception can be handled well.
Let us consider an example to show how powerful java is :
My friend and I were debating on writing an application. He wants to write it in C language and I said I will do it in java. Next day, we have our application ready. I tried to run his code on my machine but it did not work on my machine because my machine do not have the same environment on which it is developed. But, My application which is written in java is perfectly working in his machine.This is because java is a platform independent language and can run on any machine which has JRE. So Friends, Java, thus a most powerful language.
Features of JAVA:
They are also known as java buzzwords. The Java Features given below are simple and easy to understand. Following are the java buzz words.
- Platform independent
- Architecture neutral
- High Performance
Concepts of OOPS:
An object is a Real time entity or a reference to a class.
If there are two categories , Cars and Bikes, of multi brands. We can get the details using the reference for each of the category.
Cars- Maruthi Suzuki, Honda, Audi etc..
Bikes- TVS, Hero, Yamaha etc.
Now,suppose ‘c’ is an object or reference to Cars class, We can access all the public methods and public variables in Cars class using this reference.Similarly ‘b’ for Bikes class.
To create an Object to a class:
Cars c = new Cars(); //Where c is the object used to represent all the cars.
2. Class :
A Java Class is a collection of functions and variables , technically said, Class is a collection of Member functions and Data Members.
In Java, unlike C, everything must be written inside a class as its’s definition says.
Java program structure:
public class class_name
//declare global variables
// define methods
public class Cars
private String carName="";
public void displayList()
// perform operations
Encapsulation is Wrapping of data into a single entity . Otherwise also called as, holding the things together.
A class in java is a best example for encapsulation as it holds the methods and data variables all together as a single entity.
Polymorphism is showing of something in several forms. Polymorphism can be achieved in 2 ways.
1.Dynamic polymorphism :
The assignment of data types in dynamic polymorphism is known as late or dynamic binding. In dynamic binding method call occur based on the object (instance) type at Run time.
Example: method overriding
Method Overriding – This is the process of giving a new definition for an existing method in its child class. All object created at run time on the heap therefore actual binding is done at the runtime only.
If the assignment of data types is in compile time it is known as early or static binding. In static binding method call occur based on the reference type at compile time.
Eg: method overloading
Method Overloading – This means creating a new method with the same name and different signature. It uses early binding.
Inheritance is acquiring the properties of a parent class by the child class.
There are 5 types in Inheritance.
- Simple Inheritance (one parent class has one child class).
- MultiLevel Inheritance (A extends B class and C class extends B class).
- Multiple Inheritance (not supported in java as a class cannot extend two classes at same time).
- Hierarichal Inheritance (A extends B class and C also extends to B class i.e , one class has many child classes).
- Hybrid Inheritance (not supported in java as it is a combination of Multilevel and multiple inheritance concepts).
Abstraction means using simple things to represent complexity. We all know how to turn the TV on, but we don’t need to know how it works in order to enjoy it. In Java, abstraction means simple things like objects, classes, and variables represent more complex underlying code and data. This is important because it lets avoid repeating the same work multiple times.
Message passing is a form of communication used in parallel programming and object-oriented programming. Communications are completed by the sending of messages (functions, signals and data packets) to recipients.