Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By Junky

Thread: Reference in String

  1. #1
    hqt
    hqt is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Question Reference in String

    I am learning Java on Video of berkely University. In that video, they say that: given two string s1 and s2, if we write: s2=s1, s2 will point to what s1 point.
    so my code is:
    Java Code:
    String s1=new String("Java");
    String s2=new String();
    s2=s1;
    s2="forum";
    System.out.println(s1);
    So, if above statement true, Java will output "forum", but when I tested, Java output "Java". (mean s1 and s2 point to different thing)

    So,who can help me,please.
    thanks :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reference in String

    Yes when you do s2 = s1 then s2 will point to the same String object, but when you change what s2 points to, by say assigning it to a different String object, you shouldn't expect s1 to change with it, since it is still pointing to the original String object, the one with "Java" in it.
    hqt likes this.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Reference in String

    Quote Originally Posted by hqt View Post
    So, if above statement true, Java will output "forum"
    That might be true if pointers or pass-by-reference was involve. Java has neither.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reference in String

    We can debate this for years, but Java does use pointers, just not directly. But the key for original poster to understand all of this is to know that in Java Strings are immutable. Meaning although s2 refers to ("points" to) a different String object, the original String object doesn't change and in fact can't change because it's immutable, and that s1 points to that original String.

  5. #5
    hqt
    hqt is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Question Re: Reference in String

    Ah, I understand. So, Does above example base on rule: "In Java,strings are immutable" ?
    If that true: so I have this example :
    Java Code:
    s1="programmer";
    Java will created a new String name "programmer" and s1 will point to. And "Java" still allocate in memory, and garbage collector will delete later ? Does my suppose true, correct for me, please.

    thanks for help :)

    @: Oh, after I posted, I see that Fubarable have said about "in Java, Strings are immutable"
    Last edited by hqt; 11-21-2011 at 03:32 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reference in String

    Yes, you're right. There are some other unusual things about Strings that you might want to know. Java hangs on to Strings a bit longer and harder than other objects and it also holds Strings in a special spot in memory called the String-pool so that they can be easily re-used. So even if "Java" has no active references, it may be held by the JVM for a while in case it needs to be re-used.
    hqt likes this.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Reference in String

    Time to break out Fred and Barney.
    Java Code:
    String fred = new String("Java"); // write the word "Java" on a piece of paper, stick it to the wall and have Fred point at it
    String barney = fred; // Barney point at the same piece of paper that Fred is pointing at
    barney = "forum"; // write the word "forum" on another piece of paper, stick it to the wall and have Barney point at it.
    The last line in no way affects Fred and he remains pointing at the piece of paper with "Java" written on it.
    hqt likes this.

Similar Threads

  1. What to do with object reference?
    By kyle_maddisson in forum New To Java
    Replies: 6
    Last Post: 11-04-2011, 05:58 AM
  2. no array reference
    By droidus in forum New To Java
    Replies: 12
    Last Post: 10-05-2011, 05:05 AM
  3. first time GUI'r... trying to reference jpg
    By kleaverdevelopment in forum Java 2D
    Replies: 2
    Last Post: 04-08-2011, 05:33 PM
  4. object and reference
    By aizen92 in forum New To Java
    Replies: 11
    Last Post: 04-01-2011, 08:39 PM
  5. Getting the Object Reference Name
    By Deathmonger in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •