# Presentation: 114620

## Emerging Java Technologies

- Jeremy Jones
- Peyush Jain
- John Jung
- January 29th, 2004

## Agenda 

- The Current State of Java
- Java 1.5
- Generics
- Autoboxing
- Enhanced for loop
- Typesafe Enums
- Static Import
- Varargs
- Metadata
- Swing Changes
- XML
- XUL and JDNC
- Eclipse

## The Current State of Java

- Java is still going strong
- Great success on server / web apps
- Millions of Java VMs in cell phones
- Client-side Java less successful though
- The “hype” phase is over
- But big challenges remain
- Sun’s uncertain future
- Microsoft’s C# and .Net
- Microsoft’s Longhorn OS
- Open-sourcing Java still an issue for some

## Java 1.5

- (aka JDK 1.5, J2SE 1.5, “Tiger”)
- Next major release of the Java platform
- Significantly greater changes than in 1.4 or 1.3
- Few language changes in 1.4/1.3
- First beta 1Q04, release end of 04?
- Continued focus on performance and stability
- Despite this presentation’s focus on new features
- Final set of features may change before release

## Generics

- When you get an element from a collection, you have to cast
- Casting is a pain
- Casting is unsafe - casts may fail at runtime
- Plus it’s not always obvious what type goes into or comes out of a collection
- With Generics, tell the compiler what type the collection holds
- Compiler will add the casts for you
- Provides compile-time type safety
- Similar to C++ templates, but not the same

## Example without Generics

**// Removes 4-letter words from c; elements must be strings**
**static void expurgate(Collection c) {**
**	****for (Iterator i = c.iterator(); i.hasNext();) {**
**	****	****if (((String) i.next()).length() == 4) {**
**	****	****	****i.remove();**
**	****	****}**
**	****}**
**}**
**// Alternative form - a bit prettier?**
**static void expurgate(Collection c) {**
**	****for (Iterator i = c.iterator(); i.hasNext();) {**
**	****	****String s = (String) i.next();**
**	****	****if (s.length() == 4) {**
**	****	****	****i.remove();**
**	****	****}**
**	****}**
**}**

## Example with Generics

**// Removes 4-letter words from c**
**static void expurgate(****Collection<String>**** c) {**
**	****for (****Iterator<String>**** i = c.iterator(); i.hasNext();) {**
**	****	****if (****i.next().length()**** == 4) {**
**	****	****	****i.remove();**
**	****	****}**
**	****}**
**}**

- No cast, extra parentheses, or temporary variables
- Provides compile-time type checking

## Generics != Templates

- Benefits of templates without all the baggage of the C++ version
- No code-size blowup
- No precompiler messiness
- Simply provides compile-time type safety and eliminates the need for casts
- BUT – Generics do not help performance
- Casts only hidden, not eliminated
- No direct support for primitive parameter types
- Uses autoboxing instead

## Autoboxing/unboxing

- Java has split type system
- Primitives (int, float, etc.) and Objects (String, etc.)
- Wrapper classes used to bridge the gap (Integer for int, Float for float, etc.)
- Can’t add primitives to collections
- Collections take Objects
- Must wrap primitives in Objects first
- Autoboxing does this wrapping/unwrapping for you
- Automatically wraps primitives in objects
- Automatically unwraps wrapper objects to primitives
- Simplifies code
- BUT – No performance advantages at all – only hides the wrapping
- Developer must still understand the costs involved
- Potentially lots of wrapper objects being created, which can be expensive

## Enhanced for Loop

- Iterating over collections is a pain
- Often, iterator unused except to get elements
- Iterators are error-prone
- Iterator variable occurs three times per loop
- Common cut-and-paste error
- Using the wrong iterator
- Many languages have a “foreach” concept
- Now Java does, but without the “foreach” keyword
- Sun was very worried about backwards compatibility

## Enhanced for Loop Example

**// Without enhanced for loop**
**for (Iterator i = suits.iterator(); i.hasNext(); ) **
**{**
**	****Suit suit = (Suit) i.next();**
**	****for (Iterator j = ranks.iterator(); j.hasNext(); )**
**	****{**
**	****	****sortedDeck.add(new Card(suit, j.next()));**
**	****}**
**}**

**// With enhanced for loop**
**for (****Suit suit : suits****)**
**{**
**	****for (****Rank rank : ranks****)**
**	****{**
**	****	****sortedDeck.add(new Card(suit, rank));**
**	****}**
**}**

## Typesafe Enums

- Standard approach – int constants
- Not typesafe, no namespace, values are meaningless, etc.
- Typesafe Enum design pattern helps
- Much better, but lots of code required
- 1.5 basically adds language support for the Typesafe Enum pattern
- Plus can be used in switch statements
- More powerful than C/C++ enums
- Enums are classes, so can have methods, etc.

## Typesafe Enums Example

**// Simple example**
**enum Season { winter, spring, summer, fall }**

**// Enum with field, method, and constructor**
**public enum Coin {**
**	****penny(1), nickel(5), dime(10), quarter(25);**
**	**
**	****private final int fValue;**
**	****Coin(int value) { fValue = value; }**
**	**
**	****public int value() { return fValue; }**
**}**

