STRING INSTANCE METHODS
Java String describeConstable() method
describeConstable() provides the String value's nominal constant descriptor. Since every String can describe itself as a JVM constant, the method returns an Optionalcontaining the string itself.
Obtaining a String constant descriptor
import java.util.Optional;
String tekstas = "JavaQuest";
Optional<String> aprasymas = tekstas.describeConstable();
System.out.println(aprasymas.isPresent());
System.out.println(aprasymas.get());true
JavaQuest
- The imported
Optionalclass comes fromjava.utilpackage. tekstasstores JavaQuest value.describeConstable()returnsOptional<String>, so the result is saved in the variableaprasymas.isPresent()checks whether theOptionalcontains a value.Stringdoes contain one, so the result istrue.get()retrieves the contained value and prints the text JavaQuest.
Why is describeConstable() an instance method?
The method describes a specific String object's value, so it is called on that string:
Optional<String> aprasymas = tekstas.describeConstable();
On the left, tekstas is the object describing itself. Different String instances have different contents, so we do not write String.describeConstable() : a static call would lack a specific value to describe.
Method syntax
public Optional<String> describeConstable()
| Part | Meaning |
|---|---|
| Calling object | A specific String, for example, tekstas |
| Parameters | The method takes no parameters |
| Return type | Optional<String> |
| Contained value | The same String object that called the method |
| Interface | The method implements Constable.describeConstable() |
| Java version | Available since Java 12 |
What are Constable and a nominal descriptor?
Constable is an interface for objects that can describe their value as a Java class-file constant. A nominal descriptor is a standardized value representation understood by the JVM and tools working with class files or bytecode.
String is a particularly simple case: a string value can already be a constant, so its descriptor is the Stringitself. The method does not create a human-readable explanation or return a class name or type description.
Why is the result Optional?
The general Constable contract allows an object to have no nominal descriptor, so it uses Optional. However, String.describeConstable() can always return its own text.
String tekstas = "Mokausi Java";
String reiksme = tekstas.describeConstable().orElseThrow();
System.out.println(reiksme);Mokausi Java
describeConstable()wraps thetekstasvalue in anOptional<String>.orElseThrow()retrieves the text or would throw an exception if no value were present.Stringalways provides a descriptor, soreiksmereceives Mokausi Java.
The method returns the same String object
String tekstas = new String("Java");
String aprasytaReiksme = tekstas.describeConstable().orElseThrow();
System.out.println(tekstas == aprasytaReiksme);true
describeConstable() does not copy the text. The value retrieved from Optional is the same String instance, so a deliberate reference comparison using == returns in this example true.
It also works for text created at runtime
String kalba = "Java";
int versija = 21;
String tekstas = kalba + " " + versija;
System.out.println(tekstas.describeConstable().isPresent());
System.out.println(tekstas.describeConstable().orElseThrow());true
Java 21
The text need not be a literal in code. Even if a String is built at runtime, its value can still describe itself, so the Optional is not empty.
An empty String also has a descriptor
String tekstas = "";
Optional<String> aprasymas = tekstas.describeConstable();
System.out.println(aprasymas.isPresent());
System.out.println(aprasymas.orElseThrow().length());true
0
An empty string is a valid String value, not an absent value. Therefore, Optional contains empty text, and the retrieved string's length is 0.
Available since Java 12
describeConstable(), Constable and the related constant descriptor API arrived in Java 12. If a project compiles with Java 11 or earlier, the compiler will not find this method in the String class.
cannot find symbol: method describeConstable()
In that case, check the project's JDK and compilation --release or sourceCompatibility settings.
The method cannot be called on null
String tekstas = null;
System.out.println(tekstas.describeConstable());NullPointerException
null is not String object and therefore has no method to call. This differs from the empty string "": empty text is a real object, and its describeConstable() works.
Common mistakes
- Expecting to receive a
Stringdirectly when the method returns anOptional<String>. - Assuming runtime-created text returns an empty
Optional; everyStringdescribes itself. - Confusing a nominal descriptor with a human-readable object description or the
toString()method. - Using the method in a project compiled with a Java version earlier than 12.
- Calling the method through a
nullreference and getting aNullPointerException.
When should you use describeConstable()?
Everyday text processing rarely needs it. It mainly serves libraries and tools that analyze constants, generate bytecode or work with Java class files. To print text, use the Stringitself; to represent a potentially absent value safely, create an Optional according to your application's logic.