.
.
developers.sun.com java.sun.com
.
    January 11, 2005    

In this Issue

 

Welcome to the Java Technology Fundamentals Newsletter.

This monthly newsletter provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to Sun Developer Network's New to Java Programming Center.

You can receive future issues of this newsletter in HTML or text: http://developer.java.sun.com/subscription/

- Java Programming Language Basics: Variable Argument Lists
- Making Sense of the Java Classes & Tools: Using JConsole to Monitor Applications
- Java Bits: Getting Sun Java Certified
- Tiger Tips: Managed Images Everywhere & JFrame.add(...)
- Program Challenge
- What's New on Java.net: Weblogs and Articles
- Sun's Java Technology Fast Track Program I
- Sun's CD-ROM Java Courses
- For More Information

.

Java Programming Language Basics

Variable Argument Lists

Variable length argument lists are a way of defining a method that takes an arbitrarily long argument set. The calling method then loops through each argument, and processes accordingly. For instance, from a command prompt, you can ask your system for the directory of multiple locations.

Microsoft Windows
  dir a* b* c*

Linux
  ls a* b* c*

Each of these commands processes the directive by operating on the first argument, then on the second, and so on, until there are no more arguments. The same operation is performed on each, but there is no fixed limit to the number of locations to check. You can provide no arguments, and each command will default to looking in the current directory, or you can provide a set of directories to search, by providing a space-delimited list.

Another common usage of variable argument lists is with print-like functionality. Instead of the pre-5.0 way of printing lines by concatenating strings together:

String name = "Mary Ellen";
System.out.println("Hello, " + name);

In the 5.0 release of the JDK, you provide a formatting string, and a variable argument list:

System.out.printf("Hello, %\n", name);

Here, instead of combining the stream with a '+' operator, you pass in the name argument to the printf method of PrintStream. Internally, the method then replaces the % in the string with the first argument, name.

Need multiple strings replaced, provide multiple %s.

System.out.printf("Hello, % %\n", first, last);

Theoretically you could have done this in the 1.4 release by providing multiple versions with different argument counts. What the Java 2 Platform, Standard Edition (J2SE) 5.0 release provides is variable argument lists so you don't need to provide multiple versions of methods, depending upon argument length.

printf(String format, Object... args)

The "..." here means after the leading String argument, there can be any number of other arguments, including none. And, since the 5.0 release will automatically box primitive arguments into objects, the fact that the method requires an Object doesn't hinder you from using primitives.

If you're writing your own methods that accept variable argument lists, accessing them requires a little extra thought. Since each argument in the variable argument list doesn't have its own name, the access is done through an array. To demonstrate, here's a method that accepts a variable argument list of numbers, and sums up their total.

  private static double total(Number... args) {
    double sum = 0;
    for (int i=0, n=args.length; i<n; i++) {
      sum += args[i].doubleValue();
    }
    return sum;
  }

The "Number... args" bit here says, this method accepts some variable set of argument. They are accessed via an argument named args, and you access each element by treating "args" just like the main method of a class, looping to each element, typically in a for-loop.

To demonstrate, the following program calls the total method with different argument lengths. The total of summing up various sets of random numbers is then display. Notice that the Total class also uses the variable argument nature of the printf method, not just the newly defined total method of Total.

import java.util.Random;
public class Total {

    private static double total(Number... args) {
        double sum = 0;
        for (int i=0, n=args.length; i<n; i++) {
            sum += args[i].doubleValue();
        }
        return sum;
    }

    public static void main(String args[]) {
        Random random = new Random();
        int first = random.nextInt(100);
        int second = random.nextInt(100);
        int third = random.nextInt(100);
        int fourth = random.nextInt(100);
        System.out.printf("Numbers: %d / %d / %d / %d\n",
            first, second, third, fourth);
        System.out.printf("Total0: %f\n",
            total());
        System.out.printf("Total1: %f\n",
            total(first));
        System.out.printf("Total2: %f\n",
            total(first, second));
        System.out.printf("Total3: %f\n",
            total(first, second, third));
        System.out.printf("Total4: %f\n",
            total(first, second, third, fourth));
        System.out.printf("Total5: %f\n",
            total(Math.PI, Math.E));
    }

}

Each run of the sample program is bound to create unique output. The results of one such run is displayed here:

  Numbers: 99 / 78 / 58 / 10
  Total0: 0.000000
  Total1: 99.000000
  Total2: 177.000000
  Total3: 235.000000
  Total4: 245.000000
  Total5: 5.859874

Total0 is with no arguments, Total1 is with one, and so on. The last line is the sum of totaling Math.PI and Math.E, just to show that any type of number works, not just integers.

For additional information on variable argument lists, see the online documentation with the JDK, available from: http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html

.
.

Making Sense of the Java Classes

