[go: up one dir, main page]

0% found this document useful (0 votes)
13 views42 pages

CH 1

Uploaded by

Tech With Sonu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views42 pages

CH 1

Uploaded by

Tech With Sonu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

CHAPTER 1

INTRODUCTION
OBJECT ORIENTED PARADIGM
• OOP is the core of JAVA. Java is pure OOP.
• Major objective of OOP is to eliminate flow of data in procedural
approach.
• In OOP, data is a critical element & it doesn’t flow freely around
the system.
• OOP allows us to decompose problem into number of entities
called objects and then build data & move around these objects.
• Object is combination of data and methods.
• Objects are real time entities in an object oriented system.
• Entire set of data & code of an object can be made a user
defined data type using concept of class.
• A class is a user defined data type & object is a variable.
• Once class is defined we can create any number of objects
belonging to class.
• Data of an object can be accessed only by methods associated
with the object.
FEATURES OF OOP
1. Data Abstraction and Encapsulation
2. Inheritance
3. Polymorphism
4. Dynamic Binding
5. Message Communication
Data Abstraction and Encapsulation

• Wrapping up of data & method into single unit is known as


encapsulation.
• Data is accessible only to the methods which are there in
the class.
• These methods provide interface between data & program.
• This insulation of data from direct access by the program is
called “data hiding”
• Encapsulation treats objects as black box.
• Abstraction refers to representing essential features
without including back ground details or explanation.
• Classes use concept of abstraction.
Inheritance
• Inheritance is the process by which object of one class
acquire properties of objects of another class.
• Inheritance supports concept of hierarchical
classification.
• In OOP, the concept of inheritance provides the idea of
reusability.
• This means that we can add additional features to an
existing class without modifying it.
• This is possible by deriving a new class from the
existing one.
• The new class will have the combined features of both
the classes.
• Without the use of hierarchies, each object would
need to define its all characteristics explicitly.
Polymorphism
• It is an ability to take more than one form.
• For example, an operation may exhibit different behavior in
different instances.
• The behavior depends upon the types of data used in the
operation.
• For example, consider the operation of addition. For two
numbers, the operation will generate a sum.
• If the operands are strings, then the operation would produce
a third string by concatenation.
• Polymorphism plays an important role in allowing 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 actions associated
with each operation may differ.
• Polymorphism is extensively used in implementing
inheritance.
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 associated
with a given procedure call is not known until the
time of the call at runtime.
• It is associated with polymorphism and
inheritance.
• A procedure call associated with a polymorphic
reference depends on the dynamic type of that
reference.
Message Communication
• An object-oriented program consists of a set of objects that
communicate with each other. The process of programming
in an object-oriented language, therefore, involves the
following basic steps:
1. Creating classes that define objects and their behavior.
2. Creating objects from class definitions.
3. Establishing communication among objects.
• Objects communicate with one another by sending and
receiving information much the same way as people pass
message to one another.
• A message for an object is a request for execution of a
procedure that will invoke a procedure in the receiving
object and generates the desired output.
• Message passing involves specifying the name of the
object, the name of the method and the information to be
sent.
• Employee.salary(name);
BENEFITS OF OOP
• Through inheritance, we can eliminate redundant code and extend the use
of existing classes.
• We can build programs from the standard working modules that
communicate with one another, rather than having to start writing the
code from beginning. This leads to saving of development time and higher
productivity.
• The principle of data hiding helps the programmer to build secure
programs that cannot be invoked by code in other parts of the program.
• It is possible to have multiple objects to coexist without any interference.
• It is possible to map objects in the real life problem domain to those
objects in the program.
• It is easy to partition the work in a project based on objects.
• The data-centered design approach enables us to capture more details of a
model in an implementable form.
• Object-oriented systems can be easily upgraded from small to large
systems.
• Message passing techniques for communication between objects make the
interface descriptions with external systems much simpler.
• Software complexity can be easily managed.
APPLICATIONS OF OOP
• The promising areas for application of OOP
includes:
1. Real-time systems
2. Simulation and modeling
3. Object-oriented database
4. Hypertext, hypermedia
5. Artificial Intelligence and expert systems
6. Neural networks and parallel programming
7. Decision support and office automation
systems
8. CIM/ CAD/ CAD system
INTRODUCTION TO JAVA
• Java is a general-purpose programming language that is class-
based, object-oriented, and designed to have as few
implementation dependencies as possible.
• The syntax of Java is similar to C and C++, but it has fewer low-
level facilities than either of them.
• As of 2019, Java was one of the most popular programming
languages in use according to GitHub, particularly for client-
server web applications, with a reported 9 million developers.
• Java was originally developed by James Gosling at Sun
Microsystems (which has since been acquired by Oracle) and
released in 1995 as a core component of Sun Microsystems' Java
platform.
• The latest versions are Java 13, released in September 2019, and
Java 11, a currently supported long-term support (LTS) version,
released on September 25, 2018.
• The most important feature of JAVA is that it’s platform-
independent or platform natural language.
APPLICATIONS OF JAVA
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
BYTECODE
• The key that allows Java to solve security and portability problems is that the
output of a Java compiler is not executable code but it is bytecode.
• Bytecode is a highly optimized set of instructions designed to be executed by
the Java run-time system, which is called the Java Virtual Machine (JVM).
• That is, in its standard form, the JVM is an interpreter for bytecode.
• As you know, C++ is compiled to executable code.
• Most modern languages are designed to be compiled, not interpreted because
of performance concerns. Java program is executed by the JVM helps to solve
the major problems associated with downloading programs over the Internet.
• Translating a Java program into bytecode helps makes it much easier to run a
program in a wide variety of environments because 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.
• Although the details of the JVM will differ from platform to platform, all
interpret the same Java bytecode.
• If a Java program were compiled to native code, then different versions of the
same program would have to exist for each type of CPU connected to the
Internet.
• This is, of course, not a feasible solution.
• Thus, the interpretation of bytecode is the easiest way to create truly portable
programs.
BYTECODE
• The fact that a Java program is interpreted also helps to make it secure.
• Execution of every Java program is under the control of the JVM, the JVM can
contain the program and prevent it from generating side effects.
• When a program is interpreted, it generally runs substantially slower than it would
run if compiled to executable code.
• The use of bytecode enables the Java run-time system to execute programs much
faster than you might expect.
• Although Java was designed for interpretation, there is technically nothing about
Java that prevents on-the-fly compilation of bytecode into native code.
• Sun Microsystems supplied its Just In Time (JIT) compiler for bytecode, which is
included in the Java 2 release.
• When the JIT compiler is part of the JVM, it compiles bytecode into executable code
in real time, on a piece-by-piece, demand basis.
• It is not possible to compile an entire Java program into executable code all at once,
because Java performs various run-time checks that can be done only at run time.
• Instead, the JIT compiles code as it is needed, during execution.
• Though dynamic compilation is applied to bytecode, the portability and safety
features still apply, because the run-time system (which performs the compilation)
still is in charge of the execution environment.
JAVA VIRTUAL MACHINE
• Java compiler produces an intermediate code known as
bytecode for a machine that does not exist.
• This machine is called the Java Virtual Machine and it exists
only inside the computer memory.
• It is simulated computer within the computer and does all
major functions of a real computer.
• The machine specific code (known as machine code) is
generated by the Java interpreter by acting as an
intermediary between the virtual machine and the real
machine.
• Remember that the interpreter is different for different
machines.
• The Java object framework (Java API) acts as the
intermediary between the user programs and the virtual
machine which in turn acts as the intermediary between
the operating system and the Java object framework.
FEATURES OF JAVA
• Compiled and interpreted
• Platform independent and portable
• Object oriented
• Robust and secure
• Distributed
• Familiar and simple
• Multithreaded and interactive
• High performance
• Dynamic and extinsible
Compiled and interpreted
• First, Java compiler translates source code into
bytecode instructions.
• Bytecode instructions are not machine level
instructions.
• Then, in the second stage, Java interpreter
generates machine code that can be directly
executed by the machine that is running the Java
program.
• Thus, Java is both compiled & interpreted
language.
Platform Independent and Portable
• Java is portable, because java programs can easily be
moved from our computer system to another
anywhere & anytime.
• We can download JAVA applet from remote computer
onto local system via internet & run it locally.
• Java ensure portability in two ways:
1. Java compiler generates bytecode instructions & that
can be implemented on any machine.
2. Size of primitive data types are machine independent.
For example, regardless of type of machine being
used, integers are always 32 bits long in java.
Object Oriented
• Java is true object oriented language.
• In java, data does not move freely anywhere in program,
data can be accessed only through the objects.
• Almost “Everything is an Object” paradigm.
• All program code and data reside within objects and
classes.
• The object model in Java is simple and easy to extend.
• Java comes with an extensive set of classes, arranged in
packages that can be used in our programs through
inheritance.
Robust & Secure
• It has strict compile time & run time checking for data
types.
• It is having automatic garbage collection.
• If some object is unused or it goes out of scope then
memory occupied by that object will be freed.
• It also has feature called exception handling.
• It finds out all possible errors & eliminates risk of
crashing the system.
• Security is the important factor, especially if the
language is used for programming on internet.
• It doesn’t have pointers so program cannot access
memory location without proper authorization.
Distributed
• Java is distributed language for creating application on networks.
• It can share both data & programs.
• It can open and access remote objects on internet as easily as they
can do in local system.
• So that multiple programmers at multiple remote location can
collaborate & work.
• Java is designed for the distributed environment of the Internet,
because it handles TCP/IP protocols.
• In fact, accessing a resource using a URL is not much different from
accessing a file.
• The original version of Java (Oak) included features for intra
address-space messaging.
• This allowed objects on two different computers to execute
procedures remotely.
• Java revived these interfaces in a package called Remote Method
Invocation (RMI).
• This feature brings an unparalleled level of abstraction to
client/server programming.
Simple and Familiar
• Java is small & simple language.
• Java doesn’t use pointers, preprocessor header
files, go to statements etc.
• It also doesn’t use operator overloading &
multiple inheritance.
• To make the language familiar it was modeled on
C and C++.
• Java code looks like C++ code.
• Its simplified version of C++.
Multithreaded and Interactive
• Multithreaded means handling multiple tasks simultaneously.
• We don’t need to wait for the application to finish one task
before beginning another.
Ex. We can listen to an audio clip while scrolling pg & at the
same time download the video clip from distance computer.
• Java has strong support for multi process synchronization that
helps in running interactive systems smoothly with out
deadlocks.
• Interactive based programming allows you to write program
that responds to the user actions and helps developers to just
implement the logic based on the user action instead to
manage the complete multi-tasking solution.
• Java allows you to develop program that can do multiple task
simultaneously.
High performance
• Most previous attempts at cross platform solutions have
done so at the expense of performance.
• Other interpreted systems, such as BASIC, Tcl, and PERL,
suffer from low performance deficits.
• Java was designed to perform well on very low-power
CPUs.
• While it is true that Java was engineered for interpretation,
the Java bytecode was carefully designed so that it would
be easy to translate directly into native machine code for
very high performance by using a just-in-time compiler.
• Java run-time systems that provide this feature lose none of
the benefits of the platform-independent code.
• Multithreading also enhances the overall execution speed
of Java programs.
Dynamic & Extensible
• 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
expedient manner.
• This is crucial to the robustness of the applet environment,
in which small fragments of bytecode may be dynamically
updated on a running system.
• It supports functions written in other language such as C &
C++.
• These functions are known as native methods.
• Using this facility, programmer can use the efficient
functions available in these languages.
Difference between JAVA & C
Java C
Java is an object oriented language. C is a procedure oriented language.
Java is both compiled as well as interpreted C is only compiled language.
language.

