question 1:
to question 4, first of all, you need to understand that literals
are actually constants. For example, "abc"
above you, literals will be stored in the static constant pool of class
files at compile time, and will be stored in the runtime constant pool at run time. In short, there is a constant pool at last, so all the "abc"
above you are stored in the constant pool. And only one will be saved (the same string will not appear in the constant pool). Like this sentence String S1 = "abc"; String S2 = "abc";
, there is a "abc" string in the constant pool, and there are two references to it in the stack, so S1 = = S2
. Like String S1 = new String ("abc"); String S2 = new String ("abc");
, when you see new
, you know that a String object
is created in the heap, and then private final char value [] in the object. The
field points to the value
field of the string in the constant pool, and then there is a reference s
in the stack pointing to the String object
. When you new
two objects, the references in the site point to different objects, of course, but the value
fields of both objects point to the same content.
question 6:
reference is actually a piece of binary data in the stack. When you look at this data at run time, you don't know what type it is, maybe it's a int
, maybe it's a char
, but in the compilation phase, you can infer what type it is according to the context, such as String S1
, the compiler can infer that this S1
is the String
type. The compiler prompts for an error message.
question 5:
what is saved is the String
object, because the Class
of the object can be found through the s
reference (there is an object header). If there is only a character array in the runtime constant pool, then this s
cannot call any method.
question 7:
these two statements are compiled with iconst_1 istore_1 iload_1 istore_2
, which means assigning two slot
, and then initializing a value for slot
.
happened to write an article about string's https://codeshelper.com/a/11.
1:jvm virtual machine stack creates references, heap memory creates strings, and constant strings are created in the constant pool. The
2:jvm virtual machine stack creates references, heap memory creates strings, and constant strings are created in the constant pool.
the first reference points directly to the constant pool, and the second reference points to the string in the heap memory.
3: equal, because they all point to strings in the constant pool, and each string in the constant pool has only one string, so they point to the same one.
4: not equal, because they each apply for a piece of space in heap memory, and strings are stored in this space. Although the contents of the storage are equal, they point to different heap memory, so they are not equal.
5: no, constants or references are stored, and the String object is in heap memory. Why it might still be a reference, you have to check the intern () method of the String object, which is difficult to explain here.
6: yes, but the reference to String is a reference type rather than a String type.
7: variables are created on the jvm virtual machine stack.