Using JConsole to Monitor Applications

The 5.0 release provides comprehensive monitoring and management support. It not only defines the management interfaces for the Java virtual machine(JVM)*, but also provides out-of-the-box remote monitoring and management on the Java platform and of applications that run on it. In addition, 5.0 includes the Java Monitoring and Management Console (JConsole) tool. It uses the extensive instrumentation of the JVM to provide information on performance and resource consumption of applications running on the Java platform using Java Management Extension (JMX) technology.

The article describes how JConsole can be used to observe information about an application running on the Java platform. The article first gives an overview of the 5.0 monitoring and management architecture and how JConsole plugs into the architecture. It then describes how to use JConsole to access several core monitoring and management functions provided by the Java platform including:

  • Detect low memory
  • Enable or disable GC and class loading verbose tracing
  • Control the log level of any loggers in an application
  • Access OS resources--Sun's platform extension
  • Manage an application's Managed Beans (MBeans)

Read more.

.
.

Java Bits

Getting Sun Java Certified

Getting certified is a great way to invest in your professional development and to help boost your career potential. IT managers know the skills verified during the certification process are the same skills that can lead to increased productivity and enhanced staff credibility.

Earning a Sun Java technology certification provides a clear demonstration of your technical skills and professional dedication.

Sun offers the following certifications for Java technologies:

J2SE

  • Sun Certified Programmer for the Java 2 Platform, Standard Edition
  • Sun Certified Developer for the Java 2 Platform, Standard Edition

J2EE

  • Sun Certified Web Component Developer for the Java 2 Platform, Enterprise Edition
  • Sun Certified Business Component Developer for the Java 2 Platform, Enterprise Edition
  • Sun Certified Developer for Java Web Services
  • Sun Certified Enterprise Architect for the Java 2 Platform, Enterprise Edition

J2ME

  • Sun Certified Mobile Application Developer for the Java 2 Platform, Micro Edition

Any IT certification path can be confusing and costly. In addition to reading articles, tutorials, and newsletters on java.sun.com, you can take the Career Accelerator Packages (CAPs) to take the guesswork out of preparing for certification. Each CAP package is specific to the type of certification you want, and CAP packages are available in the following learning modes:

  • Instructor-led Training - Provides lectures and labs delivered by technical experts certified in their field as instructors.
  • Web-based Training - Entitles you to 24/7 access to courseware from a desktop or laptop and select courses include eMentoring from subject matter experts.
  • eMentoring - Provides a question and answer forum with guidance and interaction from subject matter experts and a community of learners who are taking the same course.
  • ePractice Exams - Helps you prepare to take Sun certification exams by practicing before taking the real exam.
  • Exam Vouchers - Gives you entitlement to take the certification exam at an authorized Prometric testing center.
  • Programming Assignments - Submitted online and evaluated by an expert programmer, validates your expertise as a Java programmer or architect.

Certification exams are based on recommended Sun instructor-led classes and six to twelve months of actual job role experience. Sun does not claim that by taking courses you are guaranteed to pass the certification examinations. However, the courses are an important component in certification preparation.

Once you have taken training, either instructor-led or web-based, you've had all your questions answered through eMentoring, you've taken practice exams and done well, received your voucher, and worked out an assignment, then you are ready to take the certification exam.

Exam costs vary depending on the country you live in. Sun certification exams and programming assignments are available for purchase online in four easy steps:

  1. Purchase exam or programming assignment.
  2. Schedule your exam.
  3. Take your test.
  4. Manage your certification progress.

Exams purchased on the Sun web site may only be used in the U.S. If you reside outside the U.S., check the web site to select a country to inquire about products delivered in your country.

Each exam has approximately 60 questions, and you are given 120 minutes to take the exam.

Read more:

CAP offer that includes the $400 Sony music player

Sun Career Accelerator Packages - Java Technology

Web-based Practice Exams

Purchase an Exam

Certification

.
.

Tiger Tips

Managed Images Everywhere & JFrame.add(...)

These tips are provided by Chet Haase

  1. Prior to 5.0, you could only get acceleration for images by either:
  2. - creating a VolatileImage:

        Component.createVolatileImage(w, h) 
        GraphicsConfiguration.createCompatibleVolatileImage(w, h) 
    

    - creating specific types of offscreen images, such as:

        Component.createImage(w, h) 
        GraphicsConfiguration.createCompatibleImage(w, h)
    

    In 5.0, no matter what kind of image you create, if 5.0 can accelerate copies from that image for you. Now you can do this:

       new BufferedImage(w, h, type) 
       Imageio.read() 
    

    ... or anything else to create your image. Now copy that image to the Swing back buffer and watch us accelerate that copy.

  3. JFrame.add(...)

  4. Prior to 5.0, if you wanted to add a component to your JFrame, you needed to add it to the "content pane", like so:

        JComponent component; 
        JFrame frame; 
        // create the component and frame 
        frame.getContentPane().add(component); 
    

    Now, Swing does that tedious step for you:

       JComponent component; 
       JFrame frame; 
      // create the component and frame 
       frame.add(component); 
    

    The same thing is happening under the hood (Swing is adding the component to the content pane), but Swing makes the right ease-of-development decision to simply do what you meant, instead of throwing that pesky Exception when you didn't add it to the content pane.