Java uses bottom – up approach. C uses top – down approach.


Java is not using the concept of pointers. C uses the pointers to directly access the
memory location.

Java provides the efficient memory C does not provide the efficient memory
management. management.

Java supports method overloading. C does not support method overloading.

Java does not support preprocessor C supports the preprocessor statement like
statement like #define. #define and #include.

Java supports the exception handling to C does not support exception handling to
handle the exception at run time. handle the errors.
Difference between JAVA & C++
Java C++
Java is a true and complete object oriented C++ is an extension of C with object
language oriented behavior. C++ is not a complete
object oriented language as that of Java.

Java supports multiple inheritance using C++ achieves multiple inheritance by


interface. permitting classes to inherit from multiple
classes.
Java does not provide global variables. Global variables can be declared in C++.

Java does not support pointers. C++ supports pointers.


In Java, destruction of objects is performed In C++, destruction of objects is performed
in finalize method. by destructor function.
Java doesn’t provide header files. C++ has header files.
No scope resolution operator :: in java It uses :: as scope resolution operator.

Java does not support operator C++ supports operator overloading.


overloading
JAVA ENVIRONMENT
• It includes large number of development tools,
hundreds of classes and methods.
• Development tools are part of system known as JDK
(Java Development kit).
• Classes & methods are part of JSL (Java standard
libraries), also known as API (Application Programming
interface).
• JDK: It is a collection of development tools used for
developing & running java programs that include:
1. Applet viewer (for viewing java applets)
2. Javac (Java compiler)
3. Java (Java interpreter)
4. Javap (Java disassemble)
5. Javah (for c header files)
6. Javadoc (for creating HRML documents)
7. Jdb (Java debugger)
JAVA ENVIRONMENT
• Applet Viewer:
– The Java Applet Viewer enable us to run java applets without using
java compatible browser.
• Javac:
– It is a java compiler which translates java source code to byte code
files.
• Java:
– It is a java interpreter which runs java applets & applications by
reading byte code files.
• Javap:
– It is a java disassemble converts byte code files into program
description.
• Javah:
– It produces header files for native methods.
• Javadoc:
– It creates HTML document from java source code files.
• Jdb – java debugger:
– It helps us to find errors in our programs.
JAVA STANDARD LIBRARY (JSL)
• It includes number of classes & methods grouped into
functional packages.
• It is having inbuilt packages like:
1. Language support package: Collection of classes & methods
required for implementing basic features of java.
2. Utilities package: Collection of classes to provide utility
function such as date & time function.
3. I/o package: Collection of classes required for communicating
with other computers via internet
4. Networking package: Collection of classes required for
communicating with other computers via internet.
5. AWT package: Abstract Window Toolkit package contain
classes that implements platform – independent GUI.
6. Applet package: set of classes that allow us to create java
applet.
Anatomy of Application and Applet
• Java can be used to create two types of programs:
applications and applets.
• An application is a program that runs on your computer,
under the operating system of that computer.
• An applet is an application designed to be transmitted
over the Internet and executed by a Java-compatible Web
browser.
• 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.
Anatomy of Application and Applet
Java Application Java Applet
 Large Program  Small Program
 Executed on stand alone computer system  Used to run a program on client Browser
 Need JDK, JRE, JVM installed on client  Applet is portable and can be executed
