Results 1 to 7 of 7
Thread: Reference in String
- 11-21-2011, 03:12 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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:
So, if above statement true, Java will output "forum", but when I tested, Java output "Java". (mean s1 and s2 point to different thing)Java Code:String s1=new String("Java"); String s2=new String(); s2=s1; s2="forum"; System.out.println(s1);
So,who can help me,please.
thanks :)
-
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.
- 11-21-2011, 03:21 AM #3
-
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.
- 11-21-2011, 03:29 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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 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.Java Code:s1="programmer";
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.
-
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.
- 11-21-2011, 03:33 AM #7
Re: Reference in String
Time to break out Fred and Barney.
The last line in no way affects Fred and he remains pointing at the piece of paper with "Java" written on it.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.
Similar Threads
-
What to do with object reference?
By kyle_maddisson in forum New To JavaReplies: 6Last Post: 11-04-2011, 05:58 AM -
no array reference
By droidus in forum New To JavaReplies: 12Last Post: 10-05-2011, 05:05 AM -
first time GUI'r... trying to reference jpg
By kleaverdevelopment in forum Java 2DReplies: 2Last Post: 04-08-2011, 05:33 PM -
object and reference
By aizen92 in forum New To JavaReplies: 11Last Post: 04-01-2011, 08:39 PM -
Getting the Object Reference Name
By Deathmonger in forum New To JavaReplies: 2Last Post: 03-12-2008, 02:51 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks