Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

U1A OOP_Java Intro

The document provides an introduction to Object-Oriented Programming (OOP) and Java, detailing various programming paradigms including imperative and declarative approaches. It covers the history of Java, its features, and the architecture of the Java Virtual Machine (JVM), as well as the Java Development Kit (JDK) and its components. The document emphasizes Java's platform independence and its significance in modern programming.

Uploaded by

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

U1A OOP_Java Intro

The document provides an introduction to Object-Oriented Programming (OOP) and Java, detailing various programming paradigms including imperative and declarative approaches. It covers the history of Java, its features, and the architecture of the Java Virtual Machine (JVM), as well as the Java Development Kit (JDK) and its components. The document emphasizes Java's platform independence and its significance in modern programming.

Uploaded by

gadhiyadaksh26
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

OOP and Java – Introduction

PREPARED BY:
CHINTAN A GAJJAR
INFORMATION TECHNOLOGY DEPARTMENT
DR S & S S GHANDHY COLLEGE OF ENGINEERING & TECHNOLOGY, SURAT
Programming Paradigms
 Paradigm can also be termed as method to solve some problem or do some
task. Programming paradigm is an approach to solve problem using some
programming language or also we can say it is a method to solve a problem
using tools and techniques that are available to us following some approach.
Imperative programming paradigm Declarative programming paradigm
 Procedural oriented programming  Logic programming paradigms
paradigm
 Object oriented programming  Functional programming paradigms
paradigm
 Parallel processing approach  Database/Data driven
programming approach
PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
1. Imperative Programming Paradigm
 It is one of the oldest programming paradigm. It features close relation to machine architecture. It
is based on Von Neumann architecture. It works by changing the program state through
assignment statements. It performs step by step task by changing state. The main focus is on how
to achieve the goal. The paradigm consist of several statements and after execution of all the
result is stored.
 Advantages:
→Very simple to implement
→It contains loops, variables etc.
 Disadvantage:
→Complex problem cannot be solved
→Less efficient and less productive
→Parallel programming is not possible
 Imperative programming is divided into three broad categories:
1. Procedural Oriented, 2.Object Oriented 3. Parallel processing
PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
1.1 Procedural Oriented Programming
 This paradigm emphasizes on procedure in terms of under lying machine
model. There is no difference in between procedural and imperative
approach. It has the ability to reuse the code and it was boon at that time
when it was in use because of its reusability.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
1.2 Object Oriented Programming
 The program is written as a collection of classes and object which are meant
for communication. The smallest and basic entity is object and all kind of
computation is performed on the objects only.
 More emphasis is on data rather procedure. It can handle almost all kind of
real life problems which are today in scenario.
 Advantages:
→ Data security
→ Inheritance
→ Code reusability
→ Flexible and abstraction is also present

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
1.3 Parallel Processing
 Parallel processing is the processing of program instructions by dividing
them among multiple processors. A parallel processing system posses many
numbers of processor with the objective of running a program in less time
by dividing them.

 This approach seems to be like divide and conquer. Examples are NESL (one
of the oldest one) and C/C++ also supports because of some library
function.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
2. Declarative programming paradigm:
 It is divided as Logic, Functional, Database. In computer science
the declarative programming is a style of building programs that expresses
logic of computation without talking about its control flow.
 It often considers programs as theories of some logic. It may simplify writing
parallel programs. The focus is on what needs to be done rather how it
should be done basically emphasize on what code is actually doing. It just
declares the result we want rather how it has be produced. This is the only
difference between imperative (how to do) and declarative (what to do)
programming paradigms.
 This paradigm can be categorised in 1. Logic, 2. Functional, 3. Database.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
2.1 Logic Programming Paradigm
 It can be termed as abstract model of computation. It would solve logical
problems like puzzles, series etc. In logic programming we have a knowledge
base which we know before and along with the question and knowledge
base which is given to machine, it produces result.
 In normal programming languages, such concept of knowledge base is not
available but while using the concept of artificial intelligence, machine
learning we have some models like Perception model which is using the
same mechanism.
 In logical programming the main emphasize is on knowledge base and the
problem. The execution of the program is very much like proof of
mathematical statement, e.g., Prolog

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
2.2 Functional programming paradigm
 The functional programming paradigms has its roots in mathematics and it is
