Jump to content
  • Advertisement
Sign in to follow this  
Crazyfool

[java] Java Strings

This topic is 3181 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I tried some quick searches, but I want to know why the Java String is so odd. Correct me if I am wrong, but I think it makes an entirely new string anytime you modify it, which makes String handling extremely time and memory consuming. They have great classes like StringBuffer which seem to fix this.. so my question is, why did they make the String like this as opposed to like the StringBuffer class?

Share this post


Link to post
Share on other sites
Advertisement
Immutable objects can be shared by many clients without having to worry about side effects. C# does the exact same thing.

Share this post


Link to post
Share on other sites
String are non mutable so you can give out private strings without accidentally overwriting a private member.

EDIT:

beaten to it but yea, to keep private members private you either have to have them non mutable or clone them before you give them out, usually the later is more wasteful of resources

EDIT2:

On a side note the b2Vec2 class in Box2DFlashAS3 totally fails at this,

var vec:b2Vec2 = _body.GetLinearVelocity();
vec.x *= .3;



will actually change the velocity of the physics body

Share this post


Link to post
Share on other sites
Quote:
Original post by Crazyfool
...which makes String handling extremely time and memory consuming...


Have you actually benchmarked an application to determine if its extremely time or memory consuming?

There is a bunch of information available on the internet about the advantages and disadvantages of mutable and immutable objects. And there are also a bunch of benchmarks on when its more of an advantage to concat immutable strings or to append a mutable string.

Share this post


Link to post
Share on other sites
Quote:
Original post by kryat
Quote:
Original post by Crazyfool
...which makes String handling extremely time and memory consuming...


Have you actually benchmarked an application to determine if its extremely time or memory consuming?

There is a bunch of information available on the internet about the advantages and disadvantages of mutable and immutable objects. And there are also a bunch of benchmarks on when its more of an advantage to concat immutable strings or to append a mutable string.


Benchmark? Not really. But I have ran into heap issues (without increasing heapsize) until optimizing, including using stringbuffers when I can. I also found a performance increase in time, but that could have been the memory issue. As a side point - I am not an expert with Java. I only know what I know from experience working on school assignments, and this was just a recent issue that came up for me.

Share this post


Link to post
Share on other sites
I didnt know StringBuilder was better/different, thanks! As I previously mentioned, it was for school (which I completed the assignment already), so performance isnt top priority, however when it throws an out of memory exception, its kind of hard to demonstrate correctness :) I also have to give a demo in person, and the only portable computer is a netbook with a tiny processor and a tiny amount of RAM, so it cant be too slow.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Immutable objects can be shared by many clients without having to worry about side effects. C# does the exact same thing.
I admit I'm not yet sold on this rationale. I can see the benefits for sure, but shouldn't this be the goal of const or final?

I also find Java Strings a bit odd. Especially because they have been granted the right to have their own operators, resulting in different behaviour from the standard copy-by-reference one would expect when calling operator= on them.
Sure, operator+ comes extremely handy, yet strings are a big language exception to me...

Share this post


Link to post
Share on other sites
Quote:
Original post by Krohm
different behaviour from the standard copy-by-reference one would expect when calling operator= on them.

I don't understand what you are talking about here. Could you elaborate? Maybe with a few lines of code that demonstrates what you would expect (and which does not happen)?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!