Tuesday, July 12, 2011

Java SE 7

JDK 1.0 was released on the 23rd January 1996. Then came:
  • JDK 1.1 (Feb 1997)
  • J2SE 1.2 (Dec '98)
  • J2SE 1.3 (May '00)
  • J2SE 1.4 (Feb '02)
  • J2SE 5.0 (sept 04)
  • Java SE 6 (Dec '06)
Each of these major releases has had minor releases which dealt with less important things (such as fixing bugs!) and after the 26th update for Java SE 6, it's now finally time for Java SE 7 to be released - officially.  Codenamed Dolphin Java SE 7 has had several preview releases, the 28th of July is the day the binaries get an official release. Incidently, not too many household names have their birthday day on the 28th July (unless you're familiar with Greek singer Nacy Alexiadi, American actor Tom Pelphrey or English Soccer player Michael Carrick) so perhaps the 28th is now a good reason to light a candle on the day which also saw Austria-Hungary declare war on Serbia (thank you Google, Wikipedia...). Having worked on every major release of Java except 1.0, I have to say I'm excited about Java SE 7.0.  Especially since no language changes (i.e. programming constructs) were introduced in Java SE 6 (even though some cool ones were introduced in Java SE 5 such as generics, autoboxing, varargs...)

Language changes is the area that I find most interesting because it is what developers will get to use in their day to day jobs. It's what people will notice. It's what people will talk about. Now, in Java SE 7 the language development was under a sub-project known as Project Coin.  There was an open call for proposals between Feb 27th, 2009 and March 30, 2009 and in total 70 proposals received some extensive discussion. The 70 were then narrowed down to five or so and now we're going to take a look at the ones that are part of Java SE 7.

Binary Literals In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number. Type - inference for Generics JDK 1.5 saw the introduction of generics. One of the great advantages of generics was cleaner code because the need for casting was eliminated (if the generics feature was used correctly!).  However, unless you enjoyed those annoying compile time warnings about type safety you had to generify both the declaration and instantiation. For example, suppose you had a list of tennis balls... The annoying thing about having to do this is that it re-introduced code bloat - something generics was trying to reduce.  Now in Java SE 7 there is type inference for generic instance creation.  You can substitute the parameterized type of the constructor with an empty set of type parameters <> (also known as the diamond operator).  That means you can do: and you won't get any annoying warning but you'll still get all the benefits of compile time type safety provided by generics.

Catching multiple exceptions I really like this one.  Say you're catching multiple exceptions where some of the exceptions don't form an inheritance hierarchy but share the same exception handling code. You're going to end up with code bloat. For example: With Java SE 7, you can now do: Not bad eh - bound to tidy up tonnes of code. In addition to the ability to catch mulitple exceptions there is also some new precision available regarding re-throwing of exceptions.  

Underscores in literals Underscores can be used in literals now to make them more easier to read:

Strings can be used in Switch statements Switch statements are useful. They can be used with primitive types and enumerated types. In Java SE 7, they can also be used with Strings. Note: this simple but useful feature was officially suggested from the lead engineer of Project Coin, Joseph D. Darcy.  You can check out his blog here.

Autoclosing of resources (also known as try-with-resources) This is a classic programming error. You use a resource (e.g. a JDBC connection) and you forget to close it.  Now, once the resource implements java.io.AutoClosable (or the interface java.io.Closeable) you don't need to close it. Yippee. The list of classes in the JDK that implement this in the JDK is pretty extensive.  Just check the JDoc. for the AutoCloseable interface. Interestingly this proposal came from Joshua Bloch, the acclaimed author of the stupendous Effective Java - a must read for all serious programmers. Note in addition to the new progamming features, Java SE 7 also includes:

  • Increased support for dynamic languages (such as Ruby and Python)
  • Improved security support. Eliptic-curve crytography (ECC) implementation will be included.
  • Improved class loader support
  • and much more

Check it out...

References

  1. Binary literals and underscores in literals http://blogs.oracle.com/darcy/entry/project_coin_literal_grammar_hackery
  2. Strings in switch http://blogs.oracle.com/darcy/entry/project_coin_string_switch_anatomy
  3. Diamond operator http://mail.openjdk.java.net/pipermail/coin-dev/2009-November/002393.html and  http://download.java.net/jdk7/docs/technotes/guides/language/type-inference-generic-instance-creation.html
  4. Improved exception handling http://blogs.oracle.com/darcy/entry/project_coin_multi_catch_rethrow
  5. Simplified varargs method invocation http://blogs.oracle.com/darcy/entry/projectcoin_inducing_contributory_pollution
  6. Try-with-resources http://blogs.oracle.com/darcy/entry/project_coin_try_out_try
  7. Java Programming Language Feature enhancements http://download.java.net/jdk7/docs/technotes/guides/language/enhancements.html
  8. Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods http://download.java.net/jdk7/docs/technotes/guides/language/non-reifiable-varargs.html

Saturday, July 2, 2011

HTML5 F.A.Q.

HTML5 has been generating a lot of excitement in the web community as of late. It advances HTML so much that Steve Jobs commented in April 2010 it will render Adobe Flash unnecessary!  This was somewhat sensational considering the ubiquity of Flash.  For example, Flash is the technology used by the very popular site youtube  to display every single one of their videos in users' browsers.  There are many who think that if youtube move to HTML5 it could really be bye bye Flash (for example the popular Lockergnome).  This is something I find difficult to disagree with.
 
Now, I've just finished reading the excellent 'HTML5 Up and Running' by Mark Pilgrim and I put together a list of F.A.Q. about HTML5 which I hope gives you a good feel about what HTML5 brings to the table.

Enjoy :-)

HTML5 General
How do I declare my page to be HTML5?
Simply start it with: <!DOCTYPE html>

Give some examples where HTML5 tries to standardise things?
  • The <nav> element is used for navigation. Before developers would use their own CSS classes to style html lists to get the effect of tabs and navigation lists.
  • HTML5 introduces elements such as <footer>,<header> .  Before developers would write their own custom CSS classes for headers and footers. These CSS classes would have their own naming which could vary from developer to developer.
Browser Support
Is it possible for browsers to support some html 5 features and not others?

Yes.  Firefox 3.0+, Safari 3.0+, Chrome 3.0+ all support canvas. However, if you want geolocation well then it's Firefox 3.5+, Safari 5.0+, Chrome 5.0+.

What is the best way for your page to detect what html 5 features are supported?

Use the modernizr javascript library. It's brilliant.  Not only can it very easily confirm if features such as canvas, video, webworker are supported it can also detect more advanced things such as exactly which video formats (ogg, h264) are supported

Canvas
What is a canvas?
A canvas is rectangle which can be defined for your web page.  There are a range of simple APIs which can be used to draw on the canvas.  They are from the very simple to the very sophisticated - which can be used for animations and even games such as Mutant Zombie Monsters by Bill Scott

Can I have more than one Canvas on the same page?

Yes.  And give each canvas its own id and you can access it like any other element.

How do I get Canvas support for IE7, IE8?
Download the ExplorerCanvas Javascript library (http://code.google.com/explorercanvas/)

Data Storage
Name some differences between HTML5 data storage and cookies?
  • Cookies don't store any information. They are just pointers to information that the client can send to the server.
  • With Cookies the data being stored is server side. HTML5 data storage is client side.
  • Storage mechanism are going to be more sophisticated server side. HTML5 client side data storage will have limitations.
Form Autofocus
What's the big deal with autofocus in HTML5?
  • Now defining which part of the page gets default focus is easy
  • All form controls can have the attribute autofocus
  • Because autofocus can now be done in a standardised way, (i.e. no need for complex javascript) it's easier for a browser to provide the option to disable it.
Geolocation
What kind of location information do I get with Geolocation?
The location information that you can get is basically any of the properties of the position object. These are pretty self explanatory and include:
  • coords.latitude
  • coords.longitude
  • coords.altitude
  • coords.accuracy
  • coords.altitudeAccuracy
  • coords.heading
  • coords.speed
  • timestamp
Does the browser always have to ask the user before any calls can be made to geolocation APIs?
Yes.

Input Types

Give some examples of the new input types supported in HTML 5?
Search boxes, Spin Boxes, Sliders, Color Pickers, Telephone numbers, Web Addresses, Email Addresses, Calendar date pickers, Months, Weeks, Times.

Placeholder text

Does HTML5 include support for placeholder text and what is it?
Placeholder text is default text in an input field used to prompt the user what they need to write.
And Yes its supported.

Video
So all I need to do to show a video on my page is use the <video> element?
Well no. You need to ensure the web page supports:
  • the containing format (e.g. mpeg - 4, ogg, WebM, Audio Video Interleave)
  • the video codec format (e.g. H.264, Theora, VP8)
  • the audio codec format used in the video (e.g. mp3, vorbis)
There is no single combination of container and codecs that works in all browsers.
The <video> element makes it easy to define a preference and fallback formats.

How do I include controls (play, stop, etc.) for my video?
Just include the controls flag. e.g. <video src="myvideo.webm" width="100" height="50" controls></video>

Web Worker 
What is a web worker?
A web worker is a way of running javascript in the background on a separate thread.
You can spawn multiple web worker threads.

Give an example of when you would use a web worker?
If you had to do some number crunching or background I/O and didn't want to block out the user.

Anything else...
Name some other HTML5 features you haven't even covered here?
Microdata, offline and caching features, the <article> element which allows developer more control over headers, svg support and presumably many more...

References
  1. Excellent online ref for HTML 5 http://diveintohtml5.org/
  2. The brilliant modernizr library which lets you check what html 5 support you have http://www.modernizr.com/
  3. Great demos of HTML5 canvas http://www.canvasdemos.com/
  4. Great HTML5 demos and tutorials http://www.html5rocks.com/en/tutorials/workers/basics/
  5. The WHATWG community http://www.whatwg.org/
  6. More HTML5 demos http://html5demos.com/