Previous | Next | Trail Map | To 1.1 -- And Beyond! | Migrating to 1.1

Alternatives to Deprecated Methods in java.lang, java.net, and java.util

The java.lang.String Class

The following constructors and methods in java.lang.String were deprecated in favor of their replacements which better support internationalization. These constructors and methods did not properly convert bytes into characters. The new constructors and methods use a named character-encoding or the default character-encoding to do the conversion.

Deprecated Methods Alternatives
String(byte[], int) String(byte[]) or String(byte[], String)
String(byte[], int, int, int) String(byte[], int, int) or String(byte[], int, int, String)
getBytes(int, int, byte[], int) byte[] getBytes(String) or byte[] getBytes()

The String and StringBuffer Classes(in the Learning the Java Language trail) covers the String class thorougly.

The java.lang.System Class

The getenv method has been out of use for several releases of the JDK. The 1.1 release formalizes this change by deprecating the method. Programmers should use Properties to store information between invocations of a program.

Deprecated Methods Alternatives
String getenv(String) String getProperty(String)

Setting Up and Using Properties(in the Learning the Java Language trail) talks about how to use the Properties class and our online trail Isolating Locale-specific Objects in a ResourceBundle(in the Internationalization trail) shows you how to use Properties to help internationalize your Java programs.

The java.lang.Runtime Class

The following methods in the java.lang.Runtime class have been deprecated because the JDK 1.1 introduced new internationalization features that replaced them. The new internationalization API includes I/O classes that translate a byte stream into a character stream based on a character-encoding.

Deprecated Methods Alternatives
InputStream getLocalizedInputStream(InputStream) InputStreamReader(in the API reference documentation) or BufferedReader(in the API reference documentation)classes
OutputStream getLocalizedOutputStream(OutputStream) OutputStreamReader(in the API reference documentation) or BufferedWriter(in the API reference documentation) or PrintWriter(in the API reference documentation) classes

You can learn more about the Runtime class in: The Runtime Object(in the Learning the Java Language trail).

The java.lang.Character

The following methods in the java.lang.Character class were deprecated in favor of their alternativies which better support internationalization.

Deprecated Methods Alternatives
boolean isJavaLetter(char) boolean isJavaIdentifierStart(char)
boolean isJavaLetterOrDigit(char) boolean isJavaIdentifierPart(char)
boolean isSpace(char) boolean isWhitespace(char)

The tutorial does not cover this class.

The java.lang.ClassLoader

The following method in java.lang.ClassLoader has been deprecated in favor of the version that takes a String as a first argument because the latter is more secure.

Deprecated Methods Alternatives
Class defineClass(byte[], int, int) Class defineClass(String, byte[], int, int)

The tutorial does not cover this class.

The java.net.Socket

Two constructors in the java.net.Socket class were deprecated. These constructors allowed programmers to create a DatagramSocket. Now programmers should explicitly construct a DatagramSocket if they want one.

Deprecated Methods Alternatives
Socket(InetAddress, int, boolean) One of DatagramSocket's three constructors
Socket(String, int, boolean) One of DatagramSocket's three constructors

You can learn about Socket and DatagramSocket in All About Sockets(in the Custom Networking trail) and All About Datagrams(in the Custom Networking trail), respectively.

The java.util.Date

Many of the methods in java.util.Date have been deprecated in favor of other APIs that better support internationalization. The following table provides a list of these methods and their alternatives.

.
Deprecated Methods Alternatives
Date(int, int, int),
Date(int, int, int, int),
Date(int, int, int, int, int, int), and
Date(String)
Create a java.util.GregorianCalendar object and use its getTime method to convert it to a Date
int getYear(),
int getMonth(),
int getDate(),
int getHours(),
int getMinutes(),
int getSeconds(),
int getDay(),
int getTimezoneOffset(),
setYear(int),
setMonth(int),
setDate(int),
setHours(int),
setMinutes(int),
setSeconds(int), and
UTC(int, int, int, int, int, int)
Create a java.util.GregorianCalendar object and use its setters and getters instead.
parse(String),
String toLocaleString(), and
String toGMTString()
Use java.text.DateFormat and its subclasses to parse and format dates.

Instinctively, when programmers want to create the current date, they immediately look at the Date class. While intuitive, this is usually the wrong choice. For storing date and time information, most programmers should use the Calendar class, and to format dates and times they should use the java.text.DateFormat class. This is all described with task-oriented documentation and examples in Internationalization(in the Internationalization trail).


Previous | Next | Trail Map | To 1.1 -- And Beyond! | Migrating to 1.1