language independent. The key principle of this paradigms is the execution of
series of mathematical functions.
 The central model for the abstraction is the function which are meant for
some specific computation and not the data structure. Data are loosely
coupled to functions.
 The function hide their implementation. Function can be replaced with their
values without changing the meaning of the program. Some of the languages
like Perl, JavaScript mostly uses this paradigm.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
2.3 Database/Data driven programming approach
 This programming methodology is based on data and its movement. Program
statements are defined by data rather than hard-coding a series of steps. A
database program is the heart of a business information system and provides
file creation, data entry, update, query and reporting functions.
 There are several programming languages that are developed mostly for
database application. For example, SQL. It is applied to streams of structured
data, for filtering, transforming, aggregating (such as computing statistics), or
calling other programs. So it has its own wide application.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
POP vs OOP
Procedure Oriented Programming Object Oriented Programming
 In POP, emphasis on doing things (algorithm).  In OOP, emphasis on data rather than procedure.
 Large programs are divided into smaller  Programs are divided into parts called objects.
programs known as functions.
 Most of the functions share global data.  Data structures are designed such that they characterize
the objects. Data is hidden and cannot be accessed by
external functions.
 Data move openly around the system from  Functions that operate on data of an object are tied
function to function. together in the data structure.
 Functions transform data from one form to  Objects may communicate with each other through
another. functions. New data and functions can be easily added
whenever necessary.
 Employs top-down approach in program design.  Follows bottom-up approach in program design.
 Example: Pascal, C, Basic, COBOL, Fortran are  Example: C++, Java, ADA, Smalltalk are some of the OOP
procedural languages. languages.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
About Java

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
History of Java
 In 1990, Sun Microsystems Inc. (US) has conceived a project to develop software for
consumer electronic devices that could be controlled by a remote. This project was
called Stealth Project but later its name was changed to Green Project.
 In January of 1991, Bill Joy, James Gosling, Mike Sheradin, Patrick Naughton, and
several others met in Aspen, Colorado to discuss this project. Mike Sheradin was to
focus on business development; Patrick Naughton was to begin work on the graphics
system; and James Gosling was to identify the proper programming language for the
project. Gosling thought C and C++ could be used to develop the project. But the
problem he faced with them is that they were system dependent languages and hence
could not be used on various processors, which the electronic devices might use. So
he started developing a new language, which was completely system independent.
 This language was initially called Oak. Since this name was registered by some other
company, later it was changed to Java.
PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
History of Java
 Why the name Java? James Gosling and his team members were consuming a lot of
coffee while developing this language. They felt that they were able to develop a
better language because of the good quality coffee they consumed. So the coffee had
its own role in developing this language and good quality coffee was exported to the
entire world from a place called 'Java island'. The term ‘Java’ in USA is generally a
slang used for coffee. Hence they fixed the name of the place for the language as Java.
Thus, the symbol for Java language is coffee cup and saucer.
 By September of 1994, Naughton and Jonathan Payne started writing WebRunner-a
Java-based Web browser, which was later renamed as HotJava. By October 1994,
HotJava was stable and was demonstrated to Sun executives. HotJava was the first
browser, having the capabilities of executing applets, which are programs designed to
run dynamically on Internet. This time, Java's potential in the context of the World
Wide Web was recognized.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
History of Java
 Sun formally announced Java and HotJava at SunWorld conference in 1995. Soon after,
Netscape Inc. announced that it would incorporate Java support in its browser
Netscape Navigator.
 Later, Microsoft also announced that they would support Java in their Internet
Explorer Web browser, further solidifying Java's role in the World Wide Web. On
January 23rd 1996, JDK 1.0 version was released. Today more than 9 million developers
use Java and more than 4 billion electronics devices, including mobile phones and TVs,
run on Java. Thus, Java pervaded the world.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
History of Java
 James Gosling is generally credited as the inventor
of the Java programming language.
 He was the first designer of Java and implemented
its original compiler and virtual machine.
 He is also known as the Father of Java.
 He was the Chief Technical Officer of Sun
Microsystems.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Features of Java
 Simple
 Pure Object-oriented
 Distributed
 Platform-Independent and Portable
 Robust and Secure
 High Performance
 Multithreaded
 Dynamic and Extensible

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Java – Platform independent
 Java is a general-purpose, high-level programming language.
 Java is designed to be architecturally neutral so that it can run on multiple
