[go: up one dir, main page]

0% found this document useful (0 votes)
20 views30 pages

Chapter 1

The document introduces programming languages, categorizing them into low-level (machine and assembly languages) and high-level languages, highlighting their advantages and disadvantages. It explains the role of translators like compilers and interpreters in converting high-level code to machine code, and discusses various programming paradigms including procedural, logic-oriented, and object-oriented programming. Additionally, it focuses on Java as a purely object-oriented language, detailing its features, advantages, and the process of writing and executing Java programs.

Uploaded by

eyuelayalew185
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)
20 views30 pages

Chapter 1

The document introduces programming languages, categorizing them into low-level (machine and assembly languages) and high-level languages, highlighting their advantages and disadvantages. It explains the role of translators like compilers and interpreters in converting high-level code to machine code, and discusses various programming paradigms including procedural, logic-oriented, and object-oriented programming. Additionally, it focuses on Java as a purely object-oriented language, detailing its features, advantages, and the process of writing and executing Java programs.

Uploaded by

eyuelayalew185
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/ 30

Chapter 1

Introduction to Object Oriented Programming

1
Introduction to Programming language
 Literally, language essential to enable the society to communicate together as well to share
certain information.

 Programming language is a language that is designed to be used (read and written) by humans to
create programs that can be executed by computers.

 we can say that programming languages provides the way so that the users may interact with the
computer to give it commands and instructions to perform certain tasks

 There are two main types of computer programming languages.

 Low-level languages

 High-level languages

2
Introduction to Programming language
Low Level Programming Languages
 These languages are near to computer hardware and far from human languages. Computer can
understand these languages easily. Following are two low-level languages:
 Machine Language
 Assembly Language
Machine Language
➢ A computer language in which instructions are written in binary form (0 and 1) is called machine
language.
➢ It is the only language that is directly understood by the computer.
➢ Machine language is the native language of computer. Machine language is also known as first
generation language.
3
Introduction to Programming language
Advantages of Machine Languages
 Very fast program execution: Because the machine language is the native language of computer
that computers directly understand and execute. There is no need of a translator program, because
computer can already understand and execute the machine language instructions directly without
the need of translation.
Disadvantages of Machine Languages
 It is difficult to understand, It is difficult to learn
 Programs of Machine Language are difficult to modify
 Machine Language requires deep knowledge of hardware
 Programs of Machine Language are difficult to remove errors
 Programs of Machine Language are Machine dependent
4
Introduction to Programming language
Assembly Language
 It is a low-level language. In assembly language, symbols are used instead of binary code.
 These symbols are easy to remember. For example Add A,R1 instruction is used to add two
numbers. Assembly language is also known as second generation language.
Advantages of Assembly Language
 Assembly language programs are executed with fast speed
 Assembly language programming is easier to learn, understand and modify than machine
language
Disadvantages of Assembly Language
 Assembly language programs are machine dependent
 Assembly language programming requires deep knowledge of hardware
5
Introduction to Programming language
High Level Programming Languages

 A type of language that is close to human languages is called high level language. High-level
languages are easy to understand. Instructions of these languages are written in English-like
words e.g. Print, Display, Write etc.

 Examples of High Level Programming Languages, Cobol , C++ , C, Java, pascal,python

Advantages of High Level Programming Languages

 The are Easy to learn and understand, programs are easy to modify

 It is Easy to remove errors in the Programs written in High Level Programming Languages

 Programs written in High Level Programming Languages are Machine independent

 High Level Programming Languages have Better documentation


6
Introduction to Programming language
Disadvantages of High Level Programming Languages

 They are slower in program execution.

 IT takes additional translation times to translate the source to machine code.

 High level programs are comparatively slower than low level programs.

 Compared to low level programs, they are generally less memory efficient.

 Cannot communicate directly with the hardware.

Reading Assignment

 List and Describe Current High level Programming language which are widely used

7
Introduction to Programming language

 High level languages are grouped in two  We can also classify high level language
several other categories based on
categories based on execution model compiled
programming paradigm.
or interpreted languages.

8
Translators Compiler
 Computers only understand machine code (binary).
 It is a translator which is used to translate program
But on the other hand, programmers prefer to use a
written in a high-level language into machine code
variety of high and low-level programming
(object code). A compiler takes the whole program and
languages. To get rid of this issue, the high-level and
translates it into corresponding machine code which
low-level program code (source code) needs to pass
can be executed thereafter.
through a translator.
 The process of compilation may take some time but the
 A translator converts the source code into machine
translated program can be used again and again
code (object code). Different types of translators
without the need for recompilation.
are:
 An error report is often produced after the full
 Compiler
program has been translated. Errors in the program
 Interpreter code may cause a computer to crash.

 Assembler  These errors can only be fixed by changing the

9
original source code and compiling the program again.
Introduction to Programming language
Interpreter
 Interpreter is a translator which is able to read, translate and execute one statement at a time
