0% found this document useful (0 votes)
41 views

INTRODUCTION To Java

Java is a high-level, object-oriented programming language that is platform independent and allows code to run on any system with a Java virtual machine. It was developed by Sun Microsystems in the 1990s. Key characteristics of Java include being platform independent through the use of byte code, being object-oriented with support for inheritance, polymorphism and encapsulation, and being robust with features like exception handling. Java is commonly used to create applications, applets, and web applications.

Uploaded by

tutoring center
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

INTRODUCTION To Java

Java is a high-level, object-oriented programming language that is platform independent and allows code to run on any system with a Java virtual machine. It was developed by Sun Microsystems in the 1990s. Key characteristics of Java include being platform independent through the use of byte code, being object-oriented with support for inheritance, polymorphism and encapsulation, and being robust with features like exception handling. Java is commonly used to create applications, applets, and web applications.

Uploaded by

tutoring center
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

INTRODUCTION

Java is a high-level, 3rd generation computer programming


language that is concurrent, object-oriented, class based and
portable across platforms. It is developed by Sun Microsystems.
It is intended to let application developers "write once, run
anywhere" (WORA), meaning that code that runs on one
platform does not need to be recompiled to run on another.
The creator of Java is James Gosling, chief programmer of Sun
Microsystems. Java is most similar to C. The most special thing
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 is used to write computer applications that play games,
store data or do any of the thousands of other things computer
software can do. It is also used to implement the web sites and
web pages. Java is used to develop stand-alone and Internet –
based applications. The main region to use java is that it is more
secure. Java software works everywhere, from the smallest
devices, such as microwave ovens and remote controls to
supercomputers.
Characteristics of Java:
1) Platform Independent
Java is a platform for application development. A platform
is a loosely defined computer industry buzzword that
typically means some combination of hardware and
system software that will mostly run all the same
software.
Java byte code is exactly the same on every platform.. Java
programs that have been compiled into byte code still need an
interpreter to execute them on any given platform. The
interpreter reads the byte code and translates it into the native
language of the host machine on the fly. Since the byte code is
completely platform independent, only the interpreter and a
few native libraries need to be ported to get Java to run on a
new computer or operating system.
All these pieces, the javac compiler, the java interpreter, the
Java programming language, and more are collectively referred
to as Java.
2) Object oriented
In object-oriented programs data is represented by objects.
Objects have two sections, fields (instance variables) and
methods. Fields tell you what an object is. Methods tell you
what an object does. These fields and methods are closely tied
to the object's real world characteristics and behavior. When a
program is run messages are passed back and forth between
objects. When an object receives a message it responds
accordingly as defined by its methods.
3) Robust
Java implements a robust exception handling mechanism to
deal with both expected and unexpected errors. The worst that
an applet can do to a host system is bringing down the runtime
environment. It cannot bring down the entire system.
Most importantly Java applets can be executed in an
environment that prohibits them from introducing viruses,
deleting or modifying files, or otherwise destroying data and
crashing the host computer. A Java enabled web browser
checks the byte codes of an applet to verify that it doesn't do
anything nasty before it will run the applet.
4) Multithreaded
Java is inherently multi-threaded. A single Java program can
have many different threads executing independently and
continuously. Three Java applets on the same page can run
together with each getting equal time from the CPU with very
little extra effort on the part of the programmer.
High performance
Java byte codes can be compiled on the fly to code that rivals
C++ in speed using a "just in-time compiler." Several companies
are also working on native-machine-architecture compilers for
Java. These will produce executable code that does not require
a separate interpreter, and that is indistinguishable in speed
from C++.
6) Dynamic
Java programs carry with them substantial amounts of run-time
type information that is used to verify and resolve accesses to
objects at run-time. This makes it possible to dynamically link
code in a safe and expedite manner. The java programming
language is unusual than other programming languages it first
compiles and then interprets the program. Compile first
translate the program into intermediate language called
intermediate language called java byte code. Java byte code is
platform independent code, which is further interpreted by the
interpreter on the java platform. Interpreter parses and run
each java byte code instruction on the computer. Compilation
occurs only once, interpretation occurs each time when the
program is executed.

Java byte code helps in making the program “write once, run
anywhere”. The program can be compiled into byte code by
any platform that is having the java compiler; the compiled java
byte code program is ready to run on any machine having the
java virtual machine (JVM). JVM is an interpreter for byte
code.

NEED OF JAVA

The main motive for developing java was to meet the need of
portable and platform independent language that could be
used to produce code that would run on a variety of systems.
Java and C
 Java does not include keywords like goto, size of and type
