aspose file tools*
The moose likes Beginning Java and the fly likes Referencing to another object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Referencing to another object" Watch "Referencing to another object" New topic
Author

Referencing to another object

Di Fusio
Greenhorn

Joined: Apr 15, 2011
Posts: 7
Hey guys,

Sorry for the indescriptive title, couldn't think of a better one.

I'm currently learning from a book, "Learn Java 6 in 21 Days". I'm at the topic of object that reference to other objects instead of creating new ones.

Take the following code:


With the result being:

string1: Peanuts
string2: Free the bound periodicials.test
Same object? false
Same value? false


In the book they state that



Creates a reference rather then a new object. Therefore I would expect the output of str2 when it prints to be "Peanutstest". Here is why I am thinking this:



Since str2 references to str1 in the 3rd line above, I would expect str2 to change to "Peanuts" when I change str1.

Can anyone explain to me where I am going wrong in this way of thinking?

Thank you.

Nico Van Brandt
Ranch Hand

Joined: Mar 31, 2011
Posts: 66

Strings are immutable in Java. This means that the state cannot be modified.

Once you add a character, you create a new string object which will have another object reference.


Oracle Java SE6 Certified Programmer
Oracle Java EE5 Certified Web Component Developer
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14342
    
  22

This is about the difference between objects and variables.

In Java, variables (of non-primitive type) are references to objects. So, the variable is not the object itself, it's only a reference (something like a pointer, if you know C).

In line 3, you declare two variables str1 and str2. Note that you don't initialize them there - at that point, they're not referring to any String objects yet.

In line 4, you're making str1 refer to a String that contains "Free the bound periodicials.".

In line 5, you're making str2 refer to the same String object that str1 refers to. Note: Probably you're misunderstanding something here. You are not making the variable str2 a reference to the variable str1 - you're just making str2 refer to the same object that str1 refers to. There is no direct link between the variables str2 and str1.

In line 6, you make str1 refer to another String object that contains "Peanuts". Note that str2 doesn't know anything about this - it is still referring to the object that contains "Free the bound periodicials.". (There is no link between variables str2 and str1).

In line 7, you make str2 refer to a new String object that contains "Free the bound periodicials.test" (the contents of the object that str2 was referring to is concatenated with "test" into a new String object, and then str2 is assigned to refer to that new String object).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Di Fusio
Greenhorn

Joined: Apr 15, 2011
Posts: 7
Ahaaah!

So the "Free the bound periodicals" is the actual object.

That clarifies alot. Just one question springs to mind now. If the String is the actual object, then why can I do something like:



Nico Van Brandt
Ranch Hand

Joined: Mar 31, 2011
Posts: 66

I'm not quite following you.

Every String is an Object, so it may have methods.

You could also do like this:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14342
    
  22

Indeed, the important thing to understand is that the variables are not the actual objects, they're just references to objects (so, you can for example make two variables refer to the same object).

Looking at a line like this (I added a missing closing parenthesis):

You're calling the substring method on the String object that str1 refers to. That method will return a new String object that will contain part of the original String object. The new String object is then passed to the println() method. The println() method ofcourse has an argument variable; it's defined in class PrintStream (because System.out is a PrintStream):

So, when you make the call, x is made to refer to the String object, and println() will do what it does.
Di Fusio
Greenhorn

Joined: Apr 15, 2011
Posts: 7
Jesper knows what i'm getting at.

Thank you both for helping. This is resolved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Referencing to another object