from a high-level language program.

 The interpreter stops when a line of code is reached that contains an error. Interpreters are often
used during the development of a program.

 They make debugging easier as each line of code is analyzed and checked before execution.

 Interpreted programs will launch immediately, but our program may run slower than a complied
file.

 No executable file is produced.

 The program is interpreted again from scratch every time you launch it
10
Introduction to Programming language
Compiler
interpreter
 Scans the entire program and translates it as a
 Translates program one statement at a time.
whole into machine code.
 It takes less amount of time to analyze the
 It takes large amount of time to analyze the source
source code but the overall execution time is
slower. code but the overall execution time is

 No intermediate object code is generated, hence comparatively faster.

are memory efficient.  Generates intermediate object code which further

 Continues translating the program until the first requires linking, hence requires more memory.
error is met, in which case it stops. Hence  It generates the error message only after scanning
debugging is easy. the whole program. Hence debugging is
E.G python, ruby comparatively hard e.g. C,C++, java
11
Introduction to Programming language
Assembler : translate a program written in a low-level assembly language into a machine code (object code) file so it
can be used and executed by the computer. Assemblers are used Once assembled, the program file can be used again
and again without re-assembly .

Reading Assignment Classification of High level Programming Language based on application

programming paradigms

 A programming paradigm is a style, or “way,” of programming.

 A paradigm is a way of doing something (like programming), not a concrete thing (like a language).

 Categorization based on Design paradigm

 On the basis of design paradigms the high level programming languages can be categorized into the following types:

 Procedural.

 Logic-oriented languages.

 Object-oriented languages:
12
Procedural or function oriented programming
► In this language, a program is written as a sequence of procedures. Each procedure contains a series of
instruction for performing a specific task. Each procedure can be called by the other procedures during the
program execution.

► In this type of programming paradigms, a code once written in the form of a procedure can be used any number
of times in the program by only specifying the corresponding procedure name. Therefore the procedure-
oriented language allows the data to move freely around the system.

► The various examples of procedure-oriented language are FORTRAN, C, BASIC, and ADA.

13
Logic oriented programming
► These languages use logic programming paradigms as the design approach for solving various computational
problems.

► In this programming paradigm, predicate logic is used to describe the nature of a problem by defining the
relationship between rules and facts. Prolog is the best example of the logic-oriented programming language.

► It allows the programmer to specify a set of rules and facts about a problem domain, and then use
those rules and facts to automatically infer solutions to problems.

► Such type of programming is mainly used to develop logic based AI.

14
Object-oriented languages:
► These languages use object-oriented programming paradigms as the design approach for solving a given
problem.

► In this programming language, a problem is divided into a number of objects which can interact by passing
messages to each other. C++, JAVA etc are the examples of object-oriented programming language.

15
Advantages of Object Oriented Programming
► Simplicity: software objects model real world objects, so the complexity is reduced and the program structure
is very clear.

► Modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the
system.

► Modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program.
Changes inside a class do not affect any other part of a program, since the only public interface that the external
world has to a class is through the use of methods.

► Extensibility: adding new features or responding to changing operating environments can be solved by
introducing a few new objects and modifying some existing ones.

► Re-usability: objects can be reused in different programs.

16
Introduction to Java

C C++
Developed by Dennis Developed by Bjarne
Ritchie in 1970s.
Based upon structure Stroustrup in 1980s.
oriented. Based upon Object oriented principle.

Inherits Object oriented


Inherits Syntax Concept

Java
Developed by James
Gosling in 1995.
Purely object Oriented

17
Advantages of Object Oriented Programming
► Java was developed in the year 1995 at Sun Microsystems.

► Java was developed by James Gosling.

► Initially it was called Oak, in honor of the tree outside James Gosling's window; its name was changed to Java
because there was already a language called Oak.

► Java is based upon the concept “Write once, run anywhere”. The idea is that the same software should run on
different kinds of computers, consumer gadgets, and the other devices.

► Unlike most programming languages that generates executable code upon compilation; the JVM generates byte
codes as a result of compilation.

► Java byte codes are form of instructions understood by Java Virtual Machine and usually generated as a result
of compiling Java languages source code.

18
Continued …..

► Java Programs are basically of two types:

1. Applications - Applications are the java programs that are designed to run on the local system and do not
need any web browser to execute.

2. Applets – Applets are the java programs that are created specially to work over internet and are executed in a
java enabled Web browsers (i.e. Internet Explorer, Mozilla Firefox, Google Chrome and Netscape etc).

19
Continued …..
The Byte Code
► 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).

► Translating a Java program into bytecode makes it easier to run a program in a wide variety of environments
i.e. Windows, Unix, Solaris etc. The reason is straightforward: only the JVM needs to be implemented for each
platform.

Java Virtual Machine

► It has an interpreter component that enables communication between Java Byte Code and a computer’s
operating system. Using a JVM we can run a java code on any platform such as windows, unix etc.

► JVM normally reads and execute java statements once at a time.

