aspose file tools*
The moose likes Performance and the fly likes String side effect & how to overcome Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "String side effect & how to overcome" Watch "String side effect & how to overcome" New topic
Author

String side effect & how to overcome

Nehul NN
Ranch Hand

Joined: Nov 05, 2003
Posts: 45
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

Joined: Jan 30, 2000
Posts: 18671
Overcome what? What's the problem?


"I'm not back." - Bill Harding, Twister
Nehul NN
Ranch Hand

Joined: Nov 05, 2003
Posts: 45
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Nehul NN
Ranch Hand

Joined: Nov 05, 2003
Posts: 45
Thanks for your reply.
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
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

Joined: Nov 04, 2003
Posts: 41
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

Joined: Jul 08, 2003
Posts: 24166
    
  30


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

Joined: Nov 04, 2003
Posts: 41
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

Joined: Jul 11, 2001
Posts: 14112
Probably because it knows that the former is easier to read to you.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Annekee Dufour
Ranch Hand

Joined: Nov 04, 2003
Posts: 41
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

Joined: Jul 11, 2001
Posts: 14112
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String side effect & how to overcome
 
Similar Threads
jButton fill colour
applets image graphics
how to achieve the "like" effect of SQL in java
How to merge row cells in JTable
Watercolor pencil