API deprecation and compatibility
What you will learn
Deprecation warns consumers that an API should no longer be the preferred choice. Removal, binary incompatibility and changed behavior are distinct risks. A useful replacement preserves the contract, including edge cases, rather than merely silencing a compiler warning.
- Interpret deprecated and for-removal metadata.
- Convert legacy dates and text with explicit semantics.
- Plan source and binary compatibility tests for consumers.
Plain Java 21: save each example as Main.java in a fresh directory, compile with javac --release 21 Main.java, then run java Main. Trusted local fixtures only. No network or third-party dependency is needed.
Inspect actual deprecation metadata
public class Main { public static void main(String[] args) throws Exception { var a=java.util.Date.class.getMethod("getYear").getAnnotation(Deprecated.class); System.out.println(a!=null); System.out.println(a.forRemoval()); }}Expected output:
truefalseReflection inspects the annotation without calling the deprecated method. This is Java 21 metadata, not a future removal guarantee.
Read the API contract
@Deprecated may carry since and forRemoval. A deprecated member can still exist and work; forRemoval indicates intent to remove in a future release, not a date guarantee. Check the Javadoc replacement and release notes. Compile with -Xlint:deprecation and -Xlint:removal; triage rather than suppress entire modules. jdeprscan checks deprecated Java SE references in classfiles, not all third-party deprecations. Binary compatibility means existing clients can link without recompilation; source compatibility means clients can rebuild. Changing a method descriptor can break an old caller with NoSuchMethodError even when new sources compile.
Legacy boundaries
Convert java.util.Date via toInstant; a LocalDate needs a zone to become an instant. Avoid default locale or timezone assumptions. Use explicit UTF-8 for files and check newline and malformed-input behavior. Keep ObjectInputStream for trusted, narrowly controlled legacy formats only; do not deserialize untrusted streams. Prefer reviewed explicit schemas for new interchange. Migration requires checking saved data and serialization versions. For app-server migrations, validate Jakarta dependencies, descriptors and runtime behavior; old javax enterprise types are not interchangeable with jakarta equivalents.
Translate a legacy date at an explicit zone
public class Main { public static void main(String[] args) throws Exception { var date=java.util.Date.from(java.time.Instant.parse("2024-01-01T00:30:00Z")); System.out.println(date.toInstant().atZone(java.time.ZoneOffset.UTC).toLocalDate()); System.out.println(date.toInstant().atZone(java.time.ZoneId.of("America/New_York")).toLocalDate()); }}Expected output:
2024-01-012023-12-31One instant can map to different calendar dates. The conversion requires business-zone semantics.
Use explicit text encoding
public class Main { public static void main(String[] args) throws Exception { String text="café"; byte[] bytes=text.getBytes(java.nio.charset.StandardCharsets.UTF_8); System.out.println(java.util.HexFormat.of().formatHex(bytes)); System.out.println(new String(bytes,java.nio.charset.StandardCharsets.UTF_8).equals(text)); }}Expected output:
636166c3a9trueExplicit UTF-8 preserves the stated encoding contract. A round trip alone does not validate all historical file encodings.
Common mistakes
Do not treat deprecation as immediate removal or assume every replacement preserves time-zone, encoding or serialization semantics. An old consumer should be tested without recompiling it.
Practice
Convert 2024-02-01T00:00:00Z to a UTC LocalDate using a legacy Date; print 2024-02-01.
Expected output:
2024-02-01Compare with the manual review criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
API deprecation and compatibility — knowledge check
Java maintenance and ecosystem reasoning.
What does forRemoval=true signal?
What does binary compatibility concern?
Why specify a zone when converting an instant?
Which API is not suitable for arbitrary untrusted serialized data?
Does jdeprscan check all third-party deprecated APIs?