def.  Java does not contain data types like structure, union and
enum.  Java does not define the type modifiers keywords like
auto, register, signed.  Java does not support an explicit
pointer type.  Java does not have preprocessor.  Java does
not support variables arguments to functions.  Java adds many
features required for Object-Oriented Programming.

Java and C++


 Java does not support operator overloading.  Java does not
support multiple inheritances.  Java does not support global
variables.  Java has replaced the destructor function with a
finalize () function.  Java is a true object-oriented language
while C++ is basically C with object oriented extension.  Java
does not provide the access to the pointers.
Object Oriented Principles
The object oriented programming languages provide
mechanisms that help you implement the object-oriented
model.
1) Data Hiding and Encapsulation
One of the important object-oriented techniques is hiding the
data within the class and making it available only through the
methods. This technique is known as encapsulation because it
seals the data (and internal methods) safely inside the
"capsule" of the class, where it can be accessed only by trusted
users (i.e., by the methods of the class). The most important
reason is to hide the internal implementation details of your
class. If you prevent programmers from relying on those details,
you can safely modify the implementation without worrying
that you will break existing code that uses the class. Another
reason for encapsulation is to protect your class against
accidental or willful stupidity. A class often contains a number
of interdependent fields that must be in a consistent state. If
you allow a programmer (including yourself) to manipulate
those fields directly, he may change one field without changing
important related fields, thus leaving the class in an
inconsistent state. If, instead, he has to call a method to change
the field, that method can be sure to do everything necessary
to keep the state consistent. Similarly, if a class defines certain
methods for internal use only, hiding these methods prevents
users of the class from calling them. When all the data for a
class is hidden, the methods define the only possible operations
that can be performed on objects of that class. Once you have
carefully tested and debugged your methods, you can be
confident that the class will work as expected. On the other
hand, if all the fields of the class can be directly manipulated,
the number of possibilities you have to test becomes
unmanageable. There are other reasons to hide fields and
methods of a class, as well:
 Internal fields and methods that are visible outside the class
just clutter up the API. Keeping visible fields to a minimum
keeps your class tidy and therefore easier to use and
understand.  If a field or method is visible to the users of your
class, you have to document it. Save yourself time and effort by
hiding it instead.
2) Inheritance
Inheritance is the process by which one object acquires the
properties of another object. This is important because it
supports the concept of hierarchical classification. Most
knowledge is made by hierarchical classification. Inheritance is
a compile-time mechanism in Java that allows you to extend a
class (called the base class or super class) with another class
(called the derived class or subclass).
In Java, inheritance is used for two purposes:
Class inheritance - create a new class as an extension of
another class, primarily for the purpose of code reuse. That is,
the derived class inherits the methods of the base class. 
Interface inheritance - create a new class to implement the
methods defined as part of an interface for the purpose of sub
typing. That is a class that implements an interface “conforms”
to the interface.
In Java, unlike C++, these two types of inheritance are made
distinct by using different language syntax. For class
inheritance, Java uses the keyword extends and for interface
inheritance Java uses the keyword implements. In Java, unlike
C++, only single class inheritance is supported. I.e., for a given
class, there is only one super class.
3) Polymorphism
Polymorphism is the capability of an action or method to do
different things based on the object that it is acting upon. This
is the third basic principle of object-oriented programming.
Two types of polymorphism:
 Overloading and Overriding.  Dynamic method binding
Classes and Objects
A class defines user defined objects and their characteristics.
Any concept need to implement in a java program is
encapsulated within a class. A class defines the attributes and
methods of objects of the same type sharing common
characteristics. The main components of classes are:
 Data members (attributes)  Methods
Classes contain statements that include the declaration of data
members, which specify the type of data to be stored. Methods
of class contain a set of executable statements that gives a
desired output.

Creating Classes in Java


Code:
class ClassName
{
//Declaration of data members
//Declaration of methods
}
Creating Objects of Classes
An object is an instance of class and has a unique identity. The
identity of an object distinguishes it from other objects. While
object has a unique identity, a class is an abstraction of the
common properties of various objects.
Declaration: Declares a variable that holds the reference to the
object.
class name object name;
Instantiation or creation: creates an object of the specified
class. When you declare an object, memory is not allocated to
it. Therefore you cannot store data in the data members of the
object. New operator is used to allocate memory to an object.
object_name= new_class_name ();
Declaring Class Members
Java supports some basic programming elements, such as data
types, keywords, literals, variables and enums. Java is strictly
typed language, which means java gives importance to type
checking.

You might also like