Java String.describeConstable
Expose a String as a nominal constant descriptor for tools that work with JVM constants.
A simple example
String.describeConstable
- Start with "A"
- Do this Ask for its constant description
- Result Present, holding A
class Main {
public static void main(String[] args) throws Exception {
System.out.println("A".describeConstable().orElseThrow());
}
}
A
Syntax
public Optional<String> describeConstable()
JDK 21 method signatures (1)
public Optional<String> describeConstable()Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaDescribe a course label for a bytecode tool
A bytecode tool needs a symbolic representation of a fixed course label. Request the label's descriptor and inspect the available value before using it in the generated class metadata.
class Main {
public static void main(String[] args) throws Exception {
String label = "BIO-101";
System.out.println(label.describeConstable().orElseThrow());
}
}
BIO-101
A tool can obtain a descriptor without inventing a separate encoding for this string.
Describe an empty configuration constant
A JVM tool inspects a string that may become a constant in generated code. Ask for its constant description and examine what is present before treating the descriptor as a usable value.
class Main {
public static void main(String[] args) throws Exception {
String blank = "";
System.out.println(blank.describeConstable().isPresent());
}
}
true
An empty string is still a present, valid constant descriptor.
Read a parcel sorting code
The depot scans a parcel marked NC-17. The program recovers the text of this existing string for a scanner log. What exact sequence of characters should survive?
What to watch for
For String the Optional contains the string itself. This API describes constants and is separate from application serialization.