|
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: https://softwarereg.sun.com/registration/developer/en_US/subscriptions
For more Java technology content, visit these sites:
java.sun.com - The latest Java platform releases, tutorials, and newsletters.
java.net - A web forum for collaborating and building solutions together.
java.com - The marketplace for Java technology, applications and services.
Java Programming Language Basics
Making Do with javac Lint
For old-time C programmers, lint was a tool that came with the UNIX operating system from AT&T. It was meant to remove bits of fluff from programs, or to remove elements that needed to be examined more closely, like portability programs or style issues. Developers who use the Java programming language have had to rely on third-party tools for lint-like code analysis, but no more. The J2SE version 5.0 SDK now provides some limited lint compile time checks. Through just a few command-line options, you can be cleaning up your code in no time.
A simple example of lint with a 1.4.x compile-time option is the -deprecation compile-time switch. With this option, the compiler tells you if you are using a member or class that has been deprecated. (Deprecation basically means that its usage should be avoided, either because it's been replaced, or deemed to be unsafe.) To demonstrate, the following program calls the stop method of Thread. This should be avoided because the thread doesn't clean up after itself.
public class Test1 {
public static void main(String args[]) {
Thread thread = Thread.currentThread();
thread.stop();
}
}
Compiling this program without any command-line options yields under 1.4:
Note: Test1.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
Under 5.0:
Note: Test1.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Notice that the 'old' -deprecation option is now also available with the -Xlint:deprecation option.
When compiled with this compile-time switch, additional information is displayed about the problem:
1.4:
Test1.java:4: warning: stop() in java.lang.Thread has been
deprecated thread.stop();
^
1 warning
5.0:
Test1.java:4: warning: [deprecation] stop() in java.lang.Thread has
been deprecated thread.stop();
^
1 warning
Basically the same information for both versions, with the additional "[deprecation]" there for 5.0.
The 5.0 JDK adds several additional lint-like compilation options. First, you can enable all warnings (yet to be explained) with -Xlint option. Keep in mind, these are just warnings. It is possible that the issues flagged are valid. Warnings do not prevent .class files from being generated. The first warning after deprecation is the fallthrough option (-Xlint:fallthrough). This flags switch blocks with cases that don't include break statements.
switch (intVar) {
case CONSTANT1:
System.out.print("Hello, ");
case CONSTANT2:
System.out.println("World");
}
Notice that if intVar is equal to CONSTANT1, the output will be "Hello, World". This is because of the fallthrough from one case block to another. There isn't a break statement between the two. In this particular case, it appears as though this was knowingly done, but had this source been compiled with the -Xlint:fallthrough option, the compiler would have warned you of the issue and you could have reexamined it to make sure you got the desired behavior.
The other lint compile-time options are listed below:
-Xlint:none
Disable all warnings not mandated by the Java Language Specification.
-Xlint:-xxx
Disable warning xxx, where xxx is one of the warning names supported for -Xlint:xxx, below
-Xlint:unchecked
Give more detail for unchecked conversion warnings that are mandated by the Java Language Specification.
-Xlint:path
Warn about nonexistent path (classpath, sourcepath, etc) directories.
-Xlint:serial
Warn about missing serialVersionUID definitions on serializable classes.
-Xlint:finally
Warn about finally clauses that cannot complete normally.
Static code analysis is becoming an art in and of itself, with IDEs and standalone tools providing many additional analysis options like finding duplicate code across different classes. With the 5.0 JDK, you get many checks for free. Just remember to always compile with the -Xlint options enabled.
Making Sense of the Java Classes
Using Properties to Manage Program Attributes
The Java Tutorial
As noted in the API reference documentation, the Properties class in the java.util package manages a set of key/value pairs. A key/value pair is like a dictionary entry: The key is the word, and the value is the definition. This is a perfect match for managing the names and values of attributes. Each Properties key contains the name of a system attribute, and its corresponding Properties value is the current value of that attribute.
The System class uses a Properties object for managing system properties. Any Java program can use a Properties object to manage its program attributes. The Properties class itself provides methods for the following:
- Loading key/value pairs into a
Properties object from a stream
- Retrieving a value from its key
- Listing the keys and their values
- Enumerating over the keys
- Saving the properties to a stream
Properties extends the HashtableProperties class and inherits methods from it for doing the following:
- Testing to see if a particular key or value is in the
Properties object
- Getting the current number of key/value pairs
- Removing a key and its value
- Adding a key/value pair to the
Properties list
- Enumerating over the values or the keys
- Retrieving a value by its key
- Finding out if the
Properties object is empty
Security Considerations: Access to properties is subject to approval by the current security manager. The example code segments in this section are assumed to be in standalone applications, which, by default, have no security manager. If you attempt to use this code in an applet, it may not work, depending on the browser or viewer in which it is running. See Security Restrictions (in the Essential Java Classes trail) for information about security restrictions on applets.
The Life Cycle of a Program's Properties
The following figure illustrates how a typical program might manage its attributes with a Properties object over the course of its execution.
Read more
Java Bits
Using Swing's Pluggable Look and Feel
by Thomas Kenneth
This article discusses the Swing concept of a pluggable look and feel and offers some thoughts on how to use it in a way that is both convenient for the programmer and desirable for the user experience. What Does "Pluggable Look and Feel" Mean?
Swing is a part of the Java Foundation Classes (JFC) and implements a set of graphical user interface (GUI) components with a pluggable look and feel (L&F). In short terms, this means that Swing-based applications can appear as if they are native Windows, Mac OS X, GTK+, or Motif applications, or they can have a unique Java look and feel through the "Metal" package. Applications can also provide a completely new user experience by implementing a totally unprecedented L&F.
In its early days, Java offered only GUI elements that used native components (rendered and handled by the underlying windowing system). To be platform-independent, Java could provide only such elements and features that had counterparts on all supporting platforms. Today, we know that this was not enough for rich client applications. The growing demand for powerful UI elements led to the development of JFC and Swing, which overcame the limitations of the original implementation. Unlike their predecessors (the original AWT components are still available, of course), Swing components do not rely on the underlying windowing system; they are "lightweight," rendered entirely in Java. Having said that, one might argue that in a very strict sense they are not "native." For the Java look and feel, this is certainly true. And even the Windows look and feel in its current implementation only mimics its native counterpart. But this is not a general problem of Swing or the pluggable look and feel concept. We could write a new look and feel that uses native Windows APIs to create, paint, and maintain GUI elements.
Read more
Program Challenge
How many different ways can you load a Properties object from a file? Write a program that demonstrates as many different ways that you can come up with. The provided solution offers three. Can you come up with more? The loaded Properties object should have three properties set. The keys should be first, alpha, and winner, with their respective values being last, omega, and loser.
- first = last
- alpha = omega
- winner = loser
See possible solution
What's New on Java.net
JDBC RowSet Implementations Tutorial
The JDBC RowSet Implementations Tutorial explains how to use the standard JDBC RowSet implementations that are provided as part of Java 5.0, and get up to speed with each RowSet definition, while benefiting from improved scalability and robustness.
Read articles and tutorials that focus on JavaServer Faces technologies.
Register and join the new2java project to see other articles and tutorials.
Sun's Online Courses
Sun Certified Programmer for Java 2 Platform 1.4
The Sun Certified Programmer for Java 2 Platform 1.4 certification exam is for programmers experienced in using the basic syntax and structure of the Java programming language. Certification is available for the Java 2 Platform.
Read more
Sun's Instructor Led Courses
Java Technology for Structured Programmers
The Java Technology for Structured Programmers course provides students with an intensive introduction to these basics as well as other related subjects, such as graphical user interfaces (GUIs) and event-driven processing. By performing hands-on exercises using the Java 2, Standard Edition (J2SE) Software Development Kit (Java 2 SDK), participants can also learn how to code Java technology applications and applets that perform exception handling and access data with Java technology file input output (I/O) features. Class discussions explore the benefits and uses of Java technology in the legacy environment.
Read more
Prepare to Become Sun Certified
Career Accelerator Packages are a heavily discounted package for people preparing to become Sun certified that include instructor-led and Web-based training, ePractice exams, and actual certification exams. It's everything you need to prepare for certification, without the guesswork. For a limited time, we are offering a $400 Sony music player with the purchase of a package.
Read More
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.
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.
|