I have been reading a textbook on Java-- I am a C/C++ programmer so my view of addresses and pointers are based on what I know from those languages. I am trying to make sense of something the textbook said about object addresses.
The textbook basically said when one object points to another it points to the same address and becomes an alias. The old address pointed to is lost and is taken care of by the GC. That makes sense to me but I wrote a little test code to see how this goes:
import java.util.Scanner;
public class sscanner {
public static void main(String[] args) {
String message_one = "Hello!";
String message_two = "World!";
Scanner scan = new Scanner(System.in);
message_two = message_one; // an alias?
// shouldn't "world!" be lost but message_two
// should print the same as message_one?
message_one = scan.nextLine();
System.out.println("M1: " + message_one + "\nM2: " + message_two + "\n");
}
}
The output looks like:
This is a test sentence.
M1: This is a test sentence.
M2: Hello!
If message two truly pointed to the address of message one, wouldn't message two print the same as message one in the end?