Chapter-1 Introduction To JAVA
Chapter-1 Introduction To JAVA
The Java is a object oriented programming language developed by Sun Microsystems and released in public
alpha and beta versions in 1995. Java is used to create executable content.
The name Java refers to a set of software tools for creating and implementing executable content using the
Java programming language.
Java Developer’s Kit (JDK) is a software environment provided by Sun Microsystems, which contains
JRE(Java Runtime Environment)+Java language development tools.
JDK comes in various versions and can be downloaded free from the Sun Microsystem.
Java is compiled and interpreted language. Java program is saved to .java files . compiler translates the
program into an intermediate language called Java bytecodes .
This bytecodes are stored into .class files which is generated from .java file after compilation. Java interpreter
is also called Java Virtual Machine (JVM) is interpreted(converted) this bytecode into executable code.
Java is a platform independent language that means it can run on different OS. The Java platform consists of
JVM and Java API (Application programming Interface). The API provides pre-written libraries and standard
functionality with classes and interfaces.
The Java was developed by Sun Microsystems in 1991.It look 18 months to develop the first working version.
It was released in public alpha and beta versions in 1995.
This language was initially called “OAK” but it was renamed “JAVA” in 1995.
Oak was first used in television set-top boxes designed to provide video-on-demand services.
Oak was unsuccessful so in 1995 sun changed the name to Java and modified the language to take advantages
of the growing World Wide Web.
Actually their original goal was to create a computer language that could be used to build programs that would
run in any different execution environment.
o Classloader:
Classloader is a subsystem of JVM that is used to load class files.
o Class(Method) Area:
Class(Method) Area stores per-class structures such as field and method data, the code for methods.
o Heap:
JAVA PROGRAMMING
o Stack:
Java Stack stores frames.It holds local variables and partial results, and plays a part in method invocation and
return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation
completes.
o Execution Engine:
It contains:
1) A virtual processor
2) Interpreter : Read bytecode stream then execute the instructions
3) Just-In-Time(JIT) compiler:
The output of a Java compiler is not executable code. Rather, it is bytecode. Bytecode is a highly optimized set
of instructions .It is executed by Java Virtual Machine (JVM). JVM is an interpreter for bytecode.
Bytecode is a machine independent code.
By translating a Java program into bytecode it much easier to run a program in a wide variety of environments
only the JVM needs to be implemented for each platform. Once the run-time package exists for a given system,
any Java program can run on it.
The use of bytecode enables the Java run-time system to execute programs much faster.
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
5. Class
6. object
1. Abstraction
Abstraction represent essential features without including the background details or explanations.
Abstraction separates specification from implementation.
Ex: we know how to drive car i.e. clutch,break etc. we also know what happens when we press on break but we
don’t know how it happened?and how it is implemented?this is called abstraction.
Classes use the concept of abstraction and are defined as a list of abstract attributes such as size,weight,cost
and methods that operate on these attributes.
2. Encapsulation
The wrapping up of data and methods into a single unit is known as encapsulation.The data is not accessible to
the outside world and only those methods,which are wrapped in the class,can access it.encapsulation provides
data hiding features.
3. Inheritance
Inheritance is the process by which objects of one class get the properties of objects of another class.
It supports the concept of hierarchical classification.Main feature of inheritance is reusability that avoids
writing the same code twice.
4. Polymorphism:
Polymorphism means the ability to take more than one form.
It means when we call to a member function it will executed different function depending on the type of object
that call a function.
Overloading is a kind of polymorphism.
5. class:
A class define the properties and behavior that is shared by all its objects.
It is blue print for the creation of objects.
6. object:
object is the fundamental building blocks.Each object is instance of some class.
It takes variables and use methods defined in the class.
POP OOP
1.Program is divided into small parts called function 1.Program is divided into parts called objects.
2.Importance is given to function 2.Importance is given to data
3.It follows top down approach 3.It follows bottom up approach
JAVA PROGRAMMING
4.It does not have any access specifier. 4.It has access specifier named public,private,protected.
5.Data can move freely from function to function. 5.Object can move and communicate with each other
through member function.
6.To add new data and function in POP is not easy 6.To add new data and function in OOP is easy
7.POP does not have any proper way for hiding data so it 7.OOP provides data hiding so provide more security.
is less secure.
8.Overloading is not possible. 8.Overloading is possible
9.Ex: C,VB, FORTRAN 9.Ex: C++ , JAVA ,VB.NET
Java program may contain many classes in which one class define main method.class contains data type
declaration ,method,and executed statements.structure of java program is:
Document section
Package stat.
Import stat.
Interface stat.
class classname
{
main method
{
}
}
o Document section:
A set of comment lines like program name, author name, and othe details .
o Package stat.:
In this declaring package name and informs the compiler that the classes defined here belong to this package.
o Import stat.:
Number of import stat. will come. this is similar to include stat.this stat. import inbuilt or user define package
o Interface:
A interface like class but includes group of method declaration.
o Class definition:
A java program may contain multiple class definition. class are primary of java program.
o main method:
Every java program requires main method .main method creates objects of various classes and communicate
between them.
class hello
{
public static void main(String args[])
JAVA PROGRAMMING
{
System.out.println(“hello”);
}
}
o public:
The public keyword is an access specifier, which allows the programmer to control the visibility of class
members.
When we write public then that member may be accessed by code outside the class. In this main() must be
called by code outside of its class when program is started.
o static:
static allows main() to be called without having to create particular instance of the class.because main() is
called by JVM before any objects are created.
o void:
simply tells the compiler that main() does not return a value.
o main():
main() is method called when a java application begins.
o String args[]:
args[] is array of instances of the class String.Objects of type String store character string.
o System:
It is predefined class that provides access to the system.
o out:
It is the output stream that is connected to the console.
o println:
It is method that is display string which is passed to it.
Q-9 Explain types of Java Programs. Differentiate Java applications and Java Applets.
o Applications
Java application programs can run directly on our computer. Applications must have a main(). Java application
programs are compiled by javac command and run using java command.
o Applet
An applet is a Java program that runs over the Internet and executed by a Java-compatible Web browser.
JAVA PROGRAMMING
An applet is actually a tiny Java program, dynamically downloaded across the network, just like an image,
sound file, or video clip. The important difference is that an applet is an intelligent program, not just an
animation or media file.
In other words, an applet is a program that can react to user input and dynamically change—not just run the
same animation or sound over and over.
Applet does not have main().
Applets run in appletviewer.
Java Applications Java Applets
Java applications run on the local computers. Java applets run on the internet generally
Java applications can be run by java command Java applets run in appletviewer
Java applications must have main() Java applets do not have main() but it should have paint()
Java applications do not need HTML file to run Java applets need HTML file to run
Applet needs HTML file before it can be run.
1.Simple
Java is easy to learn and use for professional programmer. If you know C and C++ syntax and if you already
understand the basic concepts of object-oriented programming, learning Java will be easier.
2.Platform independent
Java programs can run on variety of platforms means on different operation systems with different
programming environments.
3.Secure
When you use a Java-compatible Web browser, you can safely download Java applets without fear of viral
infection . Java achieves this protection by not allowing access to other parts of the computer by downloaded
programs. There is ability to download applets with confidence that no harm will be done. This is considered
the most important aspect of Java.
4.Object-Oriented
Java is a object oriented language. The object model in Java is simple and easy to extend. Java supports
different object oriented concepts like abstraction, polymorphism, inheritance and encapsulation.
JAVA PROGRAMMING
5.Portable
Java applications can be transferred easily from machine to machine. It provides the feature write-once-run-
anywhere makes the java language portable but the system must have JVM. Operating system upgrades,
processor upgrades, and changes in core system resources can be the failure point for the program. But Java
applications are independent of this failure. The goal of Java designers was “write once; run anywhere, any
time, forever.” We can download Java applications from WWW and can run those applications on our local
computer.
6.Robust
Java program can run on variety of system. Because Java is a strictly typed language, it checks your code at
compile time and run time. There are mainly two reasons for program failure: memory management mistakes
and mishandled exceptional conditions (that is, run-time errors). Java manages memory allocation and
deallocation for you automatically. Java provides special Exceptional handling mechanism for different
exceptions like division by zero or file not found.
7.Multithreaded
Java was designed to meet the real-world requirement of creating interactive, networked programs. To
accomplish this, Java supports multithreaded programming, which allows you to write programs that do many
things simultaneously. The Java uses concept of synchronization to handle multiple programs.
8.Interpreted and high performance
Java compiler compiles the Java source code into Java bytecode. This code can be interpreted on any system
by JVM. The Java bytecode was carefully designed so that it would be easy to translate directly into native
machine code for very high performance.JIT is also used for high performance.
9.Distributed
Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols. The
original version of Java (Oak) allows objects on two different computers to execute procedures remotely. Java
provides this facility by having a package called Remote Method Invocation (RMI). This feature brings level
of abstraction to client/ server programming.
10.Dynamic
Java is dynamic language.Java is capable of dynamically linking in new class libraries,methods,and
objects.Java supports functions written in other languages. These functions are called native methods and these
methods are linked dynamically at run time with Java code.
C++ Java
1. C++ is not fully object oriented language. 1. java is fully object oriented language as its main()
is also within the class
2. C++ supports operator overloading. 2. Java does not support operator overloading
3. C++ has templates 3. Java does not have template classes
4. C++ supports multiple-inheritance. 4. Java does not support multiple-inheritance
5. pointers are the important part of C++ 5. Java does not use pointers.
JAVA PROGRAMMING
programs
6. C++ use global variables. 6. Java generally does not use global variable
7. C++ use destructor to destroy the objects 7. Java has finalize() method to destroy the objects
8. C++ use header files. 8. There are no header files in Java. Java has inbuilt
packages to use built-in functions
o Advantages:
Java is portable language. The goal of Java designers was “write once; run anywhere, any time, forever.” We
can download Java applications from WWW and can run those applications on our local computer.
Java is platform independent language. Java programs can run on variety of platforms means on different
operation systems with different programming environments.
Java is a secure language. When you use a Java-compatible Web browser, you can safely download Java
applets without fear of viral infection or malicious intent.
Java supports multithreading so user can run more than one thread activity at a time.
Java code is checked before loading so they are secured from run time errors.
Through inheritance Java eliminates the redundant code and provides reusability.
Java loads classes dynamically at the time they are actually needed.
Java and Internet
o Java users can run applet programs on Internet.
o Java users can download Java applications form Internet and run them on local computers.
Java and WWW
o World Wide Web is a distributed system that runs on the top of the Internet. It contains web pages that
provide information and control.
o Java is used in distributed system. Java communicate with the web page through the special tag
<APPLET>.
o Disadvantages:
Performance : - Java can be consider slower and more memory consuming than c or c++
Look :- The default look of GUI application written in java is very different. It is possible to specify different
look through the pluggable look.
------------------------------------------------------------------------------------------------------------------------------------------
filename: h1.java
class hello
{
public static void main(String args[])
JAVA PROGRAMMING
{
System.out.println(“hello”);
}
}
startruncmddos prompt
------------------------------------------------------------------------------------------------------------------------------------------
GTU QUESTION:
1. List four different OOP concepts.
2. Describe JVM.
3. Describe main() method in Java.
4. State the importance of byte code.
5. Write any four differences between Java and C++.
6. Explain basic structure of Java programs.
7. List different features of Java. Explain any one of it
8. Give difference between Applet and Application.
9. Explain following
i)Byte Code (ii) JVM (iii) Platform Independent.
10. Explain following
(i) Portable (ii) JRE (iii) Applet