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());
Console output

true
JavaQuest

  1. The imported Optional class comes from java.util package.
  2. tekstas stores JavaQuest value.
  3. describeConstable() returns Optional<String>, so the result is saved in the variable aprasymas.
  4. isPresent() checks whether the Optional contains a value. String does contain one, so the result is true.
  5. 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()

Parts of the describeConstable() method
PartMeaning
Calling objectA specific String, for example, tekstas
ParametersThe method takes no parameters
Return typeOptional<String>
Contained valueThe same String object that called the method
InterfaceThe method implements Constable.describeConstable()
Java versionAvailable 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);
Console output

Mokausi Java

  1. describeConstable() wraps the tekstas value in an Optional<String>.
  2. orElseThrow() retrieves the text or would throw an exception if no value were present.
  3. String always provides a descriptor, so reiksme receives Mokausi Java.

The method returns the same String object

String tekstas = new String("Java");
String aprasytaReiksme = tekstas.describeConstable().orElseThrow();

System.out.println(tekstas == aprasytaReiksme);
Console output

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());
Console output

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());
Console output

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.

A common compiler message

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());
Result

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 String directly when the method returns an Optional<String>.
  • Assuming runtime-created text returns an empty Optional; every String describes 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 null reference and getting a NullPointerException.

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.