Sunday, December 24, 2006

Java 5 Feature: An intro to foreach

Looping is a control structure that has been around ever since we started programming. The good old syntax for looping that you would have used most often is:

String[] messages = {"Hello", "Greetings", "Thanks"};
var length = messages.length;
var int i = 0;
for(i = 0; i < length; i++){
System.out.println(messages[i]);
}

What if you want to iterate over a collection, like ArrayList? We will do something like:

Iterator iter = lst.iterator();
for(;iter.hasNext();) {
System.out.println(iter.next());
}


The foreach was introduced to avoid introduction of any new keywords. The foreach introduced in Java 5 simplifies looping further. Let’s take a look at an example of how we can use the new way to loop:

for (String msg : messages) {
System.out.println(msg);
}

You would read
for (String msg : messages)

as "foreach String msg in messages". Within the loop of foreach, msg represents a String element within the array String[].
Instead of inventing another keyword "foreach", Sun decided to use the good old "for". Also, "in" may be used in existing code forfields, variables, or methods. In order to avoid stepping over your toes, they decided to use the ":" instead.

Using foreach with a List
ArrayList lst = new ArrayList();
lst.add(1);
lst.add(4.1);
lst.add("test");

for (Iterator iter = lst.iterator(); iter.hasNext();) {
System.out.println(iter.next());
}
for (Object o : lst) {
System.out.println(o);
}


ArrayList values = new ArrayList();
values.add(1);
values.add(2);
int total = 0;
for (int val : values) {
total += val;
}
System.out.println("Total : " + total);


Can you use foreach on your own class?

You can do that, provided your class implements the Iterable interface. It is pretty simple. For foreach to work, you must return an Iterator. Of course, you know that Collection provides the iterator. However, for you to use foreach on your own class, the use of Collection would have made your class quite bulkier; the Iterable interface has just one method interate().


Pros and Cons:
  • Foreach looks simpler, elegant, and is easier to use
  • You can’t use it all the time, however. If you need access to the index in a collection (for setting or changing values) or you want access to the iterator (for removing an element, for instance), then you can’t use the foreach.
  • Users of your class will not be able to use foreach on your object if you don’t implement the Iterable interface.


Refer to Sun's Java Tutorial for more info.

Java 5 Feature: An intro to Autoboxing

With Java 5 (or Java 1.5 or JSE 5.0 or Java J2SE 5.0 or JDK 1.5), a new feature called Autoboxing has been introduced to the language. it is the automatic conversion of the primitives and wrapper objects.
Boxing is to place a primitive data-type within an object so that the primitive can be used like an object. For example, a List, prior to JDK 1.5, can't store primitive data types. So, to store int type data in a List, a manual boxing was required (i.e from int to integer). Similarly, to retrieve the data back, an unboxing process was required to convert Integer back to int.

Autoboxing is the term for treating a primitive type as an object type. The compiler automatically supplies the extra code needed to perform the type conversion. For example, in JDK 1.5, it now allows the programmer to create an ArrayList of ints. This does not contradict what was said above for List: the ArrayList still only lists objects, and it cannot list primitive types. But now, when Java expects an object but receives a primitive type, it immediately converts that primitive type to an object.

This action is called Autoboxing, because it is boxing that is done automatically and implicitly instead of requiring the programmer to do so manually.

Unboxing is the term for treating an object type as a primitive type without any extra code.

For example, in versions of Java prior to 1.5, the following code did not compile:

Integer i = new Integer(9);
Integer j = new Integer(13);
Integer k = i + j; // error prior to JDK 1.5
Originally, the compiler would not accept the last line. As Integers are objects, mathematical operators such as + were not meaningfully defined for them. But the following code would of course be accepted without complaint:
int i = 9;
int j = 13;
int k = i + j;

int x = 4;
int y = 5;
// Integer qBox = new Integer(x + y);
Integer qBox = x + y;//would have been error,but okay now-equi to prev line
The Integer is unboxed into an int, the two are added, and then the sum is autoboxed into a new Integer.

Pros and Cons:
  • Autoboxing unclutters your code, but comes with important considerations in terms of performance and sometimes unexpected behavior.
  • Understand what == means. For objects, you are comparing identity, for primitive types, you are comparing value. In the case of auto-unboxing, the value based comparison happens.
  • If the Integer object is null, assigning in to int will result in a runtime exception – NullPointerException – being thrown.
Refer to Sun's Java Tutorial for more info.

Tuesday, December 19, 2006

Google Browser Sync

Google Browser Sync for Firefox is an extension that continuously synchronizes your browser settings - including bookmarks, history, persistent cookies, and saved passwords - across your computers. It also allows you to restore open tabs and windows across different machines and browser sessions.
Please note: Google Browser Sync must update your browser settings whenever you start Firefox. This will increase the start-up time of Firefox (the time between clicking on the Firefox icon and loading your start page) - please bear with us as we work to decrease this delay.

How to Use
To use Google Browser Sync, simply install and configure the extension on all computers for which you'd like your browser settings automatically kept in sync. Even if you only install Browser Sync on a single computer, you can use it to backup your browser settings and to restore your open tabs and windows across browser sessions.

Configuring your computers
After successfully downloading and installing the extension, then restarting your browser, you'll see a setup wizard. (Please note that you'll need a Google Account to complete the setup process; if you don't already have a Google Account, you can create one now.) The setup wizard will take you through a series of quick and easy steps. You'll need to choose which browser components you'd like to keep synchronized with your other computers (the standard install includes all possible components), and choose a PIN to protect your sensitive information.
Configuring your second computer is even easier. You only need to install the extension, log in with your Google Account username and password, and provide your PIN.

Day-to-day use
Google Browser Sync is completely automated. The settings you select at startup are automatically synchronized across each of the computers on which you install Browser Sync. You won't even need to log in every time you start the browser. You can change which browser components are being synced - or even stop the syncing process entirely - using the settings panel in the upper-right corner of the page. The settings panel also gives you access to your PIN. How to Uninstall In the Firefox browser, click "Tools" and select "Extensions". The Extensions window should appear. Click "Google Browser Sync", then click the "Uninstall" button. Click the "OK" button in the confirmation window. The Extensions window should confirm that Google Browser Sync has been uninstalled. Close the Extensions window, then close all Firefox windows and restart the browser.

link:
http://www.google.com/tools/firefox/browsersync/index.html

Thursday, December 7, 2006

To Ruby From Java - Similarities and Differences

Came through this interesting post comparing Java with Ruby.

Java is mature. It's tested. And it's fast (contrary to what the anti-Java crowd may still claim). It's also quite verbose. Going from Java to Ruby, expect your code size to shrink down considerably. You can also expect it to take less time to knock together quick prototypes.

Similarities

As with Java, in Ruby...

  • Memory is managed for you via a garbage collector.
  • Objects are strongly typed.
  • There are public, private, and protected methods.
  • There are embedded doc tools (Ruby's is called RDoc). The docs enerated by RDoc look very similar to those generated by javadoc.

Differences

Ulike Java, in Ruby...

  • You don't need to compile your code. You just run it directly.
  • There are different GUI toolkits. Ruby users can try WxRuby, FXRuby, Ruby-GNOME2, or the bundled-in Ruby Tk for example.
  • You use the end keyword after defining things like classes, instead of having to put braces around blocks of code.
  • You have require instead of import.
  • All member variables are private. From the outside, you access everything via methods.
  • Parentheses in method calls are usually optional and often omitted.
  • Everything is an object, including numbers like 2 and 3.14159.
  • There's no static type checking.
  • Variable names are just labels. They don't have a type associated with them.
  • There are no type declarations. You just assign to new variable names as-needed and they just "spring up" (i.e. a = [1,2,3] ratherthan int[] a = {1,2,3};).
  • There's no casting. Just call the methods. Your unit tests should tell you before you even run the code if you're going to see an exception.
  • It's foo = Foo.new( "hi") instead of foo = new Foo( "hi" ).
  • The constructor is always named "initialize" instead of thename of the class.
  • You have mixin's instead of interfaces.
  • YAML tends to be favored over XML.
  • It's nil instead of null.

http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-java/

Wednesday, December 6, 2006

HtmlUnit - unit testing framework for web applications

HtmlUnit is a java unit testing framework for testing web based applications. It is similar in concept to httpunit but is very different in implementation. Which one is better for you depends on how you like to write your tests.
HttpUnit models the http protocol so you deal with request and response objects. HtmlUnit on the other hand, models the returned document so that you deal with pages and forms and tables.

HtmlUnit is not a generic unit testing framework. It is specifically a way to simulate a browser for testing purposes and is intended to be used within another testing framework such as JUnit.

Features
Support for the HTTP and HTTPS protocols. ( JSSE must be in your classpath in order to use HTTPS support)
Support for cookies
Ability to specify whether failing responses from the server should throw exceptions or should be returned as pages of the appropriate type (based on content type)
Support for submit methods POST and GET
Ability to customize the request headers being sent to the server
Support for html responses
o Wrapper for html pages that provides easy access to all information contained inside them
o Support for submitting forms
o Support for clicking links
o Support for walking the DOM model of the html document
Proxy server support
Support for basic authentication
Partial support for javascript (see javascript section below)

JavaScript support
Starting with the 1.1 release, HtmlUnit is supporting javascript. The core javascript language is already fully supported by the Rhino javascript engine.

http://htmlunit.sourceforge.net/


Digg This Article Bookmark The Article On Delicious Bookmark at YahooMyWeb Bookmark at reddit.com Bookmark at NewsVine Fark this

Helipad - The flexible web notepad

Helipad is a web-based tool, which means you can use it wherever you can get online.

Since Helipad is a hosted application, we do all the boring stuff: backup documents, maintain security, upgrade software, while you're left to do the most important thing: write.
  • Write notes and organize them with tags
  • Create and maintain to-do lists
  • Draft documents on any computer or phone with Internet access
  • Share your work with others

Features
  • Unobtrusive auto-save: set a timer to save automatically so you
  • don't lose your work
  • Organize your documents with tags
  • Quickly find documents with a live-search on every page
  • Customize your Helipad with themes and plugins
  • Share your documents with friends
  • Prepare documents for print by changing fonts

Get more out of Helipad
  • Helipad works with popular phone web browsers.
  • Your notes look great printed from Helipad.
  • Developers can get more out of Helipad by using the API, or by writing add-ons.


Digg This Article
Bookmark The Article On Delicious
Bookmark at YahooMyWeb
Bookmark at reddit.com
Bookmark at NewsVine
Fark this

Friday, December 1, 2006

DropBoks

DropBoks is a little website that allows you to securely upload and download your files as you please. No bells, no whistles, just an easy and quick way to store your files online.

Each account has 1 GB of storage space available. You can upload and download files (any format) as large as 50 MB.

How are we able to keep DropBoks free? We have found that when you offer a
valuable service that is extremely simple to use, folks have no problem making a
small donation. This has covered our costs since day one!



Total Pageviews

Reading List - 05-Mar-23

Talking to #AI may be the most important job skill of this century ( JohnGoodPasture ) Role of account management over the years. It’s a ro...