0

So I have a pretty large string and it will only get bigger. It's currently at 1,539 characters and I expect to tack on a couple more queries to it in the future. So when I first built the string I did it the old fashioned way created around 7 string objects and concatenating them all with the plus.

But then I remembered what my teacher said way back when and decided to switch it up and try the string builder. The only problem is the only real disadvantage I can find online about using this is readability and I don't think the readability is all that bad to be honest.

So my question is is building an object, to house over 1500 characters a good idea or a bad idea. And if you were in my situation would you use string, string builder, or string buffer? And please save the speech on readability. It's just my code and I'm the only one reading it and it's not all that difficult so don't use it as a variable. Thank you!

StringBuilder builder = new StringBuilder(1600);
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, stride.timestamp, stride.recipientView, \"stride\" as notType FROM user INNER JOIN stride ON user.id = stride.sourceUserId WHERE stride.recipientId = ? AND stride.sourceUserId != ? ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, strideLike.timestamp, strideLike.recipientView, \"strideLike\" as notType FROM user INNER JOIN strideLike ON strideLike.sourceUserId = user.id INNER JOIN stride ON stride.id = strideLike.strideId WHERE strideLike.recipientId = ? AND user.id != strideLike.recipientId ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideComment.timestamp, strideComment.recipientView, \"strideCommentMe\" as notType FROM user INNER JOIN strideComment ON user.id = strideComment.sourceUserId INNER JOIN stride ON stride.id = strideComment.strideId WHERE stride.sourceUserId = ? AND user.id != stride.sourceUserId ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideCommentLike.timestamp, strideCommentLike.recipientView, \"strideCommentLike\" AS notType FROM user INNER JOIN strideCommentLike ON strideCommentLike.sourceUserId = user.id INNER JOIN strideComment ON strideComment.id = strideCommentLike.commentId INNER JOIN stride ON stride.id = strideComment.strideId WHERE strideComment.sourceUserId = ? AND user.id != ? ");
builder.append(" ORDER BY timestamp DESC");
2
  • Thats what I had originally. But then changing parts of the query became a nightmare Commented Mar 4, 2013 at 6:33
  • You can write it as a concatenation of multiple strings. Concatenation of string literals will be done at compile time instead of run time. Commented Mar 4, 2013 at 6:34

2 Answers 2

5

If you don't need to do any other operations on the string, you should use string concatenation for this. If there are no variables being appended into the string, then the string concatenation will happen at compile time.

Using StringBuilder forces the concatenation to be performed at runtime, regardless as to whether there are variables being appended or not.

Sign up to request clarification or add additional context in comments.

2 Comments

So you only use string builder and buffer when you are utilizing variables?
+1. I was looking for a link proving that compiler will actually replace String concatenation into StringBuilder calls, and it seems here it is.
0

StringBuilder are more effective if you are generating strings at runtime.

Using either of these approaches will not be any different, compiler will do the necessary optimization.

StringBuilder builder = new StringBuilder(1600);
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, stride.timestamp, stride.recipientView, \"stride\" as notType FROM user INNER JOIN stride ON user.id = stride.sourceUserId WHERE stride.recipientId = ? AND stride.sourceUserId != ? ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, strideLike.timestamp, strideLike.recipientView, \"strideLike\" as notType FROM user INNER JOIN strideLike ON strideLike.sourceUserId = user.id INNER JOIN stride ON stride.id = strideLike.strideId WHERE strideLike.recipientId = ? AND user.id != strideLike.recipientId ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideComment.timestamp, strideComment.recipientView, \"strideCommentMe\" as notType FROM user INNER JOIN strideComment ON user.id = strideComment.sourceUserId INNER JOIN stride ON stride.id = strideComment.strideId WHERE stride.sourceUserId = ? AND user.id != stride.sourceUserId ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideCommentLike.timestamp, strideCommentLike.recipientView, \"strideCommentLike\" AS notType FROM user INNER JOIN strideCommentLike ON strideCommentLike.sourceUserId = user.id INNER JOIN strideComment ON strideComment.id = strideCommentLike.commentId INNER JOIN stride ON stride.id = strideComment.strideId WHERE strideComment.sourceUserId = ? AND user.id != ? ");
builder.append(" ORDER BY timestamp DESC");

String query = "SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, stride.timestamp, stride.recipientView, \"stride\" as notType FROM user INNER JOIN stride ON user.id = stride.sourceUserId WHERE stride.recipientId = ? AND stride.sourceUserId != ? " +
" UNION ALL " +
"SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, strideLike.timestamp, strideLike.recipientView, \"strideLike\" as notType FROM user INNER JOIN strideLike ON strideLike.sourceUserId = user.id INNER JOIN stride ON stride.id = strideLike.strideId WHERE strideLike.recipientId = ? AND user.id != strideLike.recipientId " + 
" UNION ALL " +
"SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideComment.timestamp, strideComment.recipientView, \"strideCommentMe\" as notType FROM user INNER JOIN strideComment ON user.id = strideComment.sourceUserId INNER JOIN stride ON stride.id = strideComment.strideId WHERE stride.sourceUserId = ? AND user.id != stride.sourceUserId " +
" UNION ALL " +
"SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideCommentLike.timestamp, strideCommentLike.recipientView, \"strideCommentLike\" AS notType FROM user INNER JOIN strideCommentLike ON strideCommentLike.sourceUserId = user.id INNER JOIN strideComment ON strideComment.id = strideCommentLike.commentId INNER JOIN stride ON stride.id = strideComment.strideId WHERE strideComment.sourceUserId = ? AND user.id != ? " + 
" ORDER BY timestamp DESC" +

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.