machine. by any JAVA supported browser.
 Application can access all the resources  Applet applications are executed in a
of the computer Restricted Environment
 Applications are created by writing public  Applets are created by extending the
static void main(String[] ) method. java.applet.Applet
 Application has a single start point which Has 5 methods which will be automatically
is main method invoked on occurrence of specific event
 public class MyClass  import java.awt.*;
{ import java.applet.*;
public static void main(String args[]) public class Myclass extends Applet
{ {
public void init() { } public void start() { }
} public void stop(){}
} public void destroy() {}
public void paint(Graphics g) {}
}
Anatomy and structure of Java Program
• Java program may contain more than one classes. But only one class defines
main () method.
• Classes contain data and methods that operate on data.
• Methods may contain data type declaration and executable statements.
Anatomy and structure of Java Program
1. Documentation Section:
• Comprises of set of comments lines, which contain name of program, author
name & other details.
• There is a third type of comment.
• /** . . . . */- documentation comment.
2. Package statement:
• 1st statement allowed in java file is package statement.
• It declares a package name & informs compiler that the classes in the program
belong to this package.
3. Import statement:
• Same as # include of C.
• import student.test; It instructs interpreter to load test class from student
package.
• Using this we can access the class, which is the part of other package.
4. Interface statements:
• Interface is basically a class.
• Interface contains methods. But the method are abstract method means
interface do not specify any code to implement these method.
• It is basically used to implement inheritance.
• So class has to implement an interface
Anatomy and structure of Java Program
5. Class definitions:
• Java program may have multiple classes depending
upon the complexity of the problem.
6. main() method class:
• Every java application program must have main()
method.
• The class which contain this method is called main
class.
• In main() method we create objects of various
classes.
• On reaching at the end of main(), the program
terminates & control passes back to the operating
system.
Structure of Java Program
/*this is how you write comments*/
package myPackage;