## Static Import

- Static members from a class must be qualified with the class name
- Math.cos(), Math.PI, etc.
- Developers don’t like this, particularly for constants
- Some define “Constant Interface” which classes implement to get around this problem
- Generally recognized as a bad idea (“antipattern”)
- Instead, import the static members from a class, rather than the classes from a package
- Can import static methods and fields
- Can import enums too!

## Varargs

- Sometimes you want a method that takes an arbitrary number of parameters
- Either use an array or overload the method signature
- Java has no printf as a result
- printf basically requires variable arguments
- C/C++ has it, but syntax is cumbersome
- printf will be included too!

## Varargs Syntax

- Parameter type of arguments is Object[]
- Caller uses normal syntax

**public static String format(String pattern,**
**	****	****	****	**** ****Object... arguments****)**

## Metadata

- Many APIs require “boilerplate” or “side” files associated with a specific class
- JAX-RPC web services
- EJBs
- JavaBean “BeanInfo” class
- Creating and maintaining these associated files is painful
- Instead, annotate the original class and let the tools generate the associated files for you
- Or do anything else – the use of this mechanism is undefined
- Simple example: @deprecated
- This feature adds a generic mechanism for annotating classes/methods/fields
- The meaning of the annotations is not defined
- Different APIs (JAX-RPC, EJB, etc) will define their own “standard” annotation types
- Annotation types are declared like interfaces
- Provides compile-time checking
- Annotation types can contain parameters which must be specified

## Metadata Example

**// JAX-RPC web service interface and implementation**
**public interface CoffeeOrderIF extends java.rmi.Remote {**
**	****public Coffee [] getPriceList()**
**	****	****throws java.rmi.RemoteException;**
**	****public String orderCoffee(String name, int quantity)**
**	****	****throws java.rmi.RemoteException;**
**}**
**public class CoffeeOrderImpl implements CoffeeOrderIF {**
**	****public Coffee [] getPriceList() {**
**	****	****...**
**	****}**
**	****public String orderCoffee(String name, int quantity) ****	****{**
**	****	****...**
**	****}**
**}**

## Metadata Example

**// JAX-RPC web service with metadata**
**public class CoffeeOrder {**
**	****@Remote**** public Coffee[] getPriceList() {**
**	****	****...**
**	****}**
**	****@Remote**** public String orderCoffee(String name, int ****	****	****quantity) {**
**	****	****...**
**	****}**
**}**

## Swing Changes

- Not many Swing changes
- Biggest change: New updated Java “Metal” Look and Feel
- More work on the new Windows XP and GTK/Gnome Look and Feels
- Introduced in 1.4.2
- Possible TreeTable component
- More performance work

## XML

- 1.5 adds core support for many new XML standards
- Namespaces
- Schemas
- XSLT
- DOM Level 3
- Optional Web Services Developer Pack adds much more
- WSDL, SOAP, XML DSig, etc.

## XUL and JDNC

- Peyush Jain

## XUL

- XML User Interface Language
- Pioneered by Mozilla
- Makes cross-platform user interfaces as easy as building web pages
- No hard-coded, platform specific user interfaces (e.g. MFC, Qt, etc)
- Not another GUI API/toolkit/framework

## Example

- Swing XML Java Bean Persistence

**<object class="javax.swing.JPanel">**
**  ****<void method="add">**
**    ****<object id="button1" class="javax.swing.JButton"/>**
**  ****</void>**
**  ****<void method="add">**
**    ****<object class="javax.swing.JLabel">**
**      ****<void method="setLabelFor">**
**        ****<object idref="button1"/>**
**      ****</void>**
**    ****</object>**
**  ****</void>**
**</object> **

## Example

- Java Swing

**JPanel panel1 = new JPanel();**
**JButton button1 = new JButton();**
**JLabel label1 = new JLabel();**
**panel1.add(button1);**
**panel1.add(label1);**
**label1.setLabelFor(button1); **

- XUL

**<hbox>**
**  ****<button id="button1" />**
**  ****<label for="button1" />**
**</hbox> **

## XUL Architecture

- User Interface (UI) divided into 4 parts
- Content
- Appearance
- Behavior
- Locale

## XUL Motor - Luxor

- Open-source XUL engine in Java
- XUL toolkit includes web server, portal engine, template engine, scripting interpreter and more.
- Tags

**<box>, <hbox>, <vbox>, <action>, <border>, <button>, <checkbox>, <choice>, <entry>, <icon>, <label>, <list>, <map>, <menu>, <menubar>, <menuitem>, <menuseparator>, <panel>, <password>, <text>, <textarea>, <toolbar> and many more**

## Example

**<list id="card">**
**  ****<entry value="American Express" />**
**  ****<entry value="Discover" />**
**  ****<entry value="Master Card" />**
**  ****<entry value="Visa" />**
**</list>**
**<vbox>**
**  ****<label value="Choose a method of payment:" />**
**  ****<choice list="card" />**
**</vbox>**
**<vbox>**
**  ****<label value="Choose a method of payment:" />**
**  ****<choice list="card" type="radio" />**
**</vbox>**

