Programming Languages 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, Compiled, or Interpreted Languages 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 Interpreted Languages: 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.
Interpreters Read input program interpret the operations Program input... c = a * a; b = c + b;. Source code Interpreter Abstract machine Run time Program output
Assembled, Compiled, or Interpreted Compiled languages: 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.
Compilers Read C/C++/Java program optimization translate into machine code Program input... c = a * a; b = c + b;. Source code Compiler.. 00000 01010 11110 01010.. Target code Program output Translation (compile) time Run time
Compilers Anatomy of a Computer Program written in a Programming Languages Compiler Assembly Language Translation
Anatomy of a Computer Lexical Analyzer (Scanner) Program (character stream) Token Stream Divides the string of input characters into single elements, tokens, based on strict computer punctuation
Lexical Analyzer (Scanner) 2 3 4 * ( 1 1 + - 2 2 ) Num(234) mul_op lpar_op Num(11) add_op Num(-22) rpar_op
Lexical Analyzer (Scanner) 2 3 4 * ( 1 1 + - 2 2 ) Num(234) mul_op lpar_op Num(11) add_op Num(-22) rpar_op 18..23 + val#ue Not a number Variable names cannot have # character
Anatomy of a Computer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Program (character stream) Token Stream Parse Tree Checks for errors in grammar rules
Syntax Analyzer (Parser) num * ( num + num ) <expr> <expr> <op> <expr> num * <expr> ( ) <expr> <op> <expr> num + num
Syntax Analyzer (Parser) int * foo(i, j, k)) int i; int j; { for(i=0; i j) { fi(i>j) return j; } Not a keyword Not an expression Extra parentheses Missing increment
Anatomy of a Computer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analyzer Program (character stream) Token Stream Parse Tree Intermediate Representation Determines the meaning of the string
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 Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analyzer Code Optimizer Program (character stream) Token Stream Parse Tree Intermediate Representation Optimized Intermediate Representation
Optimizer 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; } 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; }
Anatomy of a Computer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analyzer Code Optimizer Code Generator Program (character stream) Token Stream Parse Tree Intermediate Representation Optimized Intermediate Representation Assembly code
Code Generator 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; } 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
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 Each allows customization of current applications.
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. 4) Testing and debugging the program.
Introduction to Java technology
Introduction to Java technology Java technology is both: - a programming language and - a platform.
Introduction to Java technology The Java programming language is a high-level language that can be characterized by all of the following buzzwords: Simple Object oriented Distributed Multithreaded Dynamic Architecture neutral Portable High performance (JIT) Robust (memory) Secure (several layers of defense discuss )
Introduction to Java technology The Java programming language is a high-level language that can be characterized by all of the following buzzwords: 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. 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
Introduction to Java technology The Java programming language is a high-level language that can be characterized by all of the following buzzwords: 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 in behavior 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 executing
Introduction 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 your application 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 are sometimes also called lightweight processes or execution contexts.
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 (javac) JVM (java) Movie.java Movie.class Running program
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 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.
Introduction to Java technology 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
Introduction to Java technology The Java programming language is a high-level language that can be characterized by all of the following buzzwords: Architecture neutral: 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 Java interpreter 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:
Introduction to Java technology The Java platform : A platform is the hardware or software environment in which a program runs. Most platforms can be described as a combination of the operating system and 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 Client Web server Application server Data Presentation Business logic Servlets JavaServer Pages (JSPs) Enterprise JavaBeans (EJB) CORBA
Using 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 J2SE
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 networking applications and web-based applications.
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); } }