In Java, describeConstable() returns an Optional<String> that contains the string itself. A String can describe itself because it already is a valid constant description: the value and its description are the same object.
The method comes from the Constable interface in java.lang.constant, added for tools that generate or analyze class files (bytecode libraries, compilers, frameworks). In everyday code you rarely need this method; it is good to recognize when you meet it in an API.
describeConstable() is an instance method of java.lang.String, added in Java 12. Call it on a string, as in text.describeConstable(); calling it on a null reference throws NullPointerException. Requires Java 12 or newer.
Syntax
Call it with no arguments; the result is never empty for a String.
java
Optional<String> desc = text.describeConstable();
String same = desc.get(); // the same object as text
Call
What it does
"a".describeConstable()
Optional[a].
"a".describeConstable().isPresent()
true.
s.describeConstable().get() == s
true.
Types like Integer, Long, Double, and String also implement ConstantDesc, so they describe themselves. Other Constable types return a separate descriptor object, and some may return an empty Optional.
Library declarations
java
public Optional<String> describeConstable();
Parameters
Parameter
Description
None
describeConstable() takes no parameters.
Return Value
Return Value
Description
Optional<String>
An Optional containing this string.
Java Examples
Example 1 — Inspect the Returned Optional
Check that a value is present and print it in two ways.
java
import java.util.Optional;
public class DescribeConstableBasic {
public static void main(String[] args) {
Optional<String> desc = "hello".describeConstable();
System.out.println(desc.isPresent());
System.out.println(desc.get());
System.out.println(desc);
}
}
Output
true
hello
Optional[hello]
How It Works
Always present. For a String the Optional is never empty, so isPresent() prints true.
The value is the text.get() returns "hello", because a string is its own constant description.
Optional's own text form. Printing the Optional shows the wrapper as Optional[hello].
Example 2 — The Description Is the Same Object
Compare references and use map on the Optional.
java
import java.util.Optional;
public class DescribeConstableSame {
public static void main(String[] args) {
String word = "Java";
Optional<String> desc = word.describeConstable();
System.out.println("same object? " + (desc.get() == word));
System.out.println("length via map: " + desc.map(String::length).orElse(0));
}
}
Output
same object? true
length via map: 4
How It Works
No copy is made. The method wraps this, so the Optional holds the very same object and == is true.
Normal Optional operations.map(String::length) transforms the contained string into its length.
A fallback that is never used.orElse(0) would apply to an empty Optional; here the value 4 is present.
Example 3 — Call describeConstable() Through Constable
A helper that accepts any Constable value.
java
import java.lang.constant.Constable;
import java.util.Optional;
public class DescribeConstableHelper {
static String describe(Constable value) {
Optional<?> d = value.describeConstable();
return d.toString();
}
public static void main(String[] args) {
System.out.println(describe("text"));
System.out.println(describe(42));
System.out.println(describe(2.5));
}
}
Output
Optional[text]
Optional[42]
Optional[2.5]
How It Works
One interface, many types.String, Integer, and Double all implement Constable, so the helper accepts each of them.
A wildcard Optional. The interface returns Optional<? extends ConstantDesc>; the helper stores it as Optional<?>.
Self-describing values. These three types are their own descriptions, so each Optional simply wraps the original value.
Frequently Asked Questions
An Optional containing the same String. A string is its own nominal constant description.
Java 12, as part of the java.lang.constant API (JEP 334).
Rarely. It exists for libraries and tools that work with class-file constants, such as bytecode generators.
No. For String it always returns a present Optional.
An interface for types whose values can be described as constants in a class file. String, Integer, Long, Float, Double, Class, and enums implement it.