STRING INSTANCE METHODS

Java String resolveConstantDesc() method

resolveConstantDesc() obtains the value represented by a constant descriptor. For a String, the descriptor is the string itself, so the method returns the same object. The method belongs to the constant-descriptor API and is available since Java 12.

Resolving a String constant

import java.lang.invoke.MethodHandles;

String tekstas = "JavaQuest";

String rezultatas = tekstas.resolveConstantDesc(MethodHandles.lookup());

System.out.println(rezultatas);
Console output

JavaQuest

  1. The import makes the class MethodHandles available so we can obtain the argument value.
  2. The variable tekstas stores the text JavaQuest.
  3. MethodHandles.lookup() supplies a context object used in resolving constants and other JVM entities. This argument does not supply the string being resolved.
  4. tekstas.resolveConstantDesc(...) returns the same string. We store the value in rezultatas and print it.

The result looks simple because a string already serves as its own constant descriptor. The method does not search for words or transform characters.

Why is the method called on a String object?

resolveConstantDesc() is an instance method: the descriptor being resolved is the receiver itself. We therefore call it on a specific String instead of supplying the text as an extra argument.

String rezultatas = tekstas.resolveConstantDesc(MethodHandles.lookup());

A static call such as String.resolveConstantDesc(...) would be invalid. The argument in parentheses supplies the resolution context rather than the string to return.

Method syntax

public String resolveConstantDesc(MethodHandles.Lookup lookup)

Parts of the resolveConstantDesc() method
PartMeaning
ReceiverThe specific String object whose value is being resolved.
lookupMethodHandles.Lookup argument. In the general API it supplies resolution and access-control context; this String implementation ignores it.
Return typeString – the receiver itself.
InterfaceThe method implements ConstantDesc.resolveConstantDesc(...).
Java versionThe method is available since Java 12.

The concrete String method does not declare a checked exception. Below, we will see why calling through a general ConstantDesc reference has different compile-time requirements.

What is a constant descriptor?

A constant descriptor is a standardized way to describe a value usable by the Java class-file constant system. Descriptors are used by the JVM and tools that work with bytecode. They are not human-written explanations or documentation.

Roles of Constable, ConstantDesc, and String
TypePurpose
ConstableAn object that can supply a constant descriptor for its value through describeConstable().
ConstantDescThe constant descriptor itself. Its method resolveConstantDesc(...) obtains the described value.
StringImplements both interfaces: a string can supply its descriptor and can also be that descriptor.

Other descriptors may need to locate a class or a method. For a String, no additional lookup is needed: the value is already the string object itself.

The result is the same object, not a copy

import java.lang.invoke.MethodHandles;

String tekstas = new String("Java");
String rezultatas = tekstas.resolveConstantDesc(MethodHandles.lookup());

System.out.println(tekstas == rezultatas);
System.out.println(tekstas.equals(rezultatas));
Console output

true
true

  1. new String("Java") creates a distinct string object, letting us check which reference is returned.
  2. The comparison tekstas == rezultatas checks whether both references point to the same object. It is true here because the method returns the receiver.
  3. equals() compares the text content, which also matches. This is the method to use for ordinary string-content comparisons.

The method does not modify the original text, turn a variable into a constant, or create a new string copy.

What is the lookup argument for?

import java.lang.invoke.MethodHandles;

String tekstas = "JavaQuest";
MethodHandles.Lookup kontekstas = MethodHandles.lookup();

System.out.println(tekstas.resolveConstantDesc(kontekstas));
System.out.println(tekstas.resolveConstantDesc(MethodHandles.publicLookup()));
Console output

JavaQuest
JavaQuest

MethodHandles.Lookup provides resolution and access context for JVM entities. MethodHandles.lookup() supplies the calling class’s context, while MethodHandles.publicLookup() supplies a public-access context.

This String implementation does not use the argument, so both calls return the same string. We still supply a real lookup object because other constant descriptors may depend on that context.

The name lookup here does not mean a text search. The argument is not a word, an index, or a regex.

Relationship to describeConstable()

import java.lang.invoke.MethodHandles;
import java.util.Optional;

String tekstas = "JavaQuest";
Optional<String> apvalkalas = tekstas.describeConstable();
String aprasymas = apvalkalas.orElseThrow();
String rezultatas = aprasymas.resolveConstantDesc(MethodHandles.lookup());

System.out.println(apvalkalas.isPresent());
System.out.println(rezultatas);
System.out.println(rezultatas == tekstas);
Console output

true
JavaQuest
true

  1. describeConstable() returns an Optional<String> container holding the string’s constant descriptor.
  2. orElseThrow() retrieves the descriptor. A non-null string has a descriptor even when its content is empty.
  3. resolveConstantDesc(...) returns the described value directly as a String, without an additional container.
  4. Because the string describes itself, the same object is preserved throughout this sequence.

The interface Constable provides the operation for obtaining a descriptor. Resolving that descriptor belongs to the ConstantDesc interface. Their similar names do not mean they expose the same methods.

describeConstable()

It also works with strings built at runtime

import java.lang.invoke.MethodHandles;

String kalba = "Java";
int versija = 21;
String tekstas = kalba + " " + versija;

String rezultatas = tekstas.resolveConstantDesc(MethodHandles.lookup());

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

Java 21
true

The string does not need a static final declaration. A value assembled at runtime can still describe itself as a String constant. This API terminology does not prevent the variable from being assigned another value later.

The call does not format the string or parse its number. The result remains Java 21, including its space.

An empty string is also a valid value

import java.lang.invoke.MethodHandles;

String tekstas = "";
String rezultatas = tekstas.resolveConstantDesc(MethodHandles.lookup());

System.out.println("[" + rezultatas + "]");
System.out.println(rezultatas.length());
System.out.println(rezultatas == tekstas);
Console output

[]
0
true

An empty string is a real String object. It is returned just like a nonempty string. The brackets make its empty content visible, and its length is 0.

Practical example: processing several constant descriptors

import java.lang.constant.ConstantDesc;
import java.lang.invoke.MethodHandles;

public class ConstantExamples {
  public static void main(String[] args) throws ReflectiveOperationException {
    ConstantDesc[] aprasymai = {"JavaQuest", Integer.valueOf(21)};
    MethodHandles.Lookup kontekstas = MethodHandles.lookup();

    for (ConstantDesc aprasymas : aprasymai) {
      Object rezultatas = aprasymas.resolveConstantDesc(kontekstas);
      System.out.println(rezultatas.getClass().getSimpleName() + ": " + rezultatas);
    }
  }
}
Console output

String: JavaQuest
Integer: 21

  1. The array ConstantDesc[] holds two descriptors: a string and an integer object. Both types can serve as descriptors for their own values.
  2. The for loop resolves each descriptor with the same operation. This shared code does not need a separate path for strings.
  3. Through a ConstantDesc reference, the declared return type is Object. The actual result type depends on the descriptor, so the program prints the names String and Integer.
  4. The general interface declares ReflectiveOperationException. In this complete example, throws ReflectiveOperationException allows such an exception to propagate to the caller.
  5. A direct call on a String does not declare this checked exception. The compiler follows the reference’s declared type and its method signature.

This is a small model of a tool that handles different constant descriptors uniformly. Other descriptors may require class or method resolution and may fail; the string case performs no such lookup.

Differences from describeConstable(), toString(), and intern()

import java.lang.invoke.MethodHandles;

String tekstas = new String("JavaQuest");
String rezultatas = tekstas.resolveConstantDesc(MethodHandles.lookup());

System.out.println(rezultatas == tekstas);
System.out.println(tekstas.toString() == tekstas);
System.out.println(rezultatas == tekstas.intern());
Console output

true
true
false

The example deliberately creates a distinct object with new String(...). The first two calls return that object’s reference, while intern() returns the pool’s reference to equal text. The final reference comparison in this example is therefore false.

Purposes of related String methods
MethodPurpose
describeConstable()Returns an Optional<String> containing the string’s nominal descriptor.
resolveConstantDesc(...)Resolves the described value; for a String, returns the receiver itself.
toString()Produces a string representation; for a String, also returns the receiver itself.
intern()Returns the canonical string of equal content from the string pool. That can be a different object reference.

Although some calls return the same reference, their purposes differ. Resolving a constant descriptor is not string interning.

describeConstable() · intern()

The method is available since Java 12

resolveConstantDesc(), ConstantDesc, and the related constant-descriptor API were introduced in Java 12. Compiling against Java 11 or an earlier API level does not make this method available.

Check the project JDK and its compiler --release setting. A newer runtime alone is insufficient if compilation targets an older API.

The method cannot be called on null

import java.lang.invoke.MethodHandles;

String tekstas = null;

System.out.println(tekstas.resolveConstantDesc(MethodHandles.lookup()));
Result

NullPointerException

The reference null points to no string object. The method call cannot be performed and throws NullPointerException. This differs from the empty string shown earlier.

This error concerns the receiver. It does not mean that the String implementation uses its argument for access checks.

Common mistakes

  • Assuming resolveConstantDesc() creates new text or changes its characters.
  • Expecting an Optional<String> even though this method returns the string directly.
  • Calling the method through a Constable reference even though it belongs to the ConstantDesc interface.
  • Treating a call through ConstantDesc as identical at compile time to a call through String, and overlooking the interface’s checked exception.
  • Interpreting the name as a human-readable description or requiring a static final declaration.

When should you use resolveConstantDesc()?

Use resolveConstantDesc() when working with the general constant-descriptor API: for a library or tool that processes constants, class files, or bytecode. It allows strings to participate in the same mechanism as other constant descriptors.

For everyday text formatting or printing, this method is generally unnecessary. The key point is that, for a string, the described value is already the String object itself.