Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 416,725 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,907 people online right now.Registration is fast and FREE... Join Now!



String clone Rate Topic: -----

#1 AbuJaFaR  Icon User is offline

  • D.I.C Regular
  • PipPipPip

Reputation: 11
  • View blog
  • Posts: 328
  • Joined: 13-December 07


Dream Kudos: 0

Share |

String clone

Posted 19 September 2008 - 02:14 PM

String s1 = "Hello";
String s2 = "Hello";
String s3 = new String(s1);
String s4 = s2.clone();



s1==s2 is true and s2.equals(s3) is true.

So I have some questions.Hear my thoughts first.
When we have 2 strings and we compare them with the '=' operator, we compare their addresses in the memory and not the values.When we use the equals method we compare the values.

Am I Correct?

If I am, s1 and s2 have the same addresses because we didnt use the new word that indicates new space in the memory.And s1-s2-s3 have the same value which is "Hello" but s3 has different address than s1-s2.

And my final question is when i use the clone method what is happening?
I mean s4 contains the "Hello" word?Or s4==s2?(if the address thing i said its true)
Was This Post Helpful? 0
  • +
  • -


#2 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 2130
  • View blog
  • Posts: 13,803
  • Joined: 06-March 08


Dream Kudos: 550

Re: String clone

Posted 19 September 2008 - 02:24 PM

View PostAbuJaFaR, on 19 Sep, 2008 - 03:14 PM, said:

String s1 = "Hello";
String s2 = "Hello";
String s3 = new String(s1);
String s4 = s2.clone();



s1==s2 is true and s2.equals(s3) is true.

So I have some questions.Hear my thoughts first.
When we have 2 strings and we compare them with the '=' operator, we compare their addresses in the memory and not the values.When we use the equals method we compare the values.

Am I Correct?


100% correct

Quote

If I am, s1 and s2 have the same addresses because we didnt use the new word that indicates new space in the memory.And s1-s2-s3 have the same value which is "Hello" but s3 has different address than s1-s2.


Not because you were not using the new word. Because the compiler is intelligent enough to do not allocate twice an area in memory for the constant "hello" twice. If you had get s2 from another class that returns "hello" they wouldn't be equals

s1 = "hello";
s2 = someObject.getString(); // that returns "hello"
s1 != s2

Quote

And my final question is when i use the clone method what is happening?
I mean s4 contains the "Hello" word?Or s4==s2?(if the address thing i said its true)


Don't sure at 100% (would be easy to test...) but as Strings are not mutable in Java no reason not to point to the same original one


Edited to split the different questions into different tags

This post has been edited by pbl: 19 September 2008 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 AbuJaFaR  Icon User is offline

  • D.I.C Regular
  • PipPipPip

Reputation: 11
  • View blog
  • Posts: 328
  • Joined: 13-December 07


Dream Kudos: 0

Re: String clone

Posted 19 September 2008 - 03:17 PM

em what?! :blink:
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • PipPipPipPipPip

Reputation: 255
  • View blog
  • Posts: 1,883
  • Joined: 23-November 07


Dream Kudos: 0

Re: String clone

Posted 19 September 2008 - 03:25 PM

look closely, pbl has answered your question in seperate areas, i think it was accidentally all put in the quote tags though.
Was This Post Helpful? 0
  • +
  • -

#5 AbuJaFaR  Icon User is offline

  • D.I.C Regular
  • PipPipPip

Reputation: 11
  • View blog
  • Posts: 328
  • Joined: 13-December 07


Dream Kudos: 0

Re: String clone

Posted 19 September 2008 - 03:51 PM

U said that im correct so that when we say s1==s2 then we compare their addresses.

then in my example i know that s1==s2 is true(that was the answer in the exam).isnt it correct?

And I didnt understand what clone() does :crazy:
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 2130
  • View blog
  • Posts: 13,803
  • Joined: 06-March 08


Dream Kudos: 550

Re: String clone

Posted 19 September 2008 - 03:54 PM

View PostAbuJaFaR, on 19 Sep, 2008 - 04:51 PM, said:

U said that im correct so that when we say s1==s2 then we compare their addresses.

then in my example i know that s1==s2 is true(that was the answer in the exam).isnt it correct?

And I didnt understand what clone() does :crazy:

I said that clone is probably pointing to the same String, no reason for it to do otherwise as String are not mutable in Java
but I won't bet on that one. Java engineering might have decided to do otherwise for a reason that does not come to my mind

"pointer" is a forbidden word in Java world. Actually Java only uses pointers but they are hidden to the user.

This post has been edited by pbl: 19 September 2008 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 AbuJaFaR  Icon User is offline

  • D.I.C Regular
  • PipPipPip

Reputation: 11
  • View blog
  • Posts: 328
  • Joined: 13-December 07


Dream Kudos: 0

Re: String clone

Posted 20 September 2008 - 05:00 PM

When u say pointing to the same string u mean that when i say

String s4=s2.clone();


That means:

