JAVA
Introduction
History
of Java
James Gosling, Mike
Sheridan, and Patrick Naughton initiated the Java language project in June 1991.[20] Java
was originally designed for interactive television, but it was too advanced for
the digital cable television industry at the time.[21] The
language was initially called Oak after an oak tree that stood outside Gosling's office. Later the
project went by the name Green and was finally renamed Java, from Java coffee (Java coffee refers to coffee beans produced in the Indonesian island of Java. Java (Indonesian: Jawa) is an island of Indonesia). Gosling
designed Java with a C/C++-style syntax that system and application programmers
would find familiar.[23]
A Java
virtual machine (JVM)
is an abstract computing machine that enables a computer to run a Javaprogram. There are three notions of the JVM: specification,
implementation, and instance. The specification is a document that formally
describes what is required of a JVM implementation. Having a single
specification ensures all implementations are interoperable. A JVM
implementation is a computer program that meets the requirements of the JVM
specification. An instance of a JVM is an implementation running in a process that executes a computer program compiled into Java bytecode.
Java Runtime Environment (JRE) is a software package that contains what is
required to run a Java program. It includes a Java Virtual Machine
implementation together with an implementation of the Java Class Library. The Oracle Corporation, which owns the Java trademark, distributes a Java
Runtime environment with their Java Virtual Machine called HotSpot.
Java Development Kit (JDK) is a superset of a JRE and contains also
tools for Java programmers, e.g. ajavac compiler. Java Development Kit is provided free of charge
either by Oracle Corporation directly, or by the OpenJDK open
source project, which is governed by Oracle.
JAR files are fundamentally archive files, built on the ZIP file format and have the .jar
file extension. Computer users can create or extract
JAR files using the jar
command that comes with a JDK. They can also use zip
tools to do so; however, the order of
entries in the zip file headers is important when compressing, as the manifest often needs to be first. Inside a JAR,
file names are unicode text
How
to define a class in Java?
Answer:
General Syntax
class <class_name>
{
data member(s)/variable(s);
method(s)/function(s);
}
Example:
class
Sum
{
int a,b;
Sum()
{
System.out.println(“Hello”);
}
void result()
{
int c;
c=a+b;
System.out.println(c);
}
}
The
above class has name Sum that consists of two variables a, b and two functions
Sum() and result().
Here Sum() is a constructor function that (i) has the same
name as of the class name in which it belongs, (ii) it has no return type, not
even void, (iii) it is used to initialize or supplies the values into the
variables.
How
to define an object in Java?
Answer:
1.
Declare
a reference variable that can hold the address of any object.
2.
Allocate
the space for the object and store the address of the object into reference
variable.
General Syntax
<class name> <reference
variable name>;
<reference variable
name>=new constructor(parameter if any);
Example:
Sum
p;
p=new
Sum();
How To compile a .java file and how to execute it ?
Answer:
If we have a .java file for example Hello.java, then to compile this file we need to type
javac Hello.java
After compilation of this source file Hello.class file will be created. To execute this Hello.class file we need to type
java Hello
Garbage
Collection
Since objects
are dynamically allocated by using the new operator, you might be
wondering how such objects are destroyed and their memory released for later
reallocation. In some languages, such as C++, dynamically allocated objects
must be manually released by use of a delete operator. Java takes a
different approach; it handles deallocation for you automatically. The technique
that accomplishes this is called garbage collection. It works like this:
when no references to an object exist, that object is assumed to be no longer
needed, and the memory occupied by the object can be reclaimed. There is no
explicit need to destroy objects as in C++.
Primitive Data Types
Java
supports eight basic data types known as
primitive types. In addition,
it supports classes and arrays as composite data types, or reference types. The
primitive types are: a boolean type, a character type, four integer types, and
two floating-point types. The four integer types and the two floating-point
types differ in the number of bits that represent them, and therefore in the
range of numbers they can represent.
Table
2-2 summarizes these primitive data types.
Type
|
Contains
|
Default
|
Size
|
Range
|
boolean
|
true or false
|
false
|
1 bit
|
NA
|
char
|
Unicode character
|
\u0000
|
16 bits
|
\u0000 to \uFFFF
|
byte
|
Signed integer
|
0
|
8 bits
|
-128 to 127
|
short
|
Signed integer
|
0
|
16 bits
|
-32768 to 32767
|
int
|
Signed integer
|
0
|
32 bits
|
-2147483648 to 2147483647
|
long
|
Signed integer
|
0
|
64 bits
|
-9223372036854775808 to
9223372036854775807
|
float
|
IEEE 754 floating point
|
0.0
|
32 bits
|
±1.4E-45 to ±3.4028235E+38
|
double
|
IEEE 754 floating point
|
0.0
|
64 bits
|
±4.9E-324 to
±1.7976931348623157E+308
|
The Java Keywords
There are
50 keywords currently defined in the Java language (see Table 2-1). These
keywords,
combined
with the syntax of the operators and separators, form the foundation of the
Java
language. These keywords cannot be used as
names for a variable, class, or method.
Inheritance
Inheritance
means gaining the characteristic from previous generation to current
generation. In object oriented world, if a new class wants some members those
were already defined in some old class, then instead of rewriting the code in
new class, it gains them from old class. This gaining of members from old class
by a new class is called as inheritance in object oriented world.
The old class from where inheritance
is done is called “super class” or “base class” and the new class
that actually inherits is called “subclass” or “derived class”.
There
are basically 4 types of inheritance in object oriented world:
1. Single Level Inheritance
2. MultiLevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
Note: Java does not support multiple inheritance
with respect to classes.
Example
of Single level Inheritance
TestSingleInheritance.java
class A
{
inta,b;
A(int p,int q)
{
a=p;
b=q;
}
void area()
{
System.out.println("Area=
"+(a*b));
}
}
class B
extends A
{
int c;
B(int p,intq,int r)
{
super(p,q);
c=r;
}
void volume()
{
System.out.println("Volume=
"+(a*b*c));
}
}
public class
TestSingleInheritance
{
public static void main(String args[])
{
B b=new B(5,6,8);
b.area();
b.volume();
}
}
Here
we callB() constructor inside main function by passing three parameters. Then
first two parameters are used by B() constructor for
calling A() constructor so that data members a and b get initialized.
In order to call the superclass
constructor under the subclass constructor super keyword is used that must be
placed at the first line in the subclass constructor and takes parameters same
as of defined in superclass constructor.
The
third parameter C is initialized inside B() constructor.
Example
of Multi-level Inheritance
TestMultilevelInheritance.java
class A
{
int
a;
A(int
p)
{
a=p;
}
void
areaSquare()
{
System.out.println("Area
of square= "+(a*a));
}
}
class B extends A
{
int
b;
B(int
p,int q)
{
super(p);
b=q;
}
void
areaRectangle()
{
System.out.println("areaRectangle=
"+(a*b));
}
}
class C extends B
{
int
c;
C(int
p,int q,int r)
{
super(p,q);
c=r;
}
void
volume()
{
System.out.println("Volume=
"+(a*b*c));
}
}
public class TestMultilevelInheritance
{
public
static void main(String args[])
{
C
x=new C(5,6,8);
x.areaSquare();
x.areaRectangle();
x.volume();
}
}
Method Overloading
If a method name or function name is used for
multiple purposes, then this method name is said to be overloaded like a
person. This is called method overloading or function overloading.
For example if the function name area is used for calculating the area
of a circle as well as a rectangle, then this function name is used for
multiple purposes. So, here method area() is said to be overloaded.
Note: Since it is handled by compiler, it is
called as “Static Polymorphism”.
General
Criteria for method overloading:
Multiple functions must have the
same name but different parameters (difference in parameters exist in their
types as well as in their numbers).
Different ways to overload the method
There are two ways to overload the method in java
|
- By
changing number of arguments
- By
changing the data type
|
Note: In java method overloading is not possible
by changing the retuen type of the method.
1)Example of Method Overloading by changing the no. of arguments
In this example, we have created two overloaded methods, first sum method
performs addition of two numbers and second sum method performs addition of
three numbers.
class Calculation{
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
}
class Test
{
public static void main(String args[]){
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}
Output:
30
40
2)Example of Method Overloading by changing data type of argument
In this example, we have created two overloaded methods that differs in data
type. The first sum method receives two integer arguments and second sum method
receives two double arguments.
- class Calculation2{
- void sum(int a,int b){System.out.println(a+b);}
- void sum(double a,double b){System.out.println(a+b);}
-
- public static void main(String args[]){
- Calculation2 obj=new Calculation2();
- obj.sum(10.5,10.5);
- obj.sum(20,20);
-
- }
- }
Output:21.0
40
Que) Why Method Overloaing is not possible by changing the return type of
method?
In java, method overloading is not possible by changing the return type of
the method because there may occur ambiguity. Let's see how ambiguity may
occur:
because there was problem:
- class Calculation3{
- int sum(int a,int b){System.out.println(a+b);}
- double sum(int a,int b){System.out.println(a+b);}
-
- public static void main(String args[]){
- Calculation3 obj=new Calculation3();
- int result=obj.sum(20,20);
-
- }
- }
int result=obj.sum(20,20); //Here how can java determine which sum() method
should be called
Can we overload main() method?
Yes, by method overloading. You can have any number of main methods in a
class by method overloading. Let's see the simple example:
- class Overloading1{
- public static void main(int a){
- System.out.println(a);
- }
-
- public static void main(String args[]){
- System.out.println("main() method invoked");
- main(10);
- }
- }
Output:main() method invoked
10
Method
Overriding
If a subclass
contains a method with same definition as of a method defined in superclass,
then ultimately after inheritance the subclass naturally holds two methods with
same definition (definition means return type, method name and parameters).
But in reality, during execution time,
the subclass body of the function will replace the superclass body and
ultimately only one function exists in subclass i.e. the subclass version. This
is called method overriding.
Note: Since it is resolved during execution
time, it is called as “Dynamic Polymorphism or Runtime Polymorphism”.
Final and abstract keyword in inheritance:
Final
Keyword:
Final data
members/variables:
If a data member is declared with final keyword,
then it behaves as constant i.e. we can’t change the value of this data member.
final
static double pi=3.14;
Here
static keyword is used with final since only one copy of constant member is
sufficient. All object can access the constant value from there.
If
we declare final variable without static keyword, then individual copy will be
created for each object i.e., nothing but space wastage. Since all copies have
same value.
Final member
function:
If a member function is defined with final keyword,
then it behaves as constant i.e. we can’t override it.
final void
show()
{
System.out.println(“I
am here”);
}
Final class:
If a class is declared with final keyword, then it
behaves as constant i.e. no other class can inherit it.
final class
Test
{
}
Note: We can create the object of the final
class.
Abstract
Keyword:
Abstract variable:
By default all variables are implicitly abstract
i.e., we can change their values.
Example:
int p;
Or ,
abstract int p;
Abstract method:
Abstratc method has abstract keyword in its
definition for which overriding must be done.
Example:
abstract void show();
Abstract class:
A class declared with abstract keyword that
contains at least one abstract method is
called as abstract class for which inheritance is must.
Example:
abstract
class Test
{
void
show1()
{
System.out.println(“I
am here”);
}
abstract
void show2();
}
i.
Note: Abstract class may contain concrete method (method with
body).
ii.
Note: We can’t create object of abstract class directly.
Utility
of abstract method:
Interface
In order to provide common layout Java provides a
unit known as interface that can hold only constant data
members and abstract methods.
How
to define interface?
Answer:
General Syntax
Interface <interface_name>
{
Constant
data member(s)/variable(s);
Abstract
method(s)/function(s);
}
Example:
interface Salary
{
final static double x=0.15;
final static double y=0.15;
void show();
}
i.
Note: Like abstract class, we can’t create the object of interface
directly.
ii.
Note: Here the method definition requires no abstract keyword (it
is implicitly abstract).
Package
Package
is a collection of classes and interfaces. Package may be system defined as
well as user can create their own packages called user-defined packages.
Multiple system defined
packages collectively called as JSL( java standard library) or Java API (
Application Programming Interface).