Download presentation
Presentation is loading. Please wait.
Published byBlaze Hutchinson Modified over 9 years ago
1
Java Programming Introduction & Concepts
2
Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and Interpreted (Hybrid) Current version is 1.6
3
Types of Languages 2 different dimensions for classification Programming paradigm Compiled or interpreted (or hybrid)
4
Compiled Languages (Simplified) int a := 100; while (a > 0) { a := a – 1; } Source code (program) written in a source language. Compiler 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 ld length,% addcc %r1,-1,%r1 addcc %r1,%r2,%r4 …. Compiler output. the source program translated (compiled) to a target language.
5
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’.
6
The Inside of a Compiler Scanning Parsing Name resolution Type checking Intermediate code generation Optimization Code generation Typical stages of a compiler
7
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 Stream Token type Actual scanned text
8
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
9
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”; } ?
10
Name Resolution Traverses the tree and determines if all names (variables, parameters, methods, classes etc.) have been declared.
11
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.0 and 1 into 1.0.
12
Semantics Where syntax describes what the ‘shape’ of a legal program is, semantics describes the meaning of the program.
13
Intermediate Code Generation Sometimes a compiler generates intermediate code Easier to optimize Improves portability
14
Optimization Optimization rewrites code/intermediate code to make it run faster …. (not for this course to bother with!)
15
Code Generation 0:bipush 100 2:istore_1 3:iload_1 4:ifle14 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
16
Interpreted Languages Some languages are not compiled, but executed (interpreted) directly. Typically interpreted languages are slower No need to recompile when making changes
17
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)
18
Where does Java Fit? Java is an Object Oriented Hybrid language We have classes and objects Source is compiled to bytecode Bytecode is interpreted
19
The Java Compiler (javac) The java compiler is called javac Takes in source code files Produces class files for each referenced class.
20
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
21
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
22
Correctness What does it mean that something is correct? It never crashes? It always work? (what does works mean?) …..
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.