java programming introduction & concepts. introduction to java developed at sun microsystems by...
TRANSCRIPT
Java Programming
Introduction & Concepts
Introduction to Java
Developed at Sun Microsystems by James Gosling in 1991
Object OrientedFreeCompiled and Interpreted (Hybrid)Current version is 1.6
Types of Languages
2 different dimensions for classificationProgramming paradigmCompiled or interpreted (or hybrid)
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.
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 StreamToken type
Actualscannedtext
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
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”;}
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.
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 codeEasier to optimize Improves portability
Optimization
Optimization rewrites code/intermediate code to make it run faster…. (not for this course to bother with!)
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
Interpreted Languages
Some languages are not compiled, but executed (interpreted) directly.Typically interpreted languages are slowerNo need to recompile when making
changes
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)
Where does Java Fit?
Java is an Object Oriented Hybrid languageWe have classes and objectsSource is compiled to bytecodeBytecode is interpreted
The Java Compiler (javac)
The java compiler is called javacTakes in source code filesProduces 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?)…..