java programming introduction & concepts. introduction to java developed at sun microsystems by...

22
Java Programming Introduction & Concepts

Upload: blaze-hutchinson

Post on 30-Dec-2015

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Java Programming

Introduction & Concepts

Page 2: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Introduction to Java

Developed at Sun Microsystems by James Gosling in 1991

Object OrientedFreeCompiled and Interpreted (Hybrid)Current version is 1.6

Page 3: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Types of Languages

2 different dimensions for classificationProgramming paradigmCompiled or interpreted (or hybrid)

Page 4: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Compiled Languages (Simplified)

int a := 100;while (a > 0) { a := a – 1;}

Source code(program)written in asource language.

CompilerPerforms checks onthe program like- are variable names defined?- are they the right type?- are functions used correctly?- etc etc.

Produces output- bytecode- binary executable

ld length,%addcc %r1,-1,%r1addcc %r1,%r2,%r4….

Compiler output.the source programtranslated (compiled)to a target language.

Page 5: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Compiled Languages (Simplified)

A compiler Translates (compiles) a program written in a source language to a program written in a target language.

Performs a number of checks (syntactically and semantically) on the code to assure that the translated code is ‘correct’.

Page 6: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

The Inside of a Compiler

Scanning

Parsing

Name resolution

Type checking

Intermediate code generation

Optimization

Code generation

Typical stages of a compiler

Page 7: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Scanning

A scanner reads the input (source program) and creates a stream of tokens.

int a := 100;while (a > 0) { a := a – 1;}

Scanner

[INT, int], [ID, a], [COEQ, :=], [INTLIT, 100], [WHILE, while], [LPAREN, (], ….

Token StreamToken type

Actualscannedtext

Page 8: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Parsing

A parser reads a token stream and produces a parse-tree (if the program is syntactically correct)

[INT, int], [ID, a], [COEQ, :=], [INTLIT, 100], [WHILE, while], [LPAREN, (], ….

Scanner:=

a

a

while

100:=>

0

a

a +

1

Parse-tree(simplified)

int

Page 9: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Syntax

The syntax of a language describes which programs are correct in the sense that the textual representation follows the rules of the language.

int a := 100;while (a > 0) { a := a – 1;}

int a := 100;while (a > 0) { a ( > a – 1;}

int a := 100;while (a > 0) { a := a + “Hello”;}

?

Page 10: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Name Resolution

Traverses the tree and determines if all names (variables, parameters, methods, classes etc.) have been declared.

Page 11: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Type Checking

Checks that the program is correct with respect to types (one of the possibly many semantic checks) int a := 100;while (a > 0) { a := a + “Hello”;}

int a := 100;while (a > 0) { a := a - 1;}

int a := 100;while (a > 0) { a := a - 3.14;}

double a := 100;while (a > 0) { a := a - 1;}

OK though 100 or 1 are integer and not double values - 100 is coerced into 100.0and 1 into 1.0.

Page 12: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Semantics

Where syntax describes what the ‘shape’ of a legal program is, semantics describes the meaning of the program.

Page 13: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Intermediate Code Generation

Sometimes a compiler generates intermediate codeEasier to optimize Improves portability

Page 14: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Optimization

Optimization rewrites code/intermediate code to make it run faster…. (not for this course to bother with!)

Page 15: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Code Generation

0: bipush 100

2: istore_1

3: iload_1

4: ifle 14

7: iload_1

8: iconst_1

9: isub

10: istore_1

11: goto 3

14: return

Code can be machine codebyte code

Code can be forRegister based

architecturesStack based

architectures

Page 16: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Interpreted Languages

Some languages are not compiled, but executed (interpreted) directly.Typically interpreted languages are slowerNo need to recompile when making

changes

Page 17: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Programming Paradigms

A paradigm relates to the fundamental style of a language.Object Oriented: classes and objectsFunctional: everything is a functionProcess oriented: communicating processes Imperative: ‘straight line code’ (often part of

some of the above)

Page 18: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Where does Java Fit?

Java is an Object Oriented Hybrid languageWe have classes and objectsSource is compiled to bytecodeBytecode is interpreted

Page 19: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

The Java Compiler (javac)

The java compiler is called javacTakes in source code filesProduces class files for each referenced

class.

Page 20: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Java Bytecode

A compiled java class resides in a class file (one for each class)

Bytecode is a type of machine code, but for a stack oriented architecture

All computation is done on a stack, there are no registers

Page 21: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

The Java Virtual Machine (JVM)

A class file can be interpreted by the java virtual machine (JVM) using the command: java

Must contain a method called main to run

Page 22: Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and

Correctness

What does it mean that something is correct? It never crashes? It always work? (what does works mean?)…..