Library contents

Java String.describeConstable

Expose a String as a nominal constant descriptor for tools that work with JVM constants.

A simple example

String.describeConstable

  1. Start with "A"
  2. Do this Ask for its constant description
  3. Result Present, holding A
A string can describe itself as a constant. Unwrapping the optional description yields the same text.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("A".describeConstable().orElseThrow());
    }
}
Output
A

Syntax

Java 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.java

Describe 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.

From the programString label = "BIO-101"
ResultBIO-101
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String label = "BIO-101";
        System.out.println(label.describeConstable().orElseThrow());
    }
}
Output
BIO-101

A tool can obtain a descriptor without inventing a separate encoding for this string.

Advertisements

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.

From the programString blank = ""
Resulttrue
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String blank = "";
        System.out.println(blank.describeConstable().isPresent());
    }
}
Output
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?

GivenNC-17
Worked resultNC-17

What to watch for

For String the Optional contains the string itself. This API describes constants and is separate from application serialization.

Documentation for String.describeConstable