advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Articles : Java Language Enhancements :

Java Language Enhancements

by Benoy Jose

Introduction

The Java language is already powerful enough in many aspects, but it does not hurt to add a few more features that would make it more powerful and programmer friendly. With the advent of Microsoft’s Microsoft.net, Sun has been under enormous pressure to enhance the Java language and make it more programmer friendly. Though the Java community is against major changes to the language, the pressure has been on to add few features that were inherently missing in Java. A major slew of features like those in Microsoft.net would make Java less flexible, so Sun has carefully chosen a few features that already existed in languages like C and C++, but were left out in the original Java specification. Keeping this in mind Sun and its partners have proposed a set of enhancements to the Java language through the Java specification request JSR 201. The proposed enhancements are as follows:

  • An enhanced "for loop" that would allow easier iteration over collections.
  • An automatic conversion mechanism to convert primitive types to their corresponding wrappers and vice versa.
  • A new syntax for an enumerated type
  • A static import.
  • Variable-Arity Methods.

These proposed enhancements have been included into the J2SE 1.5 tiger edition which is presently in the beta 1 stage.

 

Enhanced For Loop:

The "for loop" is a very powerful construct when dealing with simple loops. But when it comes to a collection of string values it becomes worse. Take the sample below all the elements of this ArrayList are Strings yet we need to specify the cast each time an element is retrieved. Well I am not implying that the collections framework design is flawed. The idea of collections to include any object makes it truly object oriented but this feature brings in an additional burden of checking and converting objects each time they are stored in a collection. The enhanced "for loop" makes the iteration through collections easy while maintaining the integrity of the language.

 
int [] intArray = new int[10];
for (int i=0; i< 10; i++){
	System.out.println (“Value at ”+I + “ is ”+intArray[i]);
} 
ArrayList  arrayList = (ArrayList) someColl;
for (int i =0; I < arrayList.size(); i++){
	String tempString = (String) arrayList.get(i);
}

	How about this  
for  (String tempString : someColl ){
	
}

In this example tempString is some String where the value is held and someColl is a collection of type java.util.Collection. The above piece of code is an example of the new enhanced "for loop"’ proposed by the JSR 201. The enhanced "for loop" makes it easier to iterate through collections and Arrays while making code simple and more readable. The enhanced "for" uses the existing "for" keyword with some modifications so that the virtual machine is left undisturbed. The enhanced "for" construct is accommodated by modifying the compiler and these changes do not affect existing programs in any way. To make this possible a new interface called the java.lang.Iterable is being introduced. Any collection that can be used in the enhanced "for loop" needs to be an instance of the Iterable interface. For the coexistence with the existing Collections framework the java.langCollection interface will be retrofitted to implement or extend the Iterable interface.

The enhanced "for loop" assumes that the elements contained in the collection are of the same type. If this assertion is false an improper assignment exception is thrown. Additionally the enhanced "for loop" acts more like an enhancement to the "Iterator" interface. The enhanced for loop has some drawbacks as it does not allow the user to modify or delete elements in a collection as an iteration is being done. This is more on the lines of what the java.util.Iterator does, the enhanced "for loop" actually moves through the collection and exhausts it as it finishes through it, but this does not mean that the collection is exhausted or deleted, the rules that apply for the Iterator apply for the enhanced "for loop". The enhanced "for" also does not allow simultaneous iterations over multiple collections. A sample of a HashMap is given below:

 HaspMap myMap = someHashMap;
 for (Map.Entry<String, String> e : myMap.entrySet()){
 System.out.println ("Key "+ e.getKey() + " Value "+ e.getValue());
}

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs