Why two different String
variables point to the same object if they are initialized by the same value, but they point to different object if the string is passed through an Intent
?
For example:
public class MainActivity extends Activity {
public static String textA;
protected void onCreate(Bundle savedInstanceState) {
MainActivity.textA = "abc";
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("extra", MainActivity.textA);
startActivity(intent);
}
}
public class MainActivity2 extends Activity {
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String textB = intent.getStringExtra("extra");
System.out.println(textB == MainActivity.textA);
String textC = "abc";
System.out.println(textC == MainActivity.textA);
}
}
The output is
false
true
My question is why textC
and MainActivity.textA
point to the same "abc"
but textB
points to a separate "abc"
?