Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

How does Java initialize String literal

Javadoc said that:

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

We know that String class has two properties namely: value[] and hash, and String literal are stored in a String pool.

But I am not able to figure out how that String literal is initialized before putting to that pool. As if I debug the string literal later, I can see the value[] and hash is somehow populated.

Does JVM invoke a special instruction?

Upvotes: 1

Views: 1919

Answers (3)

apangin
apangin

Reputation: 98334

JVM creates a new string literal object during constant pool resolution, if the same string has not been put to the string table before by String.intern call.

It is not specified how JVM creates and initializes such strings, so JVM may do whatever it wants as long as the result object is a regular java.lang.String instance that can be accessed from application code.

As to HotSpot JVM, it does not call any of String constructors, it just allocates a new object in Heap and fills in the fields in C++ code, see java_lang_String::basic_create:

Handle java_lang_String::basic_create(int length, TRAPS) {
  assert(initialized, "Must be initialized");
  // Create the String object first, so there's a chance that the String
  // and the char array it points to end up in the same cache line.
  oop obj;
  obj = InstanceKlass::cast(SystemDictionary::String_klass())->allocate_instance(CHECK_NH);

  // Create the char array.  The String object must be handlized here
  // because GC can happen as a result of the allocation attempt.
  Handle h_obj(THREAD, obj);
  typeArrayOop buffer;
    buffer = oopFactory::new_charArray(length, CHECK_NH);

  // Point the String at the char array
  obj = h_obj();
  set_value(obj, buffer);
  // No need to zero the offset, allocation zero'ed the entire String object
  assert(offset(obj) == 0, "initial String offset should be zero");
//set_offset(obj, 0);
  set_count(obj, length);

  return h_obj;
}

hash field of such new object is initialized to zero. The right hash code will be calculated on the first call to String.hashCode.

Upvotes: 5

lupchiazoem
lupchiazoem

Reputation: 8086

Follow breakpoint on public String(char value[]) of String class.

Edit: Whoever downvoted this:

Firstly, did you try as per answer? I had tried and it stops exactly in that method. I had tried with two different IDEs. Secondly, value member(as pointed in OP) is assigned in that method. If you indeed tried, you would have got that. Thirdly, hash member(as pointed in OP) is assigned a value on the call to hashCode() and hash member is assigned a value in it.

Clearly, you haven't tried. If you did, investigation on breakpoint would have led you to answers in OP:

1) how that String literal is initialized

and

2) ...somehow populated

.

Upvotes: -2

Stephen C
Stephen C

Reputation: 718826

Does JVM invoke a special instruction?

No. There is no "special" JVM instruction to do that.

The classloading infrastructure will most likely be creating the String objects that correspond to the literals using one of the String constructors; e.g. String(char[]) or String(byte[]). It will be getting the characters or bytes from the "constant pool" area in the ".class" file.

Upvotes: 0

Related Questions