Algorithm Programming 1 using Java 89 210 BarIlan
Algorithm Programming 1 (using Java) 89 -210 Bar-Ilan University 2007 -2008 תשס"ח by Moshe Fresko
Course objectives o Learning Professional Programming n o o o by Algorithmic Examples Learning Java Object Oriented Programming Design Patterns Multithreaded Programming GUI Run-time Class Information
Course Requirements o Exercises/Projects, 25 % of the grade n n n o Programming Exercises One step-by-step Project Time schedules are strict, no late exercises are accepted. They will be handed on via the Department Submitec utility Cheating is punished by Discipline Assembly ( Very strictly ) Exam 75 %
What is Java ? o o (Almost) Fully OO Programming Language. Syntax: Similar to C++, Different in some Semantics and Internals. Architecture Neutral World-Wide-Web programming language
History o Early 1990 s, Oak, by Sun n o o 1994, Sun decided to adapt Oak to Web January 1995, renamed as Java, n o Building Web based applications May 1995, Sun’s first JDK (Java Development Kit) n o For embedded systems in Consumer Electronic Devices With Hot. Java capable of running Applets 1998, Sun released Java 2 SDK (J 2 SDK) n By time Bunch of Libraries are added
Object Oriented Reminders o o o Abstraction Encapsulation Inheritance Abstract classes Polymorphism n n n o By Inheritance By Overloading Interfacing Constructors and Finalizers
Design Patterns o o o Design Patterns help you learn from others’ successes, instead of your failures Separate things that change, from the things that doesn’t change Elegant and Cheap-to-Maintain Inheritance can be thought as a DP, and so Composition, etc. Iterator (Enumeration in Java 1. 0 and 1. 1)
Resources o Internet n n n http: //java. sun. com For Tutorials and Downloads http: //java. sun. com/j 2 se/1. 4. 2/docs/api/ API of Java 1. 4 http: //java. sun. com/j 2 se/1. 5. 0/docs/api/ API of Java 1. 5 http: //java. sun. com/docs/books/tutorial/reallybigindex. html Development Environment o o Net. Beans ide Symantec’s Cofe Borland’s JBuilder Books n n n Developing Java Software Russel Winder & Graham Roberts 2 nd Edition, 2000 John Wiley and Sons, Ltd. Java 2 Complete Gemma O’Sullivan 1998, Sybex Thinking in Java Bruce Eckel 3 rd Edition (free to download) Design Patterns Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides 1995, Addison Wesley Other Java Books …
JDK, J 2 SE o J 2 SE, J 2 EE and J 2 ME o JFC : Java Foundation Classes Java 2 D : 2 D Graphics Java. Beans : Java object component technology Servlets : Web Server Java. Help : For creating help systems etc. o o o javac : Compiler to Byte-Code java : Loads a byte-code into JVM and executes javap : Byte-code viewer javadoc : Creates HTML documentation for the code. jdb : Java debugger appletviewer : To view applets from within an HMTL page
Application Example o Programs n n Applications Applets o Programs are Case Sensitive o Example: Hello. World. App. java file public class Hello. World. App { public static void main(String[] args) { System. out. println("Hello World!"); } }
Application Example o javac Hello. World. App. java n o Compiles and creates Byte. Code in file “Hello. World. App. class” java Hello. World. App n Looks for the file “Hello. World. App. class” and for the class “Hello. World. App” in it, and starts to run the static “main” function.
Applet Example // Hello. World. java file import java. applet. *; import java. awt. *; public class Hello. World extends Applet { public void paint(Graphics g) { g. draw. String("Hello world!", 50, 25); } } // Hello. html file <HTML> <HEAD><TITLE>A Simple Program</TITLE></HEAD> <BODY>Here is the output of my program: <APPLET CODE="Hello. World. class" WIDTH=150 HEIGHT=25></APPLET> </BODY> </HTML>
JVM o o o Source Code Java Compiler (javac) -> Byte. Code Java Interpreter (java) - Java VM
JVM o o java -classpath …; …; … for providing paths of class libraries, and. jar files are a set of class files compressed into a single file Class Execution Engine JNI Adapter Interface Adapter OS Class
Java Bytecode o Like C++ Assembly, Java Byte. Code o We need to check Byte. Code n n n For Performance and Memory Usage Tuning Size and Execution Speed Debugging
Java Bytecode - Example class Employee { private String name ; private int id. Number ; public Employee(String str. Name, int num) { name = str. Name ; id. Number = num ; store. Data(str. Name, num) ; } public String employee. Name() { return name ; } public int employee. Number() { return id. Number ; } private void store. Data(String s, int num) { //. . . } } javac Employee. javap –c Employee creates Employee. class to print to string the byte-code in visual form
Java Bytecode - Example Compiled from "Employee. java" class Employee extends java. lang. Object{ public Employee(java. lang. String, int); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object. "<init>": ()V 4: aload_0 5: aload_1 6: putfield #2; //Field name: Ljava/lang/String; 9: aload_0 10: iload_2 11: putfield #3; //Field id. Number: I 14: aload_0 15: aload_1 16: iload_2 17: invokespecial #4; //Methodstore. Data: (Ljava/lang/String; I)V 20: return public java. lang. String employee. Name(); Code: 0: aload_0 1: getfield #2; //Field name: Ljava/lang/String; 4: areturn public int employee. Number(); Code: 0: aload_0 1: getfield #3; //Field id. Number: I 4: ireturn }
Java Bytecode - opcodes o opcodes n n n a. . . : opcode is manipulating type object ref. i… : opcode is manipulation type integer b… : byte, c. . . : char, d… : double, etc.
Java Bytecode - JVM o JVM n n Stack-based machine Each thread has a JVM stack which stores Frames A frame is created each time a method is invoked Each Frame has o o o Operand Stack An array of Local variables A reference to Runtime constant pool of the class of the current method
Java Bytecode – JVM diagram
Java Bytecode o Local variables n n n o Operand stack n n o 0 th : this (for constructors or instant methods) Then parameters Then local variables LIFO stack, to pop and push values Used to receive return values Example public string employee. Name() { return name ; } public java. lang. String employee. Name(); Code: 0: aload_0 1: getfield #2; //Field name: Ljava/lang/String; 4: areturn o o aload_0 : pushes this pointer into stack getfield #2 : this popped+2 added and get reference (for name) from runtime constant pool of the class. This is loaded to the stack areturn : returns the top value of the stack (name). It is popped from operand stack and pushed to the operand stack of calling method The real machine code is : 2 A B 4 00 02 B 0
Java Bytecode public Employee(String str. Name, int num) { name = str. Name ; id. Number = num ; store. Data(str. Name, num) ; } public Employee(java. lang. String, int); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object. "<init>": ()V 4: aload_0 5: aload_1 6: putfield #2; //Field name: Ljava/lang/String; 9: aload_0 10: iload_2 11: putfield #3; //Field id. Number: I 14: aload_0 15: aload_1 16: iload_2 17: invokespecial #4; //Method store. Data: (Ljava/lang/String; I)V 20: return
String and String. Buffer o o o In package java. lang String. Buffer is modifiable version of String class String has n n n n n length substring extraction Finding and matching String comparison Uppercase and lowercase conversion Leading and trailing whitespace elimination Conversion to/from char arrays Conversion from primitive types to String Appending strings Inserting strings
- Slides: 23