JAVA – A
BEGINING
Introduction:
Java is a
high-level, third generation programming language, like C, FORTRAN, Smalltalk,
Perl, and many others. You can use Java to write computer applications that
play games, store data or do any of the thousands of other things computer
software can do. Compared to other programming languages, Java is most similar
to C. However although Java shares much of C's syntax, it is not C. Knowing how
to program in C or, better yet, C++, will certainly help you to learn Java more
quickly, but you don't need to know C to learn Java. A Java compiler won't
compile C code, and most large C programs need to be changed substantially
before they can become Java programs. What's most special about Java in
relation to other programming languages is that it lets you write special
programs called applets that can be
downloaded from the Internet and played safely within a web browser. Java
language is called as an Object-Oriented
Programming language and before begining for Java, we have to learn the concept
of OOPs(Object-Oriented Programming).
Basic Concept of OOP(Object-Oriented Programming):
There are some basic concepts of object oriented programming as follows:
1. Object
2. Class
3. Data abstraction
4. Data encapsulation
5. Inheritance
6. Polymorphism
7. Dynamic binding
1. Object
Objects are
important runtime entities in object oriented method. They may characterize a
location, a bank
account, and a table of data or any entry that the program must handle.
Each object holds
data and code to operate the data. Object can interact without having to
identify the
details of each other‘s data or code. It is sufficient to identify the type of
message
received and the
type of reply returned by the objects.
2. Classes
A class is a set
of objects with similar properties (attributes), common behaviour (operations),
and common link to other objects. The complete set of data and code of an
object can be made a user defined data type with the help of class. The objects
are variable of type class. A class is a collection of objects of similar type.
Classes are user defined data types and work like the build in type of the
programming language. Once the class has been defined, we can make any number
of objects belonging to that class. Each object is related with the data of
type class with which they are formed.As we learned that, the classification of
objects into various classes is based on its properties (States) and behaviour
(methods). Classes are used to distinguish are type of object from another. The
important thing about the class is to identify the properties and procedures
and applicability to its instances.
3. Data Abstraction
Data abstraction
refers to the act of representing important description without including the
background details or explanations.Classes use the concept of abstraction and
are defined as a list of abstract attributes such as size, cost and functions
operate on these attributes. They summarize all the important properties of the
objects that are to be created.
Classes use the
concepts of data abstraction and it is called as Abstract Data Type (ADT).
4. Data Encapsulation
Data
Encapsulation means wrapping of data and functions into a single unit (i.e.
class). It is most useful feature of class. The data is not easy to get to the
outside world and only those functions which are enclosed in the class can
access it. These functions provide the boundary between Object‘s data and
program. This insulation of data from direct access by the program is called as
Data hiding.
5. Inheritance
Inheritance is
the process by which objects of one class can get the properties of objects of
another class. Inheritance means one class of objects inherits the data and
behaviours from another class. Inheritance maintains the hierarchical
classification in which a class inherits from its parents.Inheritance provides
the important feature of OOP that is reusability. That means we can include
additional characteristics to an existing class without modification. This is
possible deriving a new class from existing one.In other words, it is property
of object-oriented systems that allow objects to be built from other objects.
Inheritance allows openly taking help of the commonality of objects when
constructing new classes. Inheritance is a relationship between classes where
one class is the parent class of another (derived) class. The derived class
holds the properties and behaviour of base class in addition to the properties
and behaviour of derived class.
6. Polymorphism
Accent(Poly means ―many‖ and morph means ―form‖).
Polymorphism
means the ability to take more than one form. Polymorphism plays a main role in
allocate objects having different internal structures to share the same
external interface. This means that a general class of operations may be
accessed in the same manner even though specific activities associated with
each operation may differ. Polymorphism is broadly used in implementing
inheritance.It means objects that can take on or assume many different forms.
Polymorphism means that the same operations may behave differently on different
classes. Booch defines polymorphism as the relationship of objects many
different classes by some common super class. Polymorphism allows us to write
generic, reusable code more easily, because we can specify general instructions
and delegate the implementation detail to the objects involved.
7. Dynamic Binding
Binding refers to
the linking of a procedure call to the code to be executed in response to the
call. Dynamic binding means that the code related with a given procedure call
is not known until the time of the call at run time.
Dynamic binding
is associated polymorphism and inheritance.
Java History:
Java is a
general-purpose, object-oriented programming language developed by Sun
Microsystems of USA in 1991.Originally called Oak by James Gosling (one of the
inventor of the language). Java was invented for the development of software
for cunsumer electronic devices like TVs, tosters, etc. The main aim had to
make java simple, portable and reliable. Java Authors: James , Arthur Van ,
and others.
The first publicly available
version of Java (Java 1.0) was released in 1995. Over time new enhanced
versions of Java have been released. The current version of Java is Java 1.7
which is also known as Java 7. From the Java programming
language the Java platform evolved. The Java platform allows that the program
code is written in other languages than the Java programming language and still
runs on the Java virtual machine.
JAVA Features:
As we know that
the Java is an object oriented programming language developed by Sun
Microsystems of USA in 1991. Java is first programming language which is not
attached with any particular hardware or operating system. Program developed in
Java can be executed anywhere and on any system.
Features of Java are as follows:
1. Compiled and Interpreted
2. Platform Independent and portable
3. Object- oriented
4. Robust and secure
5. Distributed
6. Familiar, simple and small
7. Multithreaded and Interactive
8. High performance
9. Dynamic and Extensible
1. Compiled and Interpreted
Basically a
computer language is either compiled or interpreted. Java comes together both
these approach thus making Java a two-stage system. Java compiler translates
Java code to Byte code instructions and Java Interpreter generate machine code
that can be directly executed by machine that is running the Java program.
2. Platform Independent and portable
Java supports the
feature portability. Java programs can be easily moved from one computer system
to another and anywhere. Changes and upgrades in operating systems, processors
and system resources will not force any alteration in Java programs. This is reason why Java has become a trendy
language for programming on Internet which interconnects different kind of
systems worldwide. Java certifies portability in two ways.
First way is,
Java compiler generates the bytecode and that can be executed on any machine.
Second way is,
size of primitive data types are machine independent.
3. Object- oriented
Java is truly
object-oriented language. In Java, almost everything is an Object. All program
code and data exist in objects and classes. Java comes with an extensive set of
classes; organize in packages that can be used in program by Inheritance. The
object model in Java is trouble-free and easy to enlarge.
4. Robust and secure
Java is a most
strong language which provides many securities to make certain reliable code.
It is design as garbage –collected language, which helps the programmers
virtually from all memory management problems. Java also includes the concept
of exception handling, which detain serious errors and reduces all kind of
threat of crashing the system.Security is an important feature of Java and this
is the strong reason that programmer use this language for programming on
Internet.The absence of pointers in Java ensures that programs cannot get right
of entry to memory location without proper approval.
5. Distributed
Java is called as
Distributed language for construct applications on networks which can
contribute both data and programs. Java applications can open and access remote
objects on Internet easily. That means multiple programmers at multiple remote
locations to work together on single task.
6. Simple and small
Java is very
small and simple language. Java does not use pointer and header files, goto statements,
etc. It eliminates operator overloading and multiple inheritance.
7. Multithreaded and Interactive
Multithreaded
means managing multiple tasks simultaneously. Java maintains multithreaded
programs. That means we need not wait for the application to complete one task
before starting next task. This feature is helpful for graphic applications.
8. High performance
Java performance
is very extraordinary for an interpreted language, majorly due to the use of
intermediate byte code. Java architecture is also designed to reduce overheads
during runtime. The incorporation of multithreading improves the execution
speed of program.
9. Dynamic and Extensible
Java is also
dynamic language. Java is capable of dynamically linking in new class,
libraries, methods and objects. Java can also establish the type of class
through the query building it possible to either dynamically link or abort the
program, depending on the reply.
Java program is
support functions written in other language such as C and C++, known as native
methods.
Java Virtual machine:
As we know that
all programming language compilers convert the source code to machine code.Same
job done by Java Compiler to run a Java program, but the difference is that
Java compiler convert the source code into Intermediate code is called as byte
code. This machine is called the Java Virtual machine and it exits only inside
the computer memory.
The Virtual
machine code is not machine specific. The machine specific code is generated.
Java Object Framework act as the intermediary between the user programs and the
virtual machine which in turn act as the intermediary between the operating
system and the Java Object Framework.
Java Environment:
Java environment
includes a number of development tools, classes and methods. The development
tools are part of the system known as Java Development Kit (JDK) and the
classes and methods are part of the Java Standard Library (JSL), also known as
the Application Programming Interface (API).
Java Development kit (JDK) – The JDK comes with a set of tools that are used for developing and
running Java program. It includes:
1. Appletviewer ( It is used for viewing the applet)
2. Javac (It is a Java Compiler)
3. Java (It is a java interpreter)
4. Javap (Java diassembler,which convert byte code into program
description)
5. Javah (It is for java C header files)
6. Javadoc (It is for creating HTML document)
7. Jdb (It is Java debugger)
For compiling and running the program we have to use following commands:
a) javac (Java compiler)
In java, we can use any text editor for writing program and then save
that program with ―.java‖ extension. Java compiler convert the source code or program in byte
code and interpreter convert ―.java‖ file in ―.class‖ file.
Syntax:
C:\javac filename.java
If my filename is ―abc.java‖ then the syntax will be
C:\javac abc.java
b) java(Java Interpreter)
As we learn that, we can use any text editor for writing program and
then save that program with ―.java‖ extension.
Java compiler convert the source code or program in byte code and
interpreter convert ―.java‖ file in ―.class‖ file.
Syntax:
C:\java filename
If my filename is abc.java then the syntax will be
C:\java abc
Development
Process with Java
The programmer writes Java source code in a text
editor which supports plain text. Normally the programmer uses an Integrated Development Environment (IDE) for programming. An IDE supports the
programmer in the task of writing code, e.g. it provides auto-formatting of the
source code, highlighting of the important keywords, etc.
At some point the programmer (or the IDE) calls
the Java compiler (javac). The Java compiler creates the byte code instructions.
. These instructions are stored in .class files and can be executed by the Java Virtual
Machine.
Simple Java Program:
class HelloWorld
{
public static void main(String args[])
{
System.out.println(―This is my first
program‖);
}
}
The file must be named ―FirstProgram.java‖ to equivalent the class name
containing the main method.
Java is case sensitive. This program defines a class called ― HelloWorld
‖
.A class is an object oriented term. It is designed to perform a
specific task. A Java class is defined by its class name, an open curly brace,
a list of methods and fields, and a close curly brace. The name of the class is
made of alphabetical characters and digits without spaces, the first character
must be alphabetical.
The line ―public static void main (String [] args )‖ shows where the
program will start running.
The word main means that this is the main method –The JVM starts running
any program by executing this method first.
The main method in ―FirstProgram.java‖ consists of a single statement
System.out.println("This is my first program");
The statement outputs the character between quotes to the console.
Above explanation is about how to write program and now we have to learn
where to write program and how to
compile and run the program. For this reason, the next explanation is showing
the steps.
1. Edit the
program by the use of Notepad.
2. Save the
program to the hard disk.
3. Compile the
program with the javac command. (Java compiler)
4. If there are
syntax errors, go back to Notepad and edit the program.
5. Run the
program with the java command. (Java Interpreter)
6. If it does not
run correctly, go back to Notepad and edit the program.
7. When it shows
result then stop.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.