Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Fixed broken link
Source Link
dimo414
  • 49.3k
  • 19
  • 167
  • 273

I prefer:

String.format( "{a: %s, b: %s, c: %s}", a, b, c ); 

...because it's short and readable.

I would not optimize this for speed unless you use it inside a loop with a very high repeat count and have measured the performance difference.

I agree, that if you have to output a lot of parameters, this form can get confusing (like one of the comments say). In this case I'd switch to a more readable form (perhaps using ToStringBuilderToStringBuilder of apache-commons - taken from the answer of matt b) and ignore performance again.

I prefer:

String.format( "{a: %s, b: %s, c: %s}", a, b, c ); 

...because it's short and readable.

I would not optimize this for speed unless you use it inside a loop with a very high repeat count and have measured the performance difference.

I agree, that if you have to output a lot of parameters, this form can get confusing (like one of the comments say). In this case I'd switch to a more readable form (perhaps using ToStringBuilder of apache-commons - taken from the answer of matt b) and ignore performance again.

I prefer:

String.format( "{a: %s, b: %s, c: %s}", a, b, c ); 

...because it's short and readable.

I would not optimize this for speed unless you use it inside a loop with a very high repeat count and have measured the performance difference.

I agree, that if you have to output a lot of parameters, this form can get confusing (like one of the comments say). In this case I'd switch to a more readable form (perhaps using ToStringBuilder of apache-commons - taken from the answer of matt b) and ignore performance again.

Added alternatives
Source Link
tangens
  • 39.8k
  • 21
  • 128
  • 140

I prefer:

String.format( "{a: %s, b: %s, c: %s}", a, b, c ); 

...because it's short and readable.

I would not optimize this for speed unless you use it inside a loop with a very high repeat count and have measured the performance difference.

I agree, that if you have to output a lot of parameters, this form can get confusing (like one of the comments say). In this case I'd switch to a more readable form (perhaps using ToStringBuilder of apache-commons - taken from the answer of matt b) and ignore performance again.

I prefer:

String.format( "{a: %s, b: %s, c: %s}", a, b, c ); 

...because it's short and readable.

I prefer:

String.format( "{a: %s, b: %s, c: %s}", a, b, c ); 

...because it's short and readable.

I would not optimize this for speed unless you use it inside a loop with a very high repeat count and have measured the performance difference.

I agree, that if you have to output a lot of parameters, this form can get confusing (like one of the comments say). In this case I'd switch to a more readable form (perhaps using ToStringBuilder of apache-commons - taken from the answer of matt b) and ignore performance again.

Source Link
tangens
  • 39.8k
  • 21
  • 128
  • 140

I prefer:

String.format( "{a: %s, b: %s, c: %s}", a, b, c ); 

...because it's short and readable.