Library contents

Java String.resolveConstantDesc

Resolve a String constant descriptor using the standard constant-description API.

A simple example

String.resolveConstantDesc

  1. Start with The string constant "A"
  2. Do this Resolve its description
  3. Result "A"
A Java string already represents its constant value. Resolution returns that same string.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("A".resolveConstantDesc(java.lang.invoke.MethodHandles.lookup()));
    }
}
Output
A

Syntax

Java syntax
public String resolveConstantDesc(MethodHandles.Lookup lookup)
JDK 21 method signatures (1)
public String resolveConstantDesc(MethodHandles.Lookup lookup)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Resolve a label from a JVM constants exercise

A JVM tool receives a descriptor for a constant string and has to resolve it in a lookup context. Follow the resolved value through the example to see what the tool can use after that step.

From the programString label = "Gate A"
ResultGate A
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String label = "Gate A";
        System.out.println(label.resolveConstantDesc(java.lang.invoke.MethodHandles.lookup()));
    }
}
Output
Gate A

Resolving this string descriptor gives its original text.

Advertisements

Check the identity of a resolved course code

A tool resolves a course-code constant that originated as a string. Inspect the resolved reference so the tool author can tell whether this particular resolution preserves the object's identity.

From the programString course = "CS101"
Resulttrue
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String course = "CS101";
        System.out.println(course.resolveConstantDesc(java.lang.invoke.MethodHandles.lookup()) == course);
    }
}
Output
true

String resolution returns the receiver itself.

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

String is its own resolved constant. Other descriptor types may perform lookups and have different failure modes.

Documentation for String.resolveConstantDesc