Java Programming

Download Report

Transcript Java Programming

Java Programming
Introduction & Concepts
Introduction to Java

Developed at Sun Microsystems by
James Gosling in 1991
Object Oriented
 Free
 Compiled and Interpreted (Hybrid)
 Current version is 1.6

Types of Languages

2 different dimensions for classification
Programming paradigm
 Compiled or interpreted (or hybrid)

Compiled Languages (Simplified)
int a := 100;
while (a > 0) {
a := a – 1;
}
Source code
(program)
written in a
source language.
Compiler
ld length,%
addcc %r1,-1,%r1
addcc %r1,%r2,%r4
….
Compiler output.
the source program
translated (compiled)
to a target language.
Performs checks on
the program like
- are variable names
defined?
- are they the right type?
- are functions used
correctly?
- etc etc.
Produces output
- bytecode
- binary executable
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’.

The Inside of a Compiler
Scanning
Parsing
Name resolution
Type checking
Intermediate code generation
Optimization
Code generation
Typical stages of a compiler
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
type
Actual
scanned
text
Token Stream
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, (], ….
while
Scanner
int
:=
a 100
Parse-tree
(simplified)
>
a 0
:=
a
+
a 1
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”;
}


?
Name Resolution

Traverses the tree and determines if all
names (variables, parameters, methods,
classes etc.) have been declared.
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”;
}

double 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 - 3.14;
}

OK though 100 or 1 are integer and not
double values - 100 is coerced into 100.0
and 1 into 1.0.
Semantics

Where syntax describes what the ‘shape’
of a legal program is, semantics
describes the meaning of the program.
Intermediate Code Generation

Sometimes a compiler generates
intermediate code
Easier to optimize
 Improves portability

Optimization

Optimization rewrites code/intermediate
code to make it run faster

…. (not for this course to bother with!)
Code Generation

Code can be
machine code
 byte code


Code can be for
Register based
architectures
 Stack based
architectures

0:
2:
3:
4:
7:
8:
9:
10:
11:
14:
bipush 100
istore_1
iload_1
ifle
14
iload_1
iconst_1
isub
istore_1
goto 3
return
Interpreted Languages

Some languages are not compiled, but
executed (interpreted) directly.
Typically interpreted languages are slower
 No need to recompile when making
changes

Programming Paradigms

A paradigm relates to the fundamental
style of a language.
Object Oriented: classes and objects
 Functional: everything is a function
 Process oriented: communicating processes
 Imperative: ‘straight line code’ (often part of
some of the above)

Where does Java Fit?

Java is an Object Oriented Hybrid
language
We have classes and objects
 Source is compiled to bytecode
 Bytecode is interpreted

The Java Compiler (javac)

The java compiler is called javac
Takes in source code files
 Produces class files for each referenced
class.

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

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

Correctness

What does it mean that something is
correct?
It never crashes?
 It always work? (what does works mean?)
 …..
