classification of programming languages based on how … · 2 compiler – compilers vs....

29
1 Classification of Programming Languages Based on How They Derive Machine Code

Upload: vulien

Post on 05-Jul-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

1

Classification ofProgramming Languages

Based on How TheyDerive Machine Code

Page 2: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

222

Compiler –

Compilers vs. Interpreters

computer program that transforms source code written in one computer language to equivalent code in anotherlanguage

A compiler must translate an entire program into machine code before

the program can be executed.

• compiled languages: C, C++, Eiffel, Fortran, Crystal, …• most commonly it is translation from a high-level to a machine lang.

Page 3: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

3

COMMERCIAL COMPILER

Page 4: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

4

http://www.slideshare.net/darokoblog/an-introduction-to-java-programming-language-forbeginnersjava-programming-tutorials

Page 5: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

5Compilers vs. Interpreters (cont.)

Interpreter – program that directly executes instructions written in a programming or scripting language without compilingthem into a machine language

An interpreter translates one program statement at a time, executing a statement as soon as it is translated (‘on the fly’).

• interpreted languages: JavaScript, PHP, Ruby, Python, …

Page 6: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

6Compilers vs. Interpreters (cont.)

Once compiled, code is immediately ready

to run.

Code needs to be compiled separately for different CPUs.

Can be optimized for a CPU, to run faster.

Compiled code cannot be optimized

‘on the fly’.Difficult to ‘decipher’what the source code was from executable.

Code cannot be rununless compiled.

The same source code can be ‘run’ on

all machines.

User must install proper interpreter.

Can test code right away, without having

to compile it.

Have to go through ‘on the fly’ translation

every time.Can observe

program execution ‘line by line’.

Source, not machine code, gets

disseminated.

Page 7: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

7Compilers vs. Interpreters (cont.)

Is Java a compiled or an interpreted

language ?!?!

Page 8: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

8

Classification ofProgramming Languages

Based on Programming/Coding Philosophy

Page 9: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

9Procedural vs. Object Oriented Programming

Procedure-OrientedProgramming

- style of programming in which individualoperations used in a program are groupedinto logical units called procedures / functions

• operations / procedures are executed in the order of their appearance in the main program

• main program can be viewed as a sequence ofprocedure calls

• procedures from one program often CANNOT be reused in another program

Page 10: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

10Procedural vs. Object Oriented Programming (cont.)

Example [ C program ]

Page 11: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

11Procedural vs. Object Oriented Programming (cont.)

Object-OrientedProgramming(OOP)

- style of programming in which both data and thefunctions that operate on that data are combined/encapsulated into a single program entity calledobject

• thinking in OOP manner involves envisioning programcomponents as objects (class instances) that are similarto concrete objects in the real world

Page 12: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

12

Object-Oriented Programming (OOP) Advantages Ease of comprehension. Ease of modification and maintenance. Ease of code re-use.

Procedural vs. Object Oriented Programming (cont.)

Object-Oriented Programming (OOP) Disadvantages Requires (initially) longer code/software design time. Could lead to larger and more complex programs. Could produce slower programs.

Page 13: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

13Procedural vs. Object Oriented Programming (cont.)

C, Fortran, Pascal, … Eiffel, Ruby, Python, …

Java is 99% OOP language.

Page 14: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

14

OOP Basics

Class - a blueprint / template for objects• class definition describes what attributes its objects will have and

what methods those objects will be able to do

You can use the same class as a template to make (instantiate) many objects.

Object Oriented Programming

Page 15: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

15Object Oriented Programming (cont.)

Object - a concrete instance of a classes• you can create objects from classes that you write and from classes

written by other programmers, including Java’s creators

Page 16: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

16Object Oriented Programming (cont.)

https://engamrtarek.wordpress.com/2013/12/29/java-the-three-oop-principles-encapsulation-inheritance-polymorphism/

Page 17: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

17Object Oriented Programming (cont.)

Encapsulation - mechanism that binds together code and itsrespective data - all methods and data relatedto an object are stored within the object/class

• serves as a ‘protective wrapper’ that shields thecode and data from outside interference and misuse

• key enablers of encapsulation: public and privateaccess modifiers

• ideally, object data should be accessible only throughobject methods

Page 18: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

18Object Oriented Programming (cont.)

Example [ encapsulation ]

Page 19: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

19Object Oriented Programming (cont.)

Inheritance - ability to create classes that share the attributes andmethods of existing classes, but with more specificfeatures• child/sub class inherits all the members (fields, methods and

nested classes) from its parent/super class

single inheritance multilevel inheritance hierarchical inheritance

• enables faster code development, and allows for better codeorganization and minimization of duplicate code

Page 20: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

20Object Oriented Programming (cont.)

Example [ inheritance ]

Page 21: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

21Object Oriented Programming (cont.)

Example [ inheritance ]

Page 22: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

22Object Oriented Programming (cont.)

- allows actions to act differently based on either which object is performing the action orthe object the action is being performed on• method is able to deal with different types of inputs

• polymorphism enables programmers to create codethat is easier to understand and reuse

Polymorphism

Page 23: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

23Object Oriented Programming (cont.)

Example [ polymorphism ]

Page 24: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

24

Page 25: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

25Java

Is Java a pureOO language?!

- for a language to be pure OO language,it must:• support Encapsulation• support Inheritance• support Polymorphism• all predefined types must be Objects• all user defined types must be Objects• operations performed on objects must be

through methods exposed at the objects

Java’s primitive data types are not objects:

int, char, float

Java static methods and variables can be accessed

directly from the class, without the need to create an instance.

Java is 99% OOP.

Page 26: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

262626Java (cont.)

Is Java compiled or interpreted language?!Java combines the use of a compiler and an interpreter.Java compiler translates Java source code into (intermediate-level)Java bytecode – ‘machine language’ for Java Virtual Machine (JVM) .Java VM (interpreter) reads Java bytecode and executes it on a specific machine.

MyProgram.java MyProgram.class

Win JVM

Mac JVM

Unix JVM

Page 27: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

27Java (cont.)

- bytecode (x.class) generated after compilationof a java source file can be executed on anyplatform

Java is PlatformIndependent(Cross-Platform)Language • bytecode generated on Windows is the same as the

bytecode generated on Mac• note, however, that JVMs are platform dependant !!!

Page 28: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

28Java (cont.)

Java and the Internet

http://java.meritcampus.com/core-java-topics/Creation-Of-Java-As-Platform-Independence-WORA

Page 29: Classification of Programming Languages Based on How … · 2 Compiler – Compilers vs. Interpreters. computer program that transforms source code written in one computer language

29Java (cont.)

Java History