|
-
May 12th, 2005, 07:52 PM
#1
Thread Starter
Frenzied Member
Which is worse?
Ok, I'm working with some Strings, and these Strings will get remade up to an infinite number of times, and I know that a new object is created each time.
So, which is worse..
Keep using the String, and have the value continously change, or
Use a StringBuffer, and continously delete the current text so that the upcoming read text can be the only thing in the StringBuffer.
I hope that makes sense!
-
May 12th, 2005, 09:14 PM
#2
Junior Member
Re: Which is worse?
Hi I currently have a java course and I'd say use the StringBuffer... but the garbage collector should delete unused objets if you use String so it could be interesting to test both way to do it and choose the best result.
-
May 13th, 2005, 07:33 PM
#3
Thread Starter
Frenzied Member
Re: Which is worse?
Yeah, thanks. I'm using it to access network data one line at a time, so I think StringBuffer is the way to go.
-
May 15th, 2005, 02:02 PM
#4
Dazed Member
Re: Which is worse?
Java treats string of the same content to be of the same ref. So in essence i guess they share the same memory location. Since you said your Strings are going to constantly change you will be in effect creating a new object for each new String. As lemenz70 pointed out the Garbage Collector will clean up any unused objects but you will probably get a performance hit though. You are better off just using a StringBuffer and set it's length to zero when you want to clear it out.
-
May 15th, 2005, 02:21 PM
#5
Dazed Member
Re: Which is worse?
Code:
public class SBTest{
public static void main(String[] args){
StringBuilder sb = new StringBuilder("Sun Powered");
System.out.println(sb.toString());
sb.setLength(0);
sb.append("Java Powered");
System.out.println(sb.toString());
sb.setLength(0);
sb.append("System_Error Powered");
System.out.println(sb.toString());
}
}
-
May 15th, 2005, 05:33 PM
#6
Thread Starter
Frenzied Member
Re: Which is worse?
Yeah, thanks Dillenger. That seems to be the most logical thing to do.
-
May 16th, 2005, 07:30 PM
#7
Junior Member
Re: Which is worse?
 Originally Posted by Dilenger4
Java treats string of the same content to be of the same ref.
False
make a test program and do this
Code:
String lsString1 = new String ("Hi");
String lsString2 = new String ("Hi");
if (lsString1 == lsString2)
{
JOptionPane.showMessageDialog(null, "lsString1 == lsString2");
}
if (lsString1.compareTo(lsString2) == 0)
{
JOptionPane.showMessageDialog(null, "lsString1 compare to lsString2 = 0");
}
result: only the MessageBox called by the compareTo will be displayed because lsString1 == lsString2 is false. The object reference is compared and it is not the same even if lsString1 is the same text as lsString2. By using compareTo, it compares the text itself and not the reference.
(I learned that in my java class)
that's why I said use the StringBuffer or there will be too much unuseful String objects.
I hope didn't make that much mistakes!
-
May 16th, 2005, 09:09 PM
#8
Dazed Member
Re: Which is worse?
No True. You are using String Objects. I was talking about String literals.
Code:
public class RefTest{
public static void main(String[] args){
String s1 = "java";
String s2 = "java"; // same refs
if(s1 == s2){
System.out.println("Same java refs");
}
String s3 = new String("sun");
String s4 = new String("sun"); //! same refs
if(s3 == s4){
System.out.println("Same sun refs");
}
}
}
-
May 17th, 2005, 12:08 AM
#9
Fanatic Member
Re: Which is worse?
... and of course, Objects can equalize by .equals(Object) method.
-
May 21st, 2005, 08:33 AM
#10
Re: Which is worse?
And of course, there's String.intern()...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|