• import packageName;
public class ClassName
{
public static void main (String args[])
{
// Your logic Goes Here
}
}
First Java Program
class JavaDemo
{
public static void main(String args[])
{
System.out.println(“Hello World”);
}
}
First Java Program
• Class declaration:
 As Java is pure object – oriented, everything must be placed inside class.
 here “class” is the keyword that declares new class definition.
 “JavaDemo” is the identifier that specifies the name of the class.
 For most computer languages, the name of the file that holds the source
code to a program is arbitrary. But this is not the case with Java.
 For this example, the name of the source file should be JavaDemo.java.
 In Java, a source file is officially called a compilation unit.
 It is a text file that contains one or more class definitions.
 The Java compiler requires that a source file use the .java filename
extension.
 The name of that class should match the name of the file that holds the
program.
 You should also make sure that the capitalization of the filename matches
the class name.
 The reason for this is that Java is case-sensitive.
First Java Program
 The javac compiler creates a file called JavaDemo.class that contains
the bytecode version of the program.
 When Java source code is compiled, each individual class is put into
its own output file named after the class and using the .class
extension.
 This is why give your Java source files the same name as the class
they contain—the name of the source file will match the name of
the .class file.
 “java JavaDemo” is used to run the code.
 When you execute the Java interpreter, you are actually specifying
the name of the class that you want the interpreter to execute.
 It will automatically search for a file by that name that has the .class
extension. If it finds the file, it will execute the code contained in the
specified class.
• Opening brace :
 Every class definition starts with ‘{‘ and ends with ‘}’.
Signature of main()
• public:
– It is a keyword is an access modifier which represents visibility, it
means it is visible to all.
– Since main method is public in java, JVM can easily access and
execute it.
• static:
– The main method is executed by the JVM, so it doesn’t require to
create object to invoke the main method. So it saves memory.
– The keyword static allows main( ) to be called without having to
instantiate a particular instance of the class.
– This is necessary since main( ) is called by the Java interpreter
before any objects are made.
• void:
– It is a keyword and denotes that the main() method does not return
a value.
• main():
– It is the name of the method.
Signature of main()
• String args[]:
– It declares a parameter named args, which contains an array
of object of the type String class. This is used to store
command line arguments, which are passed by user at
runtime.
• System.out.println (“ Hello World”);
– As java is true object oriented language, every method must
be a part of an object.
– The println() and print() methods are member of ‘out’ object,
which is a static data member of System class.
– println() always appends newline character to the end of the
string.
– ‘out’ displays the output on the command line.
– That means any other output will appear on the next line.
– Every java statement must end with a ‘;’.
Signature of main()
• main() is the method called when a Java application
begins.
• Keep in mind that Java is case-sensitive.
• Thus, Main is different from main.
• It is important to understand that the Java compiler
will compile classes that do not contain a main( )
method.
• But the Java interpreter has no way to run these
classes.
• So, if you had typed Main instead of main, the compiler
would still compile your program.
• However, the Java interpreter would report an error
because it would be unable to find the main( ) method.

You might also like