• In the beginning
… To use a computer, you needed to
know how to program it.
• Today
… People no longer need to know how to
program in order to use the computer.
• To see how this was accomplished, lets investigate how
programming languages evolved.
– First Generation
- Machine Language (code)
– Second Generation
- Assembly Language
– Third Generation
- People-Oriented Programming Languages
– Fourth Generation
- Non-Procedural Languages
– Fifth Generation
- Natural Languages
Assembled, Compiled, or Interpreted
Languages
• All programs must be translated before
their instructions can be executed.
• Computer languages can be
grouped
according to
which translation process is
used to convert the instructions into binary
code:
– Assemblers
– Interpreters
– Compilers
• Assembled languages:
– Assembler
: a program used to translate Assembly
language programs.
– Produces one line of binary code per original
program statement.
• The entire program is assembled before the program is
sent to the computer for execution.
Assembled, Compiled, or Interpreted
Languages
• Interpreted Languages:
– Interpreter
: A program used to translate high-level
programs.
– Translates one line of the program into binary code at a
time:
• An instruction is fetched from the original source code.
• The Interpreter checks the single instruction for errors. (If an error is found, translation and execution ceases. Otherwise…)
• The instruction is translated into binary code. • The binary coded instruction is executed.
• The fetch and execute process repeats for the entire program.
Assembled, Compiled, or Interpreted
Languages
Interpreters
………….... c = a * a; b = c + b; ………. Source code Program input Program output Interpreter Run time Abstract machine• Compiled languages:
– Compiler
: a program used to translate high-level
programs.
– Translates the entire program into binary code before
anything is sent to the CPU for execution.
• The translation process for a compiled program:
– First, the Compiler checks the entire program for syntax errors in the original
source code.
– Next, it translates all of the instructions into binary code.
» Two versions of the same program exist: the original source code version, and the binary code version (object code).
– Last, the CPU attempts execution only after the programmer requests that the program be executed.
Assembled, Compiled, or Interpreted
Languages
Compilers
……….. 00000 01010 11110 01010 ……….. ………….... c = a * a; b = c + b; ……….Source code Target code
Program input
Program output
Compiler
Translation (compile) time Run time
Anatomy of a Computer
Program written in a Programming Languages Assembly Language TranslationCompiler
Compilers
Anatomy of a Computer
Lexical Analyzer (Scanner)
Token Stream
Program (character stream)
Lexical Analyzer (Scanner)
Token Stream
Program (character stream)
Divides the string of input characters into single
Lexical Analyzer (Scanner)
Num(234) mul_op lpar_op Num(11) add_op
rpar_op
2 3 4 * ( 1 1 + - 2 2 )Lexical Analyzer (Scanner)
18..23 + val#ue
Num(234) mul_op lpar_op Num(11) add_op
rpar_op
2 3 4 * ( 1 1 + - 2 2 )
Num(-22)
Not a number
Anatomy of a Computer
Lexical Analyzer (Scanner)
Syntax Analyzer (Parser)
Token Stream
Parse Tree
Program (character stream)
Syntax Analyzer (Parser)
Token Stream
Parse Tree
Syntax Analyzer (Parser)
num
+
num
(
)
*
num
<expr> <expr> <expr> <expr> <expr> <expr> <op> <op>num ‘*’ ‘(‘ num ‘+’ num ‘)’
Syntax Analyzer (Parser)
int * foo(i, j, k))
int i;
int j;
{
for(i=0; i j) {
fi(i>j)
return j;
}
Extra parentheses
Missing increment
Not an expression
Not a keyword
Anatomy of a Computer
Intermediate Representation
Semantic Analyzer Lexical Analyzer (Scanner)
Syntax Analyzer (Parser)
Token Stream
Parse Tree
Program (character stream)
Intermediate Representation
Semantic Analyzer
Parse Tree
Semantic Analyzer
int * foo(i, j, k)
int i;
int j;
{
int x;
x = x + j + N;
return j;
}
Type not declared
Mismatched return type
Uninitialized variable used
Undeclared variable
Anatomy of a Computer
Code Optimizer
Optimized Intermediate Representation
Intermediate Representation
Semantic Analyzer Lexical Analyzer (Scanner)
Syntax Analyzer (Parser)
Token Stream
Parse Tree
Program (character stream)
Code Optimizer
Optimized Intermediate Representation
Intermediate Representation
Optimizer
int sumcalc(int a, int b, int N) { int i; int x, t, u, v; x = 0; u = ((a<<2)/b); v = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + v + t*t; v = v + u; } return x; }
int sumcalc(int a, int b, int N) { int i; int x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x+4*a/b*i+(i+1)*(i+1); x = x + b*y; } return x; }
Anatomy of a Computer
Code Optimizer
Code Generator
Optimized Intermediate Representation
Assembly code
Intermediate Representation
Semantic Analyzer Lexical Analyzer (Scanner)
Syntax Analyzer (Parser)
Token Stream
Parse Tree
Program (character stream)
Code Generator
Optimized Intermediate Representation
Assembly code
Code Generator
sumcalc:
xorl %r8d, %r8d xorl %ecx, %ecx movl %edx, %r9d cmpl %edx, %r8d jg .L7
sall $2, %edi .L5: movl %edi, %eax cltd
idivl %esi
leal 1(%rcx), %edx movl %eax, %r10d imull %ecx, %r10d movl %edx, %ecx imull %edx, %ecx leal (%r10,%rcx),
%eax
movl %edx, %ecx addl %eax, %r8d cmpl %r9d, %edx jle .L5
.L7: movl %r8d, %eax ret
int sumcalc(int a, int b, int N) { int i; int x, t, u, v; x = 0; u = ((a<<2)/b); v = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + v + t*t; v = v + u; } return x; }
Programming for Everyone
• Several ways to control what your computer does
or the way it accomplishes a particular task:
– Using Macros
– Using HTML to create Web Pages
– Scripting
Building a Program
• Whatever type of problem needs to be solved, a
careful thought out plan of attack, called an
algorithm, is needed before a computer solution can
be determined.
1) Developing the algorithm.
2) Writing the program.
3) Documenting the program.
Introduction to Java technology
Java technology is both:
- a
programming language
and
- a
platform
.
• Simple
• Object oriented
• Distributed
• Multithreaded
• Dynamic
• Architecture neutral
• Portable
• High performance (JIT)
• Robust (memory)
• Secure (
several layers of defense discuss …)
The
Java programming language
is a high-level language that can
be characterized by all of the following buzzwords:
Introduction to Java technology
Simple:
- The fundamental concepts of Java technology are grasped quickly; programmers can be productive from the very beginning.
- Java design team examined many aspects of the "modern" C and C++ languages to determine features that could be eliminated in the context of modern object-oriented programming.
The
Java programming language
is a high-level language that can
be characterized by all of the following buzzwords:
• No More Typedefs, Defines, or Preprocessor • No More Structures or Unions
• No Enums
• No More Functions
• No More Multiple Inheritance • No More Goto Statements
• No More Operator Overloading
• No More Automatic Coercions. You must do so explicitly by using a cast. • No More Pointers
Object oriented:
To be truly considered "object oriented", a programming language should support at a minimum four characteristics:
- Encapsulation
--implements information hiding and modularity (abstraction)- Polymorphism
--the same message sent to different objects results inbehavior that's dependent on the nature of the object receiving the message
- Inheritance
--you define new classes and behavior based on existing classes to obtain code re-use and code organization- Dynamic binding
--objects could come from anywhere, possibly across the network. You need to be able to send messages to objects without having to know their specific type at the time you write your code. Dynamic binding provides maximum flexibility while a program is executingIntroduction to Java technology
The
Java programming language
is a high-level language that can
be characterized by all of the following buzzwords:
Multithreaded:
Built-in support for
threads
provides Java programmers with a powerful tool to improve interactive performance of graphical applications. If yourapplication needs to run animations and play music while scrolling the page and downloading a text file from a server,
multithreading
is the way to obtain fast, lightweight concurrency within a single process space. Threads aresometimes also called lightweight processes or execution contexts.
Introduction to Java technology
The
Java programming language
is a high-level language that can
be characterized by all of the following buzzwords:
Interpreted and Dynamic:
Introduction to Java technology
The
Java programming language
is a high-level language that can
be characterized by all of the following buzzwords:
• Java source code is stored as text in a .java file.
• The .java file is compiled into .class files (with the same name). • A .class file contains Java bytecodes (instructions).
• The bytecodes are interpreted at run time.
– The Java .class file is the executable code.
Compile Movie.java JVM Running program Movie.class (javac) (java)
Interpreted and Dynamic:
Introduction to Java technology
The
Java programming language
is a high-level language that can
be characterized by all of the following buzzwords:
How Does JVM Work?
•The class loader loads (in JVM) all required classes.
–JVM uses a CLASSPATH setting to locate class files.
The classpath can be added in run time by using the java cp or -classpath option.
•JVM Verifier checks for illegal bytecodes.
–This validation ensures that there are no memory access violations or other illegal actions performed.
•JVM Verifier executes bytecodes.
–JVM may invoke a Just-In-Time (JIT) compiler.
•Memory Manager releases memory used by the dereferenced object back to the OS.
–JVM handles Garbage collection.
Benefits of Just-In-Time (JIT) Compilers
JIT compilers:
- Improve performance
- Are useful if the same bytecodes are executed repeatedly
- Translate bytecodes to native instruction
- Optimize repetitive code, such as loops
- Use Java HotSpot VM for better performance and
reliability
Architecture neutral:
Introduction to Java technology
The
Java programming language
is a high-level language that can
be characterized by all of the following buzzwords:
The solution that the Java system adopts to solve the binary-distribution problem is a "binary code format" that's independent of hardware
architectures, operating system interfaces, and window systems.
The format of this system-independent binary code is
architecture
neutral
. If the Java run-time platform is made available for a given hardware and software environment, an application written in Java can then execute in that environment without the need to perform any special porting work for that application.Introduction to Java technology
The
Java programming language
is a high-level language that can
be characterized by all of the following buzzwords:
Portable:
The primary benefit of the interpreted byte code approach is that compiled Java language programs are
portable
to any system on which the Javainterpreter and run-time system have been implemented.
Java specifies the sizes of all its primitive data types and the behavior of arithmetic on them. Here are the data types:
The
Java platform
:
Introduction to Java technology
A
platform
is the hardware or software environment in which a program runs. Most platforms can be described as a combination of the operating systemand underlying hardware.
The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other hardware-based platforms.
Using Java with Enterprise
Internet Computing
Web server Application server Presentation Business logic • Servlets • JavaServer Pages (JSPs) • Enterprise JavaBeans (EJB) • CORBA Client DataUsing the Java Virtual Machine
Operating system
JVM
Application
Running Java Applications
All Java applications run within a Java Virtual Machine (JVM).
JVM is invoked differently depending on whether the Java program is an application or an applet.
Java Software Development Kit
Sun Java J2SE (known as JDK and Java SDK)
provides:
– Compiler (javac)
– Core class library
• classes.zip • rt.jar
– Debugger (jdb)
– Bytecode interpreter: The JVM (java)
– Documentation generator (javadoc)
– Java Archive utility (jar)
– Others
Typical Java Development
Environment
•
Integrated development environments (IDEs)
•
Provide tools that support the software-development
process, including editors for writing and editing
programs and debuggers for locating
logic errors
—errors
that cause programs to execute incorrectly.
•
Popular IDEs
•
Eclipse (www.eclipse.org)
•
NetBeans (www.netbeans.org)
•
JBuilder (www.codegear.com)
•
JCreator (www.jcreator.com)
•
BlueJ (www.blueJ.org)
•
jGRASP (www.jgrasp.org)
Using the Appropriate Development Kit
Java2 comes in three sizes:
• J2ME
(Micro Edition): geared toward developing
applications for small, memory-constrained devices, such
as cell phones, pagers and PDAs.
• J2SE
(Standard Edition): Complete ground-up
development environment for the Internet
• J2EE
(Enterprise Edition): Everything in the J2SE plus,
geared toward developing large-scale, distributed
lolo.java
package ex4d; // collection of classes of similar functionality
import java.util.* ; // import for date class
Interface Monster {
void menace(); }
interface DangerousMonster extends Monster {
void destroy(); }
public class lolo {
static void w (Lethal l) { l.kill(); }
public static void main (String[] args) {
DangerousMonster barney = new DragonZilla(); w(vlad);
} }