Other answers are pretty good. Some specific points:
I asked this to my teacher and he replied that it is because what is in quotes is equivalent to an address, but he is not sure. Can you confirm?
Yes. He is right. As others have pointed out, a string literal, i.e. a valid Unicode character sequence enclosed inside a pair of "s is recognized by the Java Language Specification. When the Java compiler (the javac
program) encounters such a literal (e.g. "Hello"
) in your Java source file, it takes it literally and places it in a specific section inside the compiled class file. This section is called the runtime constant pool. It then creates a particular instruction called ldc
that it places in the class file where the compiled code for your initialization is stored. For instance, the body of the main
method in the following Java source code:
public class StringAsPrimitive {
public static void main(String[] args) {
String s = "Hello";
}
}
gets compiled to:
0: ldc #2 // String Hello
2: astore_1
3: return
(There is no magic here. Just compile your code using javac
program and view the class file using the javap
program. You can even see the contents of the .class file using a hex viewer and you will see the String Hello
literally).
This code is then interpreted by the JVM when you ask the java
program to run this class file.
The ldc
instruction to the JVM is documented here. This is what the JVM then does when it interprets #2
which is a reference to the actual location where the literal string "Hello"
is stored in the computer's memory (RAM):
Otherwise, if the run-time constant pool entry is a reference to an
instance of class String representing a string literal (§5.1), then a
reference to that instance, value, is pushed onto the operand stack.
So, what your teacher said turns out to be right, although we don't say it quite that way. In Java, there are two kinds of types: primitive and reference. So, it would be more appropriate to say that the value of the String literal is a reference (and not address).
Is "Hello" stored in an instance variable of the class String?
No. The String variable initialized to a String literal could be anything, a static
or class variable, an instance variable or a local variable (my code above). It is stored in the class file: StringAsPrimitive.class
and later it is available to the JVM in a particular memory location. This happens when the JVM loads the class.