Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String side effect & how to overcome

 
Nehul NN
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had notice string effect on thin client (set-top box) but how it could affect normal Java Application or Applet? I mean you define string like this
String s = "javaranch";
Just like to know..
Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overcome what? What's the problem?
 
Nehul NN
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem in application. But generally String object create problem in garbage collection & effect your app performance too. So just want to know is there any know pitfall of using String object in your application?
Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nehul,
Go find whoever's been telling you this nonsense, and mess up their hair and tell them they're a silly, silly person. Then stop listening to them.
String objects will in no way "cause problems." Relax, and be happy.
 
Nehul NN
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This nonsense stems from exsessive String creation in loops such as:

That would result in a new String being created for every result in the resultset.
The proper way:

Thats only for excessive concatenation in loops though. Some people go to the other extreme and use a StringBuffer for all appending - even in-line appending (where the post compiler adds the StringBuffer in for you)!
My 1/2 cent for what it's not worth.
Dana
 
Annekee Dufour
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the risk of sounding stupid: what is inline appending?
Do you mean something like the following?
System.out.println("foo" + "bar");
When I looked at the decompiled code using decafe,
I saw no StringBuffer...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System.out.println("foo" + "bar");
When I looked at the decompiled code using decafe, I saw no StringBuffer...

In this particular line of code, the string concatenation can be done by the compiler rather than at runtime; code for System.out.println("foobar") gets emitted.
 
Annekee Dufour
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right, wrong example. But here's my code:
public class TestClass {
public static void main(String[] args){
String test = "yoho!";
System.out.println("foo" + test + "bar" + args[0]);
}
}
and here is what decafe makes of it
public class TestClass {
public TestClass(){
}
public static void main(String[] args){
String test = "yoho!";
System.out.println("foo" + test + "bar" + args[0]);
}
}
Why not the following?
System.out.println(new StringBuffer("foo").append(test).append("bar").append(args[0]).toString());
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because it knows that the former is easier to read to you.
 
Annekee Dufour
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O, yes, it is much easier to read.
But now I still don't understand when Strings get replaced by StringBuffers! Can you give me a code example?
Also, should I understand from your response that, when such a replacement occurs, I cannot see it using decafe because decafe changes the stringbuffers back to strings to make it more readable? Or perhaps because the replacement takes place runtime?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Sun compiler (and any other compiler I am aware of) compiles
"Foo" + bar + "something"
to *exactly the same* byte code as
new StringBuffer().append("Foo").append(bar).append("something").toString()
So decafe cannot possibly know which of the above you really used. It just guesses that it was the former.
[ November 26, 2003: Message edited by: Ilja Preuss ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic