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?

7 Years
Discussion Span
Last Post by \007

Java variables come in 2 flavours, primitives (eg int, char, byte, float, boolean - all lower case names) and reference variables (which you can think of as pointers).
All variables of array or Object types are reference variables. So, in your code message_one and message_two are reference variables of type String, or string pointers. The memory for those pointers is part of sscanner's memory. Initially they are both null (refer to nothing).
"Hello!" and "World!" are two new instances of the String class whose memory is allocated in the heap. Your two variables are then set to refer to these two instances. ie
message_one -> "Hello!"
message_two -> "World!"

You then execute
message_two = message_one;
which copies the value (the reference or pointer value) of message_one to message_two. After which
message_one -> "Hello!"
message_two -> "Hello!"

There are now no remaining references to the "World!" object, so it will be garbage collected and its memory released.
Finally you execute
message_one = scan.nextLine();
scan.nextLine() creates a new String object containing the user input, and message_one's value (the reference or pointer value) is set to refer to that new String, so you now have
message_one -> "This is a test sentence."
message_two -> "Hello!"

Votes + Comments

Ahh, I see how it goes. The textbook doesn't make it sound like that.

The textbook speaks in terms of address of the objects so I took it as if message_two pointed to message_one which points to a string.

message_two -> message_one -> "User test string."

So I took it as if changing what message_one points two would 'automatically' make message_two point to it as well. (Similar to how C/C++ works)


Edited by \007: n/a

String message_one = "Hello!";
String message_two = "World!";

message_one points to a object having a value "Hello!"
message_two points to a object having a value "World!"

So value of message_one is "Hello!" and value of message_two is "World!"

message_two = message_one;

message_two points to the object which message_one points and the old object having the value "World!" is disconnected from message_two and the object haveing value "Hello!" is connected to message_two.

Now the value of message_one and message_two both are "Hello!"

message_one = scan.nextLine();

Now a new object is assigned to message_one.

Accordeing to your information the value of the new object is

This is a test sentence.

So Now message_one points to the new object having the value This is a test sentence. and was disconnected from the object having value "Hello!". And the message_two is unchanged so it remains point to the same object which is having the value "Hello!"

Now the value of message_one is "This is a test sentence".
And the value of message_two is "Hello!"

Is it clear now?

If you need any clarrification reply.
else if you got the solution mark this post as solved.

PS. Sorry. I have not seen there is a reply to this post and replied.

Edited by sbhavan: Adding PS because my mistake I have replied. It may be a duplicate post/reply.


Thanks for your reply as well-- it makes sense to me.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.