.
.

Program Challenge

The Java Reflection API offers Class.getMethod() to locate the method of a class and Method.invoke() to invoke that method. Since these methods have been around for some time, they deal with arrays of variable length, instead of argument lists of variable lengths. To facilitate their usage, create a new helper class with a single method that takes a variable argument list:

public class MethodInvoker {
  public static Object invokeMethod(
    Object instance,
    String name,
    Object... args)
}

Implement the method to invoke the original two methods.

See possible solution.

.
.

What's New on Java.net

Weblogs and Articles

Weblogs -- Find out what other developers in the Java community are doing, what technologies they're working with, what kinds of solutions they've discovered, and what their opinions are in the most recent weblogs on http://weblogs.java.net/

Articles -- Behind the Graphics2D: The OpenGL-based Pipeline -- This article describes the current state of the OpenGL-based pipeline as of J2SE 5.0. Keep in mind that this story may change a bit in future releases as we find ways to accelerate more operations using OpenGL

Register and join the new2java project at https://new2java.dev.java.net/ to see other articles and tutorials.

.
.

Sun's Java Technology Fast Track Program I

The Java Technology Fast Track Program I (non-inclusive) course is designed to teach experienced developers all the topics covered in Sun's core Java technology classes: Java Programming Language (SL-275) and Java Programming Language Workshop (SL-285). Combining what would normally take ten days (two weeks) to complete, this five-day, twelve hour per day bootcamp training event is designed to immerse an experienced developer in the structure, syntax, design and development of the Java programming language.

http://suned.sun.com/US/catalog/courses/JB-341.html

.
.

Sun's CD-ROM Java Courses

The Java Programming Language course provides students with a self-paced CD-ROM course that can help experienced programmers learn how to write, compile, and run Java technology applications. This course provides fundamental knowledge about the Java programming language and its runtime environment, object-oriented programming with the Java programming language, creating programs with graphical user interface (GUI) components, handling exceptions, using threads, creating applets, and how input/output and networking work in the Java programming language.

http://suned.sun.com/US/catalog/courses/CDJ-275.html

.
.

For More Information

.
.

Downloading the Java 2 Platform

For most Java development, you need the class libraries, compiler, tools, and runtime environment provided with the J2SE development kit.

As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.

.
.
Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
If you would like a reply to your comment, please submit your email address:
Note: We may not respond to all submitted comments.
.
.

Find archived issues of the following Java technology developer newsletters or manage your current newsletter subscriptions: https://softwarereg.sun.com/registration/developer/en_US/subscriptions

Subscribe to the following newsletters for the latest information about technologies and products in other Java platforms:

- Core Java Technologies Newsletter. Learn about new products, tools, resources, and events of interest to developers working with core Java technologies.
- Mobility Developer Newsletter. Learn about the latest releases, tools, and resources for developers working on wireless and Java Card technologies and applications.
- Core Java Technologies Tech Tips (formerly JDC Tech Tips) Get expert tips, sample code solutions, and techniques for developing in the Java 2 Platform, Standard Edition (J2SE)


You can subscribe to these and other JDC publications on the JDC Newsletters and Publications page

PRIVACY STATEMENT:
Sun respects your online time and privacy. You have received this based on your e-mail preferences. If you would prefer not to receive this information, please follow the steps at the bottom of this message to unsubscribe.


IMPORTANT: Please read our Terms of Use, Privacy, and Licensing policies:
http://www.sun.com/share/text/termsofuse.html
http://www.sun.com/privacy/
http://developer.java.sun.com/berkeley_license.html


Copyright 2004 Sun Microsystems, Inc. All rights reserved. Sun Microsystems, Inc. 10 Network Circle, MPK10-209 Menlo Park, CA 94025 US

FEEDBACK
Comments? Send your feedback on the Java Technology Fundamentals Newsletter to: dana.nourie@sun.com


SUBSCRIBE/UNSUBSCRIBE
- To subscribe, go to the subscriptions page, choose the newsletters you want to subscribe to and click Update
- To unsubscribe, go to the subscriptions page, uncheck the appropriate check box, and click Update


Trademark Information: http://www.sun.com/suntrademarks/
Java, J2EE, J2SE, J2ME, and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.


Sun Microsystems, Inc.
image
image