• 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

Help with returning one string from three methods of type string?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone!

I have three methods that get the Title, the Firstname and the Surname here are the methods:


My Question is how can I create a method that can combine the above three get method and return a string as in one line,

so have something like getFullName() that returns the title firstname and the surname.

Thanks for all the Help

Zub
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your getFullName() method could call each of the other three methods, and can simply concatenate the results. then, return that concatenated string...

Note that this is not the ideal solution, but for something simple like this, it's probably ok.
 
Zubi Pen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
right.. thanks for your reply.. what would be an ideal solution for something like this for future reference
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Ideal solution will be using a StringBuffer to concatenate those string and return , because by concatenating String we end up in creating lot of new objects ,since string are immutable . Hope this helps
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"ideal" was probably not the best choice of words by me. One thing you'll learn is that there is never and ideal solution that applies to all possible scenarios. Every solution has tradeoffs that have to be weighed and balanced. For example, there are some situations where a bubble sort, which can be horribly slow, is actually the best solution, where a quick-sort, which is usually quite fast, would be a wretched choice.

So, without know the specifics of what you want to do, you can't determine the best.

Having said all that, any time you concatenate Strings, you may want to think about what you are doing. any time you see

String finalString = string1 + string2 + string3 + string4...

your going to be creating all kinds of unnecesary String objects you are not aware of - there's no avoiding this. Using a StringBuffer for the concatenation then converting that to a String would probably be better.

But, for a beginner, the added complexity of using a StringBuffer may not be worth it. Or, if the concatenation will only ever happen once in your program, it may not matter.

Software design is all about tradeoffs, and analysis of the specific situation for what will most likely happen and what COULD happen.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was going to post something with string + string but I knew someone would come in and say it's not optimal, etc..

Do you also use just one square of toilet paper?

This would fall into the lines of premature optimization - especially considering the IBM 1.5 compiler will do it automatically anytime you have string + string.

Anyway, write code that is easy to understand, and unless it's obviously slow, or you know a more readable and faster solution, go with something that makes sense and you completely understand.

This is not to say it's ok to place bad design in the hands of good hardware - but as Fred said, tradeoffs.

And Prasanna, FYI - try this:

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Adam]: And Prasanna, FYI - try this:

Hm, that seems rather misleading to me. The times are so short that random variations in JVM startup effects are much more significant than anything else. Also, the time to write to System.out is probably much more significant than creating the string. Unless maybe that was the point, sort of? But again, random variation in startup effects will probalby make that very hard to see. Also, using "I " + "like " + "pie." isn't representative of string concatenation in general, since it's a compile-time constant expression and is replaced at compile time with a single constant: ""I like pie."

Here's a somewhat fairer comparison of the two:

[ November 08, 2007: Message edited by: Jim Yingst ]
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I ran your example, here was the output:


After running this 10 or so times, StringBuilder was never faster than +.

My point was that all of those other things you mentioned are a much larger factor than using + or using StringBuilder, and that often times people "optimize" code and just make it slower.

I do agree, though, my test was more biased - but the difference is negligable in either case.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Schaible:

This would fall into the lines of premature optimization - especially considering the IBM 1.5 compiler will do it automatically anytime you have string + string.



Not just the IBM compiler - String optimisation

Sun introduced the StringBuilder class in J2SE 5.0, which is almost the same as StringBuffer, except it's not thread-safe. Thread safety is usually not necessary with StringBuffer, since it is seldom shared between threads. When Strings are added using the + operator, the compiler in J2SE 5.0 and Java SE 6 will automatically use StringBuilder. If StringBuffer is hard-coded, this optimization will not occur.


[ November 09, 2007: Message edited by: Joanne Neal ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should have used a larger value for MAX. I think if you add a zero (or maybe two zeros), you will see the two times are much closer, relatively. With times under a second, we're still seeing significant JVM startup time effects. Probably.

Anyway, yes, the point is that ultimately the difference between these two techniques is negligible. At least for the way the code has been written here, where the complete String is calculated in just one line. If the computation is spread across multiple lines (or one line is repeated in a loop), then StringBuilder starts to become much better. For example:

Here the StringBuilder version is much, much faster. (Adam probably already knows this, but I include it for others who are learning about these differences.) It may be instructive to try to figure out why there is such a huge difference in the performance of these two methods, as it gives a good understanding of when you really, really should use StringBuilder.

The article linked by Joanne has some nice additional examples.

[Adam]: This would fall into the lines of premature optimization - especially considering the IBM 1.5 compiler will do it automatically anytime you have string + string.

A minor bit of trivia: I've been told by someone who worked on IBM's JDK 5 compiler that it does not use StringBuffer or StringBuilder for String concatenation when the + operator is used. Instead they use their own custom class to do this. Perhaps this custom class is slightly more efficient than Sun's classes. Or perhaps it has some other benefit; I don't know. I suspect it makes little difference most of the time.
[ November 09, 2007: Message edited by: Jim Yingst ]
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joanne Neal:


[ November 09, 2007: Message edited by: Joanne Neal ]



So this got me a bit interested in what was happening.. Considering Jim's post about how "I like pie." would be changed to a compile time constant, and then how the compiler would replace it with StringBuiler...

It turns out that compile time constant outweighs stringbuilder. Here's the javap output:

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am doing an application code review which has the problems of OutOfMemoryError.There are many String concatenation operation done in the code by using + operator.Can that be contributing towards OutOfMemoryError?
As someone said "+" operator creates many string objects,will this problem be reduced if we use StringBuffer.append method?
By reading this thread,I understand there is not much difference in performance.But what about object creation?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Jitesh]: By reading this thread,I understand there is not much difference in performance.

If you read the whole thread, you will see that under certain circumstances there can be a huge difference. We don't know enough about how the concatenation is used in your code to see whether that applies here.

There are many String concatenation operation done in the code by using + operator.Can that be contributing towards OutOfMemoryError?

I think that's unlikely. The concatenation may degrade performance by creating a lot of temporary extra objects, but those objects should all available for garbage collection. If you're getting OutOfMemoryError, it's because something cannot be collected. You probably need to look elsewhere.
 
Jitesh Sinha
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim :-

I think that's unlikely. The concatenation may degrade performance by creating a lot of temporary extra objects, but those objects should all available for garbage collection. If you're getting OutOfMemoryError, it's because something cannot be collected. You probably need to look elsewhere.


In that case,if too many objects are created before GC thread starts to run,we shld get OutOfMemoryError.Is that correct?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because if that happens, the system will run full GC before it throws an OutOfMemoryError.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic