As I am studying java, I have learned that the proper way to compare 2 Strings is to use equals and not \"==\". This line
static String s1 = \"a\"; static String s2
You aren't missing anything. The Unsafe library is reporting what is actually happening.
Bytecode:
static {};
Code:
0: ldc #11; //String a
2: putstatic #13; //Field s1:Ljava/lang/String;
5: ldc #11; //String a
7: putstatic #15; //Field s2:Ljava/lang/String;
10: return
Notice both Strings are put in different locations in memory, 13 and 15.
There is a difference between where the the variables are stored in memory, which needs a separate address, and whether a new Object is put on the heap. In this case, it assigns two separate addresses for two variables, but it does not need to create a new String Object as it recognizes the same String literal. So both variables reference the same String at this point.
If you want to get the Adress, you can use the answer found in this question,How can I get the memory location of a object in java?. Make sure you read the caveats before using, but I did a quick test and it seems to work.