• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String side effect & how to overcome

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overcome what? What's the problem?
 
Nehul NN
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Nothing? Or something? Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic