Java




Java is one of the world's most important and widely used computer languages, and it has held this distinction for many years. Unlike some other computer languages whose influence has weared with passage of time, while Java's has grown. As of 2015, Java is one of the most popular programming languages in use, particularly for client-server web applications, with a reported 9 million developers using and working on it.
Java was developed by James Ghosling, Patrick Naughton, Mike Sheridan at Sun Microsystems Inc. in 1991. It took 18 months to develop the first working version.
The initial name was Oak but it was renamed to Java in 1995 as OAK was a registered trademark of another Tech company.
Evolution of Java :
Java was initially launched as Java 1.0 but soon after its initial release, Java 1.1 was launched.
Java 1.1 redefined event handling, new library elements were added.
In Java 1.2 Swing and Collection framework was added and suspend(), resume() and stop() methods were deprecated from Thread class.
No major changes were made into Java 1.3 but the next release that was Java 1.4 contained several important changes. Keyword assert, chained exceptions and channel based I/O System was introduced.
Java 1.5 was called J2SE 5, it added following major new features :
• Generics
• Annotations
• Autoboxing and autounboxing
• Enumerations
• For-each Loop
• Varargs
• Static Import
• Formatted I/O
• Concurrency utilities
Next major release was Java SE 7 which included many new changes, like :
• Now String can be used to control Switch statement.
• Multi Catch Exception
• try-with-resource statement
• Binary Integer Literals
• Underscore in numeric literals, etc.
And the latest addition to the lot is, Java SE 8, it was released on March 18, 2014. Some of the major new features introduced in JAVA 8 are,
• Lambda Expressions.
• New Collection Package java.util.stream to provide Stream API.
• Enhanced Security.
• Nashorn Javascript Engine included.
• Parallel Array Sorting.
• The JDBC-ODBC Bridge has been removed etc.
Java is widely used in every corner of world and of human life. Java is not only used in softwares but is also widely used in designing hardware controlling software components.
There are more than 930 million JRE downloads each year and 3 billion mobile phones run java. Following are some other usage of Java :
1. Developing Desktop Applications
2. Web Applications like Linkedin.com, Snapdeal.com etc
3. Mobile Operating System like Android
4. Embedded Systems
5. Robotics and games etc.
1) Simple :
Java is easy to learn and its syntax is quite simple, clean and easy to understand.The confusing and ambiguous concepts of C++ are either left out in Java or they have been re-implemented in a cleaner way.
Eg : Pointers and Operator Overloading are not there in java but were an important part of C++.
2) Object Oriented :
In java everything is Object which has some data and behaviour. Java can be easily extended as it is based on Object Model.
3) Robust :
Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time error checking and runtime checking. But the main areas which Java improved were Memory Management and mishandled Exceptions by introducing automatic Garbage Collector and Exception Handling.
4) Platform Independent :
Unlike other programming languages such as C, C++ etc which are compiled into platform specific machines. Java is guaranteed to be write-once, run-anywhere language. On compilation Java program is compiled into bytecode. This bytecode is platform independent and can be run on any machine, plus this bytecode format also provide security. Any machine with Java Runtime Environment can run Java Programs.

5) Secure :
When it comes to security, Java is always the first choice. With java secure features it enable us to develop virus free, temper free system. Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure.
6) Multi Threading :
Java multithreading feature makes it possible to write program that can do many tasks simultaneously. Benefit of multithreading is that it utilizes same memory and other resources to execute multiple threads at the same time, like While typing, grammatical errors are checked along.
7) Architectural Neutral :
Compiler generates bytecodes, which have nothing to do with a particular computer architecture, hence a Java program is easy to intrepret on any machine.
8) Portable :
Java Byte code can be carried to any platform. No implementation dependent features. Everything related to storage is predefined, example: size of primitive data types.
9) High Performance :
Java is an interpreted language, so it will never be as fast as a compiled language like C or C++. But, Java enables high performance with the use of just-in-time compiler.
A constructor is a special method that is used to initialize an object.
Every class has a constructor,if we don't explicitly declare a constructor for any java class the compiler builds a default constructor for that class.
A constructor does not have any return type.
A constructor has same name as the class in which it resides. Constructor in Java can not be abstract, static, final or synchronized. These modifiers are not allowed for constructor.
class Car
{
 String name ;
 String model;
 Car( )    //Constructor 
 {
  name ="";
  model="";
 }
}		


There are two types of Constructor
• Default Constructor
• Parameterized constructor
Each time a new object is created at least one constructor will be invoked.
Car c = new Car()       //Default constructor invoked
Car c = new Car(name); //Parameterized constructor invoked

Constructor Overloading :
class Cricketer 
{
 String name;
 String team;
 int age;
 Cricketer ()   //default constructor.
 {
  name ="";
  team ="";
  age = 0;
 }
 Cricketer(String n, String t, int a)   //constructor overloaded
 {
  name = n;
  team = t;
  age = a;
 }
 Cricketer (Cricketer ckt)     //constructor similar to copy constructor of c++ 
 {
  name = ckt.name;
  team = ckt.team;
  age = ckt.age;
 }
 public String toString() 
 {
  return "this is " + name + " of "+team;
 }
}

Class test
{
 public static void main (String[] args)
 {
  Cricketer c1 = new Cricketer();
  Cricketer c2 = new Cricketer("sachin", "India", 32);
  Cricketer c3 = new Cricketer(c2 );
  System.out.println(c2);
  System.out.println(c3);
  c1.name = "Virat";
  c1.team= "India";
  c1.age = 32;
  System .out. print in (c1);
 }
}


output :
this is sachin of india
this is sachin of india
this is virat of india
Inheritance is one of the key features of Object Oriented Programming.
Inheritance provided mechanism that allowed a class to inherit property of another class. When a Class extends another class it inherits all non-private members including fields and methods. Inheritance in Java can be best understood in terms of Parent and Child relationship, also known as Super class(Parent) and Sub class(child) in Java language.
Inheritance defines is-a relationship between a Super class and its Sub class. extends and implementskeywords are used to describe inheritance in Java.

Let us see how extend keyword is used to achieve Inheritance.
class Vehicle. 
{
  ......  
}
class Car extends Vehicle 
{
 .......  //extends the property of vehicle class.
}


Types of Inheritance :
1. Single Inheritance
2. Multilevel Inheritance
3. Heirarchical Inheritance
NOTE :Multiple inheritance is not supported in java
A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process).
Multithreaded programs contain two or more threads that can run concurrently. This means that a single program can perform two or more tasks simultaneously.
For example, one thread is writing content on a file at the same time another thread is performing spelling check.
In Java, the word thread means two different things.
• An instance of Thread class.
• or, A thread of execution.
An instance of Thread class is just an object, like any other object in java. But a thread of execution means an individual "lightweight" process that has its own call stack. In java each thread has its own call stack.

The main thread :
Even if you don't create any thread in your program, a thread called main thread is still created. Although the main thread is automatically created, you can control it by obtaining a reference to it by calling currentThread() method.
Two important things to know about main thread are,
• It is the thread from which other threads will be produced.
• main thread must be always the last thread to finish execution.
class MainThread
{
 public static void main(String[] args)
 {
  Thread t=Thread.currentThread();
  t.setName("MainThread");
  System.out.println("Name of thread is "+t);
 }
}

Output :
Name of thread is Thread[MainThread,5,main]

Life cycle of a Thread :


1. New : A thread begins its life cycle in the new state. It remains in this state until the start() method is called on it.
2. Runable : After invocation of start() method on new thread, the thread becomes runable.
3. Running : A method is in running thread if the thread scheduler has selected it.
4. Waiting : A thread is waiting for another thread to perform a task. In this stage the thread is still alive.
5. Terminated : A thread enter the terminated state when it complete its task.
Home Test Tutorials Program About Us
www.000webhost.com