0

I want to short my coding as much as I can. I tried to use "concat" but facing a problem to combine in multiple way. Did concat allow to combine two concat only (per concat)?

String concat1 = "Please Make A Choice :";
    String concat2 = "(1) For Calculate Area Of Circle";
    String concat3 = "(2) For Calculate Area Of Rectangle";
    String concat4 = "(3) For Calculate Area Of Triangle";
    String concat5 = concat1.concat(concat2,concat3,concat4);
Edie
  • 65
  • 1
  • 2
  • 11

2 Answers2

2

concat takes one String argument, and appends it to the String instance it's called on. See the docs for the details.

You can use the + operator to do this

String concat5 = concat1+concat2+concat3+concat4;

But be aware that this is a relatively expensive process, and if you're doing a lot of concatenation the StringBuilder class is the thing to use instead.

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
  • Outside of a loop, the compiler will usually optimise to a `StringBuilder` anyway (`StringBuffer` is an older `synchronized` version and should be avoided). The `+` operator is shorthand for `concat`. – Boris the Spider Oct 16 '14 at 17:08
  • @BoristheSpider not quite: http://stackoverflow.com/questions/47605/string-concatenation-concat-vs-operator – clcto Oct 16 '14 at 17:09
  • Okay, fair enough, it calls a nullsafe `toString`. – Boris the Spider Oct 16 '14 at 17:11
  • Please don't use `StringBuffer`; `StringBuilder` is essentially always preferable. – Louis Wasserman Oct 16 '14 at 17:15
  • @Jon Kiparsky , sir.. what do you mean by " But be aware that this is a relatively expensive process, and if you're doing a lot of concatenation the StringBuffer class is the thing to use instead. ".. Im so sorry.. I'm new with coding.. – Edie Oct 16 '14 at 17:15
  • @Edie Don't worry about that. `concat1 + concat2 + concat3 + concat4` is the right way to do it. It gets translated into `StringBuilder` by the Java compiler automatically, and so you don't need to worry about it. – C. K. Young Oct 16 '14 at 17:20
  • @Edie then don't worry about it, it is a performance optimization that you don't have to worry about until performance becomes an issue. Also, in this scenario, the string is probably just interned, so it makes 0 difference. – clcto Oct 16 '14 at 17:20
  • @ChrisJester-Young since they are all compile time constants, won't it just do it during compile? – clcto Oct 16 '14 at 17:21
  • @clcto .. Thanks for the explaination.. You guys so nice.. :) – Edie Oct 16 '14 at 17:24
  • @clcto They're only treated as constants if declared as static final fields. Not the case here in the OP's code. – C. K. Young Oct 16 '14 at 17:25
  • @Edie yes, don't worry about it. For a small set of operations it's not going to make any difference. If you were looping over a large number of Strings and glomming them together, it would make a difference, since each + operation would create a new StringBuilder – Jon Kiparsky Oct 16 '14 at 17:25
  • @all - yes, I misspoke. StringBuilder, not StringBuffer. (I confess, I do get the two mixed up in my head... my bad) – Jon Kiparsky Oct 16 '14 at 17:27
1

you can use + operator to concat Strings but if you need to use keyword concat then this is how

concat5=  concat1.concat(concat2).concat(concat3).concat(concat4);
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60