字符串生成器对大字符串的缺点

时间:2013-03-04 06:30:10

标签: java string

所以我有一个非常大的字符串,它只会变大。它目前有1,539个字符,我希望将来可以再提出几个问题。因此,当我第一次构建字符串时,我按照旧方式创建了大约7个字符串对象并将它们连接起来。

但后来我想起了我的老师回答的时候,并决定将其切换并尝试使用字符串构建器。唯一的问题是我能在网上找到关于使用这个可读性的唯一真正的缺点,我认为可读性并不是说实话。

所以我的问题是构建一个对象,容纳超过1500个字符是一个好主意或一个坏主意。如果你在我的情况下你会使用字符串,字符串构建器或字符串缓冲区吗?请保存关于可读性的演讲。这只是我的代码,我是唯一一个阅读它的人,并不是那么困难,所以不要把它当作变量。谢谢!

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 个答案:

答案 0 :(得分:5)

如果您不需要对字符串执行任何其他操作,则应使用字符串连接。如果没有变量附加​​到字符串中,则字符串连接将在编译时发生。

使用StringBuilder强制在运行时执行连接,无论是否附加变量。

答案 1 :(得分:0)

如果在运行时生成字符串,

StringBuilder会更有效。

使用这些方法中的任何一种都不会有任何不同,编译器将进行必要的优化。

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" +