220 likes | 236 Views
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
E N D
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;} Compiler Performs checks onthe program like- are variable names defined?- are they the right type?- are functions used correctly?- etc etc. Produces output- bytecode- binary executable Source code(program) written in a source language. ld length,%addcc %r1,-1,%r1addcc %r1,%r2,%r4…. Compiler output. the source program translated (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. Scanner int a := 100;while (a > 0) { a := a – 1;} [INT, int], [ID, a], [COEQ, :=], [INTLIT, 100], [WHILE, while], [LPAREN, (], …. Token Stream Token type Actual scanned text
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 + a 0 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”;} 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.0and 1 into 1.0. double a := 100;while (a > 0) { a := a - 1;}
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 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 code • byte code • Code can be for • Register based architectures • Stack based architectures
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?) • …..