The Java 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, Classpaths,... 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 <ClassName> to see the byte code. Every computer must have its own Java Virtual Machine (JVM) which runs the the byte code. Every different OS must have it s own JVM
Java Program.java Compile Java to Byte Code Using javac Java Byte Code.class Run Byte Code On JVM using java Windows JVM Windows Computer Linux JVM Linux Computer Phone JVM Mobile 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 (APIs) 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 classpath 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: 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 Java Development Kit 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. Swing (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 type conversion 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-z] = a,b or c = lowercase letter [a-za-z] = any letter \\n \\d \\s = new line = any digit =white space XY X then Y X Y X or Y (not both) X? one or zero Xs X* zero or more Xs X+ one of more Xs
What Matches With What? A: [abcd][bcde][cdef] B: [a-z]+@[.a-z]+ C: [a-za-z]@[.a-z].[a-z]+ D: GET PUT[\\s]+[//.:a-z@]* E: [A-Z@\\d]+[\\d] 1 Me@gmail.com 2 bed 3 PUT@911 4 GET http://www.x.com 5 abc@cs.bham.ac.uk
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 pattern matching, 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