Skip to main content

Posts

Showing posts with the label java 5

Java 1.5 - Application Memory Monitoring Made Easy

There is a new tool called Java Monitoring & Management Console in 1.5 edition of J2SE (available as % JDK_HOME%\bin\jconsole.exe ). Simply set JAVA_OPTS=-Dcom.sun.management.jmxremote and start your Application Server or Servlet Container. Launch JConsole tool, and when asked for attaching, pick the appropriate process to attach. [Pic 1 - Choose a Process to attach to.] [Pic 2 - JConsole in Action] I have a JDK 1.6 on W2K machine. Here is a link from Sun's Java website. Advertisement Amazon.com Textbooks

Java 5 Feature: Static Imports

Static imports are another convenience feature added to version 1.5 that extends the way imports work in Java. For example, consider the code fragment shown below that calls the static ceil() method on the java.lang.Math class // x is a number of type double such as 98.765 double y = Math.ceil(x); With static imports in 1.5, you can ask the Java compiler to import only a class's static portions, as shown, for example, in the rewritten code fragment below: // Import declaration at the top of the class along with the other imports import static java.lang.Math.ceil; // And then somewhere in the code... // x is a number of type double such as 5.345 double y = ceil(x); In the above fragment, I used the new static import feature to import the static method ceil() from the Math class. Now when I call the method, I don't have to qualify it with Math. If I wanted to use multiple static methods from the Math class, I could import them individually or all at once as shown below: // Import...

New Features in Java 5

Several useful features have been introduced in Java 5. Generics : A way to make classes type-safe that can work on any type, such as narrowing an instance of a collection to hold a specific object type and eliminating the need to cast objects when taking an object out of the collection. Enhanced for loop : A cleaner and less error-prone version of the for loop for use with iterators. Variable arguments : Support for passing an arbitrary number of parameters to a method. Boxing/unboxing : Direct language support for automatic conversion between primitive types and their reference types (such as int and Integer). Type-safe enumerations : Clean syntax for defining and using enumerations, supported at the language level. Static import : Ability to access static members from a class without need to qualify them with a class name. Metadata : Coupled with new tools developed by third-party companies, saves developers the effort of writing boilerplate code by automatically generating the code...

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 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 keywor...