stringbuilder

Java Compiler replacing StringBuilder with + concatenation

ぐ巨炮叔叔 提交于 2021-02-18 13:39:51
问题 Here's some simple Java code: String s = new StringBuilder().append("a").append("b").append("c").toString(); I compile it with JRE 1.6, and I observe the following in the decompiled class file: String s = "a" + "b" + "c"; I had the following questions: Why does the compiler choose '+' over StringBuilder? Do we have any official Java specification that justifies this behavior? Does it really make sense to use StringBuilder in such cases, where we know compiler is going to change it anyway? 回答1

Java Compiler replacing StringBuilder with + concatenation

梦想与她 提交于 2021-02-18 13:39:07
问题 Here's some simple Java code: String s = new StringBuilder().append("a").append("b").append("c").toString(); I compile it with JRE 1.6, and I observe the following in the decompiled class file: String s = "a" + "b" + "c"; I had the following questions: Why does the compiler choose '+' over StringBuilder? Do we have any official Java specification that justifies this behavior? Does it really make sense to use StringBuilder in such cases, where we know compiler is going to change it anyway? 回答1

Duplicate Value in HashMap

南楼画角 提交于 2021-02-16 18:07:01
问题 I am in big trouble, created a hashMap and inserted two values with same key using StringBuilder as a key of map. Now, while trying to retrieve the data using StringBuilder object is working fine, but in other case its fails to return any value. I have listed three cases in the below given code, ` class MainClass { public static void main(String[] args) { MainClass m = new MainClass(); StringBuilder sb = new StringBuilder("sb"); StringBuilder sb1 = new StringBuilder("sb"); Map<StringBuilder,

Duplicate Value in HashMap

血红的双手。 提交于 2021-02-16 18:05:40
问题 I am in big trouble, created a hashMap and inserted two values with same key using StringBuilder as a key of map. Now, while trying to retrieve the data using StringBuilder object is working fine, but in other case its fails to return any value. I have listed three cases in the below given code, ` class MainClass { public static void main(String[] args) { MainClass m = new MainClass(); StringBuilder sb = new StringBuilder("sb"); StringBuilder sb1 = new StringBuilder("sb"); Map<StringBuilder,

Duplicate Value in HashMap

我的梦境 提交于 2021-02-16 18:05:11
问题 I am in big trouble, created a hashMap and inserted two values with same key using StringBuilder as a key of map. Now, while trying to retrieve the data using StringBuilder object is working fine, but in other case its fails to return any value. I have listed three cases in the below given code, ` class MainClass { public static void main(String[] args) { MainClass m = new MainClass(); StringBuilder sb = new StringBuilder("sb"); StringBuilder sb1 = new StringBuilder("sb"); Map<StringBuilder,

StringBuilder.toString() is printed as empty string in Eclipse-console when too big?

妖精的绣舞 提交于 2021-02-16 05:49:49
问题 The following returns nothing for me in eclipse, is this expected behaviour ? StringBuilder sb = new StringBuilder(""); for(int i = 0; i < 256*256*2*6; i++) { sb.append("a"); } System.out.println(sb.toString()); The code returns without error and the size is well withhin memory capacity unlike in Maximum number of characters stringbuilder can accommodate Solved: The problem lies in the eclipse console and enabling (Window -> Preferences -> Run/Debug -> Console -> fixed width console) will

What's the difference between using chained append and using multiple + to concatenate [duplicate]

家住魔仙堡 提交于 2021-01-07 03:21:35
问题 This question already has answers here : Yet again on string append vs concat vs + (6 answers) Closed 10 months ago . Am trying to append multiple strings in StringBuilder, is there any major difference using String str1 = "some"; String str2 = "string"; Now, sb.append(str1).append(str2); // using chained appends and using sb.append(str1 + str2); // using '+' operator My IDE suggests me to use the first method, is there difference between them regarding being thread safe or something like