Download Introduction to Java, JVM, and Byte

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Algorithm Programming 1
(using Java)
89-210
Bar-Ilan University
2007-2008 ‫תשס"ח‬
by Moshe Fresko
Course objectives

Learning Professional Programming







by Algorithmic Examples
Learning Java
Object Oriented Programming
Design Patterns
Multithreaded Programming
GUI
Run-time Class Information
Course Requirements

Exercises/Projects, 25 % of the grade






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 ?




(Almost) Fully OO Programming Language.
Syntax: Similar to C++, Different in some
Semantics and Internals.
Architecture Neutral
World-Wide-Web programming language
History

Early 1990s, Oak, by Sun



1994, Sun decided to adapt Oak to Web
January 1995, renamed as Java,


Building Web based applications
May 1995, Sun’s first JDK (Java Development Kit)


For embedded systems in Consumer Electronic Devices
With HotJava capable of running Applets
1998, Sun released Java 2 SDK (J2SDK)

By time Bunch of Libraries are added
Object Oriented Reminders





Abstraction
Encapsulation
Inheritance
Abstract classes
Polymorphism




By Inheritance
By Overloading
Interfacing
Constructors and Finalizers
Design Patterns





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

Internet





http://java.sun.com
For Tutorials and Downloads
http://java.sun.com/j2se/1.4.2/docs/api/
API of Java 1.4
http://java.sun.com/j2se/1.5.0/docs/api/
API of Java 1.5
http://java.sun.com/docs/books/tutorial/reallybigindex.html
Development Environment




NetBeans ide
Symantec’s Cofe
Borland’s JBuilder
Books





Developing Java Software
Russel Winder & Graham Roberts
2nd Edition, 2000 John Wiley and Sons, Ltd.
Java 2 Complete
Gemma O’Sullivan
1998, Sybex
Thinking in Java
Bruce Eckel
3rd Edition (free to download)
Design Patterns
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
1995, Addison Wesley
Other Java Books …
JDK, J2SE

J2SE, J2EE and J2ME

JFC : Java Foundation Classes
Java2D : 2D Graphics
JavaBeans : Java object component technology
Servlets : Web Server
JavaHelp : For creating help systems
etc.











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

Programs


Applications
Applets

Programs are Case Sensitive

Example: HelloWorldApp.java file
public class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Application Example

javac HelloWorldApp.java


Compiles and creates ByteCode in file
“HelloWorldApp.class”
java HelloWorldApp

Looks for the file “HelloWorldApp.class”
and for the class “HelloWorldApp” in it,
and starts to run the static “main” function.
Applet Example
// HelloWorld.java file
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("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="HelloWorld.class" WIDTH=150 HEIGHT=25></APPLET>
</BODY>
</HTML>
JVM



Source Code
Java Compiler (javac) -> ByteCode
Java Interpreter (java) - Java VM
JVM


java -classpath …;…;…
for providing paths of class libraries, and .jar files
.jar files are a set of class files compressed into a single file
Class
Class
Class
Execution Engine
JNI
Adapter Interface
Adapter
OS
Class
Java Bytecode

Like C++ Assembly, JavaByteCode

We need to check ByteCode



For Performance and Memory Usage Tuning
Size and Execution Speed
Debugging
Java Bytecode - Example
class Employee {
private String name ;
private int idNumber ;
public Employee(String strName, int num) {
name = strName ;
idNumber = num ;
storeData(strName,num) ;
}
public String employeeName() {
return name ;
}
public int employeeNumber() {
return idNumber ;
}
private void storeData(String s, int num) {
// ...
}
}
javac Employee.java
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 idNumber:I
14: aload_0
15: aload_1
16: iload_2
17: invokespecial
#4;
//MethodstoreData:(Ljava/lang/String;I)V
20: return
public java.lang.String employeeName();
Code:
0:
aload_0
1:
getfield
#2;
//Field name:Ljava/lang/String;
4:
areturn
public int employeeNumber();
Code:
0:
aload_0
1:
getfield
#3;
//Field idNumber:I
4:
ireturn
}
Java Bytecode - opcodes

opcodes





a... : opcode is manipulating type object ref.
i… : opcode is manipulation type integer
b… : byte,
c... : char,
d… : double, etc.
Java Bytecode - JVM

JVM




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



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

Local variables




Operand stack



0th : 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 employeeName() { return name ; }
public java.lang.String employeeName();
Code:
0: aload_0
1: getfield
#2; //Field name:Ljava/lang/String;
4: areturn




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 : 2A B4 00 02 B0
Java Bytecode
public Employee(String strName, int num) {
name = strName ;
idNumber = num ;
storeData(strName,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 idNumber:I
14: aload_0
15: aload_1
16: iload_2
17: invokespecial
#4; //Method storeData:(Ljava/lang/String;I)V
20: return
String and StringBuffer



In package java.lang
StringBuffer is modifiable version of String class
String has










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