platforms. The same runtime code can run on any platform that supports
Java. In other words it follows “write once, run anywhere, anytime, forever”
(WORA) approach.
 To achieve its cross-architecture capabilities, the Java compiler generates
architecturally neutral bytecode instructions. These instructions are designed
to be both easily interpreted on any machine and easily translated into native
machine code on-the-fly, as shown in Figure.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Java Virtual Machine (JVM)
 Java Virtual Machine (JVM) is the heart of the entire Java
program execution process. It is responsible for taking the
.class file and converting each byte code instruction into
the machine language instruction that can be executed by Java Source Code
the processor.
 Java Runtime Environment (JRE) includes JVM, class libraries,
and other supporting files.
Java Bytecode
 JRE = JVM + Core Java API libraries
 JDK = JRE + development tools like compilers
 Tools such as javac (compiler), java (interpreter), and others JVM
are provided in a bundle, popularly known as Java (Java Virtual Machine)
Development Kit (JDK). JDK comes in many versions and is
different for different platforms such as Windows and Linux.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
JDK & JRE
 JDK : Java Development Kit is a software development kit that includes all the
tools necessary to develop Java Applications. It includes the Java compiler,
which converts Java source code into bytecode that can be run on any
platform using a JVM, as well as other tolls such as the Java Debugger, Java
Documentation Generator, JavaFX SDK. The JDK is typically used by
developers who are building Java applications from scratch.
 JRE : Java Runtime Environment is required to run Java applications. It
includes the JVM, which interprets the bytecode generated by the Java
compiler and executes it on the underlying hardware, as well as the Java
class library, which provides a set of pre-written classes that can be used by
Java applications. The JRE is typically used by end-users who are running java
applications on their computer.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Working of JVM
Source Code
 A Java program is written in editor. public class HelloWorld {
public static void main(String args[ ]) {
System.out.println(“Hello World!”);
}

 The program is compiled and classfile generated. }

Compiler
 A class file is produced containing bytecodes.
Class File
Bytecodes
 The bytecodes are interpreted by the JVM.

Java Virtual Machine


 The JVM translates bytecodes into native machine
code.
Native Machine Code

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Java program Running on Different platforms
class HelloWorld {
public static void main(String args[ ]) {
System.out.println(“Hello World!”);
}
} Java Program

Compiler

Bytecode Bytecode Bytecode


Interpreter Interpreter Interpreter

Hello
Hello Hello
World!
World! World!

Windows Linux MacOS


PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Process of Building & Running Java Program
Text Editor

Java Source
javadoc HTML Files
Code (.java)

javac

Java Class
javah Header Files
File (.class)

java jdb

Output

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Java Development Kit (JDK) for knowledge
 javac - The Java Compiler, which translates java source-code to bytecode
files that interpreter can understand.
 java - The Java Interpreter, which runs applets and applications by
interpreting bytecode files.
 jdb- The Java debugger, which helps to find errors in the programs.
 javap – java disassembles class file and prints the Java bytecodes.
 javaprof - Java profiler monitors the application's performance and
memory usage.
 javadoc - documentation generator
 javah - creates C header files
 appletviewer -Tool to run the applets
PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Application Programming Interface (API) for knowledge
 The Java Standard Library (or API) is a collection of ready-made software
components that provide many useful capabilities.
 It is grouped into libraries of related classes and interfaces which are known as
packages. Most commonly used packages are:
1. Language Support Package: A collection of classes and methods required for
implementing basic features of Java.
2. Utilities Package: A collection of classes and methods to provide utility functions
such as date and time functions.
3. Input/Output Package: A collection of classes and methods required to
input/output manipulation.
4. Networking Package: A collection of classes for communicating with other
computers.
5. AWT Package: The Abstract Window Tool Kit package contains classes that
implements platform-independent graphical user interface.
6. Applet Package: This includes a set of classes that allow us to create Java applets.
PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
First Program in Java

F:\SEM4\OOPJ>javac first.java

F:\SEM4\OOPJ>java first.class
Error: could not find or load main…