s4==s2 OR s4.equals(s2) ?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 2130
  • View blog
  • Posts: 13,803
  • Joined: 06-March 08


Dream Kudos: 550

Re: String clone

Posted 20 September 2008 - 07:02 PM

View PostAbuJaFaR, on 20 Sep, 2008 - 06:00 PM, said:

When u say pointing to the same string u mean that when i say

String s4=s2.clone();


That means:

s4==s2 OR s4.equals(s2) ?

s4 == s2 same pointer to the same String in memory

Why philosophically argumenting ? Test it.

Don't see why

class String
{
	 Object clone() {
		 return this;
	 }
}



May be not... but the reason it wouldn't do that does not come to my mind
As String are not mutable why not ?

Funny

	public static void main(String[] arg) {

		String a = "abc";
		String b = (String) a.clone();
		System.out.println(a == b);
		
	}



does not compile.... method "clone is not visible"
don't ask me why

This post has been edited by pbl: 20 September 2008 - 07:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 eazyigz  Icon User is offline

  • New D.I.C Head
  • Icon

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-November 09


Dream Kudos: 25

Re: String clone

Posted 14 January 2010 - 02:47 PM

View Postpbl, on 20 Sep, 2008 - 07:02 PM, said:

Funny

	public static void main(String[] arg) {

		String a = "abc";
		String b = (String) a.clone();
		System.out.println(a == b);
		
	}



does not compile.... method "clone is not visible"
don't ask me why

The String class in Java does not expose a public clone() method. In fact, one must implement the Cloneable interface and override the clone() function in order to achieve the clone behavior.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 2130
  • View blog
  • Posts: 13,803
  • Joined: 06-March 08


Dream Kudos: 550

Re: String clone

Posted 14 January 2010 - 08:25 PM

View Posteazyigz, on 14 Jan, 2010 - 02:47 PM, said:

The String class in Java does not expose a public clone() method. In fact, one must implement the Cloneable interface and override the clone() function in order to achieve the clone behavior.

As String is final and you cannot extend it
I wonder how you would be able to overload it's clone() method :D
Not really a reason to wake up a year old topic
Was This Post Helpful? 1
  • +
  • -

#11 Simple_Condolences  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 10-January 10


Dream Kudos: 0

Re: String clone

Posted 15 January 2010 - 11:07 AM

View PostAbuJaFaR, on 19 Sep, 2008 - 03:51 PM, said:

then in my example i know that s1==s2 is true(that was the answer in the exam).isnt it correct?


Tell me you didn't do what I think you did... If you did, please don't anymore. This site is not for that. It is a study tool and is here to assist you in writing and fixing programs/code. Not as a test answer resource...

If you didn't use us to answer a question on your test, then ignore above statement and look back over PBL's post.

- Zach
Was This Post Helpful? 0
  • +
  • -

#12 eazyigz  Icon User is offline

  • New D.I.C Head
  • Icon

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-November 09


Dream Kudos: 25

Re: String clone

Posted 15 January 2010 - 12:02 PM

Quote

I wonder how you would be able to overload it's clone() method

I was stipulating about overriding the clone() method of the Object class, not overloading it. Now that I thought about it, why would one want to clone a String object in the first place? In Java, cloning performs a shallow copy (copies the reference of the original object along with its field values to the new object). In that case, why not intern the String - which essentially does the same thing.
Sorry for waking up this thread, but it came up #1 on Google's search results for "Java String clone". So if you have any further thoughts on this subject please let us know.

Thanks a lot,
Igor

This post has been edited by eazyigz: 15 January 2010 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#13 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • Icon

Reputation: 105
  • View blog
  • Posts: 670
  • Joined: 15-October 09


Dream Kudos: 300

Re: String clone

Posted 15 January 2010 - 12:13 PM

View Posteazyigz, on 15 Jan, 2010 - 07:02 PM, said:

Quote

I wonder how you would be able to overload it's clone() method

I was stipulating about overriding the clone() method of the Object class, not overloading it.


Yeah, that's what pbl meant by “you cannot subclass String anyway.”
Was This Post Helpful? 1
  • +
  • -

#14 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 2130
  • View blog
  • Posts: 13,803
  • Joined: 06-March 08


Dream Kudos: 550

Re: String clone

Posted 15 January 2010 - 06:48 PM

View Posteazyigz, on 15 Jan, 2010 - 12:02 PM, said:

Quote

I wonder how you would be able to overload it's clone() method

I was stipulating about overriding the clone() method of the Object class, not overloading it. Now that I thought about it, why would one want to clone a String object in the first place? In Java, cloning performs a shallow copy (copies the reference of the original object along with its field values to the new object). In that case, why not intern the String - which essentially does the same thing.
Sorry for waking up this thread, but it came up #1 on Google's search results for "Java String clone". So if you have any further thoughts on this subject please let us know.

Thanks a lot,
Igor

String str2 = new String(str1);
will do the job no need for a clone() method that you won't be able to implement anyhow
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users