## XUL Benefits

- Platform Portability
- Splits presentation and application logic
- Makes UI building easier
- UI can be easily updated
- UI can be loaded at start-up from web-server, database, etc
- UI can be tested in browser like a web page

## JDNC

- Java Desktop Network Components
- Used for creating Web-enabled Java desktop clients
- Very High Level Beans: Table, Tree, TreeTable, Editor, Form
- Built-in Data and Networking Smarts
- Leveraging XML for configuration
- Browser or Standalone Deployment

## Example

**Embedding the applet**

**<applet name="JDNCApplet" archive="table.jar,   common.jar" code="*****com.sun.jdnc.JDNCApplet.class*****"   width="800" height="300" align="middle">**
**  ****<param name="*****config*****" value="*****bugtable.xml*****">**
**  ****<param name="boxbgcolor" value="255,255,255">**
**</applet> **

**Configure the data model**
**Configure UI and bindings to the data**

## Example

- Authoring the XML Configuration File

- <jdnc-app>
-   <data id="**bugdata**">
-   <source url="bugs.txt"/>
-   <format mimetype="plain text">
-     <columns>
-       <column id="**bugid**">
-       <link>
-     </column>
-     <column id="**priority**">
-       <integer minimum="1" maximum="5"/>
-     </column>
-     <column id="**state**">
-       <string><values>
-         <value>dispatched</value>
-         <value>fixed</value>
-         <value>closed</value>
-       </values></string>
-     </column>
-  ...

## Example

- <table id="**bugtable**" sortable="true" draggable="true" grid="true">
-   <statusbar/>
-   <rows margin="20"></rows>
-   <columns>
-     <column dataref="**bugdata.bugid**" label="**BugID**“ alignment="center">
-       <sample>8888888</sample>
-     </column>
-     <column dataref="**bugdata.priority**" label="**Pri**">
-       <sample>1</sample>
-     </column>
-     <column dataref="**bugdata.state**" label="**State**" editable="true" alignment="center">
-       <sample>dispatched</sample>
-     </column>
-   </columns>
- </table>
- </jdnc-app>

## Eclipse

- John Jung

## Eclipse - Overview

- The Java IDE
- The Application Platform
- The Debate: SWT vs. Swing

## Java IDE

- Feature-rich IDE
- Java Perspective
- Standard Views: Navigator, Outline, Editor
- Other useful perspectives: Debug, CVS Repository
- Java Editor
- Code assist: hover – javadoc spec, ctrl-click to jump to source, method completion, dynamic syntax check, automatic error corrections
- Code generation: code templates, method insertion/generation, comment generation
- Refactoring: organize imports, renaming/moving
- Excellent help system
- Best Feature: price

## Java IDE

## Application Platform

- Eclipse is actually a thin layer that serves as the middleman between plug-ins and the OS; not just an IDE
- Building applications using Eclipse become possible by developing plug-ins that interact with each other
- Emerges as a competitor to Microsoft’s dominance on application building platforms (Visual Basic)
- Gathering momentum: close to declaring independence from IBM; also close to the version 3.0 release

## SWT vs Swing

- SWT: Standard Widget Toolkit
- Small generic graphics/GUI set
- Buttons, lists, tables, text, menus, fonts, etc
- “Snappy”, faster, uses native widgets – better look and feel
- Memory de-allocation required, not object-oriented, must maintain different versions for different platforms
- Swing
- Large, elegant set of tools
- Object-oriented, large libraries, easier to use, large user base, easy to maintain code, more flexible
- Can be slower, look and feel can be dated

## SWT vs Swing

- IBM – SWT – Eclipse
- Sun – Swing – NetBeans
- Two giants wage battle over which will be the main platform for Java applications
- With Eclipse’s impending independence from IBM, Sun’s next move anticipated

## References

- Java 1.5
- http://java.sun.com/features/2003/05/bloch_qa.html
- _[http://servlet.java.sun.com/javaone/sf2003/conf/sessions/display-1540.en.jsp](http://servlet.java.sun.com/javaone/sf2003/conf/sessions/display-1540.en.jsp)_
- _[http://servlet.java.sun.com/javaone/sf2003/conf/sessions/display-3072.en.jsp](http://servlet.java.sun.com/javaone/sf2003/conf/sessions/display-3072.en.jsp)_
- XUL and JDNC
- _[http://luxor-xul.sourceforge.net/index.html](http://luxor-xul.sourceforge.net/index.html)_
- http://luxor-xul.sourceforge.net/talk/jug-oct-2001/slides.html
- http://luxor-xul.sourceforge.net/talk/vanx-jul-2002/slides.html
- http://www.javadesktop.org/articles/JDNC/index.html
- Eclipse
- http://www.eclipse.org/
- http://www.eclipse.org/eclipse/presentation/eclipse-slides.ppt
- http://www.fawcette.com/javapro/2002_12/magazine/columns/proshop/
- http://news.com.com/2100-7344_3-5149102.html
- http://cld.blog-city.com/readblog.cfm?BID=15428