20
What makes Java, most powerful language??
Simple

► Java was designed to be easy for the professional programmer to learn and use effectively.

► Because Java inherits the C/C++ syntax and many of the object-oriented features of C++, most programmers
have little trouble learning Java. Also, some of the more confusing concepts from C++ are either left out of
Java or implemented in a cleaner, more approachable manner.

► In Java, there are a small number of clearly defined ways to accomplish a given task.

Security

► When we use a Java-compatible Web browser, we can safely download Java applets without fear of

viral infection or malicious intent.

► Java achieves this protection by confining a Java program to the Java execution environment (sand box area)
and not allowing it access to other parts of the computer. This control is exercised by JVM.
21
What makes Java, most powerful language??
Portability

► Many types of computers and operating systems are in use throughout the world and many are connected to the Internet. For
programs to be dynamically downloaded to various types of platforms connected to the Internet, some portable executable code is
needed that can run on any environment

► i.e. the Byte Code.

Object-Oriented

► Like C++ , java uses all the OOP concepts like encapsulation, inheritance and polymorphism etc. Java is called a purely object
oriented language as everything we write inside class in a java program.

Robust: Comprehensive exception handling, strong type checking, automatic memory management, and compiler checks.

There is automatic garbage collection in Java which runs on the Java Virtual Machine to eliminate objects which are not being
accepted by a Java application anymore.

Multithreaded: java supports multithreaded programming, which allows us to write programs that do many things simultaneously.

22
Editing, Compiling a java aprogram
 Writing and running a java program involves the following phases.

 Phase 1: Writing a program


 Writing a java program involves editing a file with an editor. The following are some Java Integrated Development
Environments (IDEs).

 Jbuilder, NetBeans , Sun ONE Studio, Eclipse, jEdit, Jcreator, BlueJ, jGRASP, etc.

 Most of these editors can be downloaded for free.

 Any text editor can also be used for creating Java programs.

 Programs written on IDEs or any other editors are known as Source Code.

 Java source code file names end with .java extension.

 Program is created in an editor and stored on disk in a file ending with .java.

23
Cont’d …
 Phase 2: Compiling Java Programs into Bytecodes
 Since a computer cannot understand a source program, program called a compiler is used to translate the source
program into a machine language program called an object program.
 With Java, you write the program once, and compile the source program into a special type of object code, known as
bytecode.
 The bytecode can then run on any computer (platform) with a Java Virtual Machine.
 A virtual machine is a software application that simulates a computer-JVM is one of the most widely used virtual
machine.
 Java Virtual Machine, which is part of JRE, is software that interprets Java bytecode. Compiler creates bytecodes and
stores them on disk in a file ending with .class.
Java Bytecode

Java Virtual
Machine

Any
Computer

24
Cont’d …

 Phase 3: Loading a program

 A program must be placed in memory before it can execute: a process known as loading.

 The class loader takes the .class file (produced during compilation process) containing the program’s
bytecodes and transform them to primary memory.

 The class loader also loads any of the .class files provided by java that your program uses.

 The .class files can be loaded from a disk on your system or over a network.

 Phase 4: Bytecode Verification

 Involves examining bytecodes to ensure that they are valid and do not violate Java’s security restriction.

 Java enforces strong security, to make sure that Java programs arriving over the network do not damage your
files or your system (as computer viruses and worms might).

25
Cont’d …
 Phase 5: Execution
 The JVM executes the program’s bytecodes, thus performing the actions specified by the program.

 .class file is not final machine code yet. It is virtual machine code.

 JVM need a second compilation to convert virtual machine code to real machine code. We call it just-in-time
compiler (JIT). This greatly affects the speed of the execution.

 Traditionally, our C/C++ program can finish all the compilation before execution.

 The generated machine code could run directly on OS or hardware.

 Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly
storing data values as the program executes.

26
Cont’d …
A Simple Java Program
 In order to write Java programs, you need to have the Java SDK (Software Development
Kit) installed on your PC.
 This includes the API and the VM, as well as compilers and debuggers for Java.
 You can write a java program using note pad.
 Then you can compile and run your program using cmd.
 The following code is a java program that prints the string “Hello, World”.

public class HelloWorld


{
public static void main(String[] args)
{ // Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
27
Cont’d …
Steps:
 Save your program with the same as your class name and with .java extension.
 In the above case it will be named as HelloWorld.java.
 Before actually starting compiling and running java source code, environmental variable path must be
set.
 Set path to JDK bin directory: set path= C:\Program Files (x86)\Java\jdk1.6.0_29\bin
 Set class path to the current directory: set classpath= C:\Users\BW\Documents\NetBeansProjects
 To Compile:
 Write the command Javac Helloworld.Java as shown below

28
Cont’d …
 Compiling the source could will add a new file , HelloWorld.class to the folder
containing the source code.
 To Run:
 Write the command Java HelloWorld as shown below

At the end the out ‘Hello World’ will be displayed.

29
Do you have any question ????

30

You might also like