• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Referencing to another object

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Di Fusio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper knows what i'm getting at.

Thank you both for helping. This is resolved.
reply
    Bookmark Topic Watch Topic
  • New Topic