F:\SEM4\OOPJ>java first
First Program in Java.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Execution of first program on command prompt
>javac first.java: (step-1)
This command compiles the java program
file. The file name must be same as class
name used in the program. Here, first is
the class name as well as the file name.
The output of this command is class file
Step-1 (bytecode).

>java first: (step-2)


This command interprets the bytecode and
generates output of the program. (execute by
JVM)

Step-2

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Java Program Structure

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Java Program Structure
1. Documentation Section
2. Package declaration
3. Import statements
4. Interface section
5. Class definition
6. Main method
7. Statements and expressions

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
1. Documentation Section
 The documentation section is an important section but optional for a Java
program. It includes basic information about a Java program. The
information includes the author's name, date of creation, version, program
name, company name, and description of the program.
 It improves the readability of the program. Whatever we write in the
documentation section, the Java compiler ignores the statements during the
execution of the program. For documentation, we use comments. Type of
comments in Java are as follows:
1. Single line comment
2. Multi-line comment
3. Documentation comment

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
2. Package Declaration
 The package declaration is optional. It is placed just after the documentation
section. In this section, we declare the package name in which the class is
placed. Note that there can be only one package statement in a Java
program. It must be defined before any class and interface declaration.
 We use the keyword package to declare the package name.
 For example:

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
3. Import statements
 The package contains the many predefined classes and interfaces. If we want
to use any class of a particular package, we need to import that class. The
import statement represents the class stored in the other package. We use
the import keyword to import the class.
 It is written before the class declaration and after the package statement. We
use the import statement in two ways, either import a specific class or
import all classes of a particular package.
 In a Java program, we can use multiple import statements.
 For example:

//it imports all the class of the java.util package


PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
4. Interface Section
 It is an optional section. We can create an interface in this section if
required. We use the interface keyword to create an interface.
 An interface is a slightly different from the class. It contains only constants
and method declarations.
 Another difference is that it cannot be instantiated. We can use interface in
classes by using the implements keyword.
 For example:

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
5. Class Definition
 In this section, we define the class. It is vital part of a Java program. Without
the class, we cannot create any Java program.
 A Java program may conation more than one class definition. We use
the class keyword to define the class. It contains information about user-
defined methods, variables, and constants.
 Every Java program has at least one class that contains the main() method.
For example:

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
6. Main method
 In this section, we define the main() method. It is essential for all Java
programs. Because the execution of all Java programs starts from the main()
method.
 In other words, it is an entry point of the class. It must be inside the class.
Inside the main method, we create objects and call the methods.
 The main method has a specific signature that must be followed.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
7. Statements & expressions
 The statements and expressions are used to perform various actions and
manipulate data in a Java program.
 They includes assignments, operators, conditions, loops and other control
statements and method calls.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Comments in Java
 Single line comment:
Example:
 Multi-line comment:

 Documentation comment:

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Create API Documentation using javadoc

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Output of javadoc Calc.java

Many html files are created. Open the index.html or Calc.html file to get the information about the classes.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Content of index.html

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
Content of index.html

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
public static void main(String args[]) means…
 is the starting point for JVM to start execution of a Java program. If
is not written in a Java program, JVM will not execute it.
 : A method can return some result. If we want the method to return the
result in integer, then we should write before the method name. If a
method is not meant to return any value, then write . means no
value. method does not return any value.
 : A method is executed only when it is called. To call a method an
object is required. How is it possible to create an object before calling
method? So, we should call the method without creating an object.
Such methods are called static methods and should be declared as .
 Static methods are the methods, which can be called and executed without
creating the objects. JVM calls main() using its class name as
at the time of running the program.
PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT
public static void main(String args[]) means…
 JVM is a program written by Java team and is the method
written by us. Since, method should be available to JVM, it should be
declared as . If we don’t declare as , then it doesn’t
make itself available to JVM and JVM cannot execute it.
 Here, is an array of type String. It can store a
group of strings. The values passed to main() method are called command-
line arguments. These arguments are stored into args[] array, so the name
args[] is generally used for it.
 It is the name of the Java main method. It is the identifier that the
JVM looks for as the starting point of the java program. It’s not a keyword.

PREPARED BY: C A GAJJAR, IT DEPT, DR S & S S GHANDHY COLLEGE OF ENGG & TECH, SURAT

You might also like