The Technical Details

ICW Lecture 3 Tom Chothia

Reminder of Last Time:

• Your programs defines “Classes”.

• Each class defines “Objects”.

• An Object is defined as having a number of “Fields” that store data...

• ...and a number of “Methods” that perform computation.

This Time:

• How does Java actually work. – Byte code, JVMs, , ...

• A short history of Java – What was added when – and what’s coming next.

• Pasrsing – How to turn test into a data structure you can work with.

To Run a Java Program

• Write the source .java program.

• Compile the source using javac to get a .class file.

• Run the source using java.

To Run a Java Program

• Write the source .java program.

• Compile the source using javac to get a .class file.

• Run the source using java.

• but the .class file runs on any computer! How?

Multi-platform.

• Java does compile to machine code.

• Java compiles to Java Byte Code. – Type: “javap -c ” to see the byte code.

• Every computer must have its own (JVM) which runs the the byte code.

• Every different OS must have it’s own JVM Java Program Compile Java to Byte .java Code Using “javac”

Java Byte Code .class Run Byte Code On JVM using “java”

Windows Phone JVM JVM JVM Windows Linux Mobile Computer Computer Phone

Java Byte Code

• Java Byte Code is very close to assembler.

• But where is the system.print code?

APIs

• The best aspect of Java is all it's libraries, or “Application Programming Interfaces ()”

• For a list standard packages see: http://java.sun.com/javase/6/docs/api/

• Third party APIs exist for almost any functions. Classloaders

• A “classloader” will load the byte code for other classes, as it is needed.

• This lets your byte code stay small, but still have access to all the libraries.

• Classloaders will be discussed futher in lecture 18.

Classpath

• The “” tells the classloader where to look for the byte code. • If you want to use your own libraries you must add them to the classpath. – Linux/Unix: use setenv command.

– Windows: Control Panel>System utility. – : Eclipse>Preferences>Java>Build Path>Classapth variable

Jar Files

• If you want to bundle classes together, use a Java Archive (JAR) file.

• A JAR file is a zipped directory of java class files.

• To run a jar file type: java -jar filename

• To create a jar file use: jar cf jar-file input-files • Or in Eclipse: File>Export>Java>Jar Running a Java Program

• Write source. • Compile with javac which – checks the classpath packages – checks types – Generates byte code • Run with java – Java verifies the byte code – Loads the byte code into the JVM. • The JVM runs the program – JVM execute the byte code – And loads more classes as needed. What You Need to Know

• Javac to compile, java to run.

• Set Classpath to file other class files.

• Normal you don’t need to know about JVMs and byte code

What You Need to Know

• Javac to compile, java to run.

• Set Classpath to file other class files.

• Normal you don’t need to know about JVMs and byte code but you might get errors when the version changes.

The Version Changes???

• Yes, every few years there is a new version of Java.

• New JVMs can run old classes.

• Old JVMs cannot run all new classes. – So Java is platform independed, but not JVM independed.

Java Version History

• Pre-Java: 1991 Sun built “Oak” • a language for home devices.

• 1996: home devices going no-where. Web is huge, Oak is made into 1 (JDK 1) and released.

Java Version History

• 1997 JDK 1.1 released. Adds: – JDBC (database access) – RMI (Remote Method Innvocation)

• 1998 J2SE 1.2 (Java 2 Platform, Standard Edition) – CORBA for interaction with other languages. – (GUIs) becomes core. – JVM updated to JIT

Java Version History

• 2000 J2SE 1.3 – RMI now works with CORBA – Support for clients to look up remote services (JNDI). – Serious debugging added.

• 2002 JDK 1.4 – assertions – Reguler expressions (later in this lecture) – IP6 support – XML parser and XSLT – Java Crypto (JCE)

Java Version History

• 2004 J2SE 5.0 – Generics Types – Better – Static inport – No more stubs in RMI

• 2006 Java SE 6 – Support for old windows systems dropped – 64-bit support – Rhino scripting language – Better JDBC

Java Version Future

• Java 7 (codeman Dolphin) is likely to be released in 2010.

• Dynamic language features (code can be changed and passed at run time).

• Support better support for parallel processers.

• Super packages.

Regular Expressions and Parsing

Regular Expressions

• Regular expressions describe strings based on common characteristics.

• They can be used to search, edit, or manipulate text and data.

• Once you understand the basics you'll be able to process any text you want.

Pattern and Matcher Classes

• Use the java.until.regex.Pattern Object to define what you want to match: Pattern p = Pattern.compile(“gmail”);

• Link it to the String you want to scan: Matcher m = p.matcher(myString);

• m.match() is true is String contains “gmail”.

Wild Cards

[abc] = a,b or c [a-z] = lowercase XY X then Y letter X|Y X or Y (not both) [a-zA-Z] = any letter X? one or zero Xs \\n = new line X* zero or more Xs \\d = any digit X+ one of more Xs \\s =white space

What Matches With What? A: [abcd][bcde][cdef] 1 [email protected] B: [a-z]+@[.a-z]+ 2 bed C: [a-zA-Z]@[.a-z].[a-z]+ 3 PUT@911 D: GET|PUT[\\s]+[//.:a-z@]* 4 GET http://www.x.com E: [A-Z@\\d]+[\\d] 5 [email protected]

Capture Groups

● Capture Groups let you process data.

● Put brackets round the bit you want to use.

● And use matcher.group(n) to match the n-th group

Finding email addresses.

Pattern p = Pattern.compile (“([a-zA-Z\d])@([a-zA-Z\d]).com”)

Matcher matcher = pattern.matcher(terms[0].trim());

While (m.match()) { System.out.print(“user: ”+m.group(1)); System.out.println(“ host: ”+m.group(2));

} Full Parsers

• You could parse the syntax of a language using , but it would be a lot of work.

• For big grammars you should use a ``parser generator'' such as antlr or yacc.

• You specific the grammar and the tool builds the parser for you.

Conclusion

• Java byte code can run the any computer with a JVM.

• Set you classpath to point to the classes/JAR files you want to use.

• Java is updating all the time.

• Parsers and Regular Expressions.

Next Time: The Java Crypto API • Generate and handle keys.

• How to encrypt and decrypt – public key encryption, – and symmetric key encryption.

• Hashes

• Keystores