5

Little question about the output generated from the javap command regarding the constant pool. When javap prints the pool it defines string constants as Asciz strings, which I understand means null terminated Ascii:

const #20 = Asciz       hello world;

This would imply that the length of the string is not known, and to parse you would read each byte until you encounter the null.

However, the length of constant pool string constants is defined by the two bytes preceding the string and there is no null appended. (Constant pool specification).

Does javap define strings as Asciz incorrectly or does Asciz have another meaning I'm not aware of?

CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
Jivings
  • 22,834
  • 6
  • 60
  • 101

2 Answers2

4

See bug #6868539. It's fixed in OpenJDK7, now javap prints Utf8 instead.

axtavt
  • 239,438
  • 41
  • 511
  • 482
0

The const #20 is not the actual string, but rather the UTF8 defined characters used for your string. You probably have another constant which is a string that references the constant #20. The asciz constants are used for other things than strings such as field names etc. The actual info contains the UTF8 tag, the length and the bytes.

See http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
  • 2
    This is what I described in my short definition. You actually linked to the same reference as I did. The fact that it defines a UTF8 string, and not an ASCIZ string is what I was referring to. – Jivings Nov 20 '11 at 11:56
  • Since there was no reference of null terminated ascii strings in the class file format specification I thought you meant how Strings were defined in the pool. (Even in the the 1.0 specification there are no ascii strings, but rather Unicode. None of them are null terminated). Jivings seemed to understand your question better than me. – Roger Lindsjö Nov 20 '11 at 15:53
  • Simply, what I wanted to know is why they are named Asciz in the constant pool when they are clearly not null terminated. – Jivings Nov 22 '11 at 12:36