Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

J2SE 1.5: Java's Evolution Continues : Page 4

J2SE 1.5 (codename: Tiger) epitomizes Java's ongoing evolution and adaptation to the emerging needs of programmers and their applications. With features such as generics, autoboxing, and enhanced for loops, it makes Java code easier to develop and read.


advertisement
Formatting of Output
Tiger introduces the java.util.Formatter package, which enables you to justify and align text. It also provides common formats for numeric, string, and data/time data, as well as local-specific output. Inspired by the C programming language's printf offering, this feature shares a lot of similarities with C. The class below shows a couple of examples of leveraging the Formatter package and its help in formatting. Also, note the use of a convenience method (i.e., System.out.format) to format the current time:

import java.lang.StringBuilder; import java.util.Formatter; import java.util.Locale; import java.util.Calendar; public class FormatterExample { public static void main (String args[]) { // Send all output to the Appendable object stringBuilder StringBuilder stringBuilder = new StringBuilder(); // Construct a new formatter Formatter formatter = new Formatter(stringBuilder, Locale.US); float balance = 500434.03F; formatter.format("The current balance is: $ %(,.2f", balance); System.out.println(stringBuilder); // Prints out: The current balance is: $ 500,434.03 // Writes the current time to system out using the format // HH:MM:SS in 12 hour format System.out.format("Local time: %tr", Calendar.getInstance()); // sample output - 05:16:24 System.out.println(); // Writes the current time to system out using the format // HH:MM:SS in 24 hour (aka military) format System.out.format("Local time: %tT", Calendar.getInstance()); // sample output - 17:16:24 } }

Variable Argument Lists
Tiger also introduces support for variable argument lists. In short, you can pass in a variable number of arguments by specifying (...). The simple class below shows how you can use a variable argument list in tandem with the enhanced for loops introduced previously. Note that the demonstrate method does not need to extract the length of your argument list, as it did in previous versions of Java.



public class VariableArgumentLists { public static void demonstrate(String groupName, String... args) { System.out.println("Group Name: " + groupName); for (String current: args) { System.out.println(current); } System.out.println("-----------------"); } public static void main(String args[]) { demonstrate("Stooges", "Larry","Moe","Curly"); demonstrate("Marx Brothers", "Chico", "Harpo", "Groucho", "Gummo", "Zeppo"); demonstrate("Laurel and Hardy", "Stan", "Ollie"); } }

Java's Evolution Continues
Tiger epitomizes Java's ongoing evolution and adaptation to the emerging needs of programmers and their applications. This article presented a sampling of what Tiger will offer. As you have seen, the common theme of the features is that the responsibility of "writing" code has shifted to the compiler behind the scenes. The release is being advertised as a "developer focused" release, and—as usual with Java—the new offerings don't sacrifice backward compatibility. Features such as generics, autoboxing, and enhanced for loops go a long way toward making Java code easier to develop and read.



Kulvir Singh Bhogal works as an IBM consultant, devising and implementing Java-centric solutions at customer sites across the nation. .
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap