Page 1 of 1
[HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Tue Jun 11, 2019 10:11 am
by Username987654
The capacity of a StringBuilder is analogous to a bucket of water. It is empty at the beginning
and fills up as you add water to it. Once it is full, you need to get a bigger bucket and transfer
the water from the smaller bucket to the bigger one.
should be
The array of a StringBuilder is analogous to a bucket of water. It is empty at the beginning
and fills up as you add water to it. Once it is full, you need to get a bigger bucket and transfer
the water from the smaller bucket to the bigger one.
?
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Tue Jun 11, 2019 11:31 am
by admin
You could say that as well. But it is explaining capacity as an abstract concept. It is implemented using an array but that is irrelevant.
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Tue Jun 11, 2019 12:27 pm
by Username987654
Of course. I do L-O-V-E that way of explaining that concept as well. It's just that since there's (always) an initial capacity [particularly with StringBuilder()], I was wondering if any readers could possibly be confused by saying that the capacity is "empty at the beginning", whereas there would always be an array (even without elements)? However, to the book's point .... the word "analogous" was emphasized ... and it completely underscores the point. Thanks as always.
Although there's no (current) information on it, I'm still looking forward to Enthuware's 1Z0-817 sequel once I get past this current hurdle.
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Tue Jun 11, 2019 8:37 pm
by admin
ok, I see your point. Will improve.
817 mock exams are still a few weeks away from release.
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Tue Jun 11, 2019 9:22 pm
by Username987654
Correction: Although there's no (current) information on it, I'm still looking forward to Enthuware's 1Z0-817 book sequel (to 808) once I get past this current hurdle (no rush).
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Tue Jun 11, 2019 9:54 pm
by admin
Username987654 wrote: ↑Tue Jun 11, 2019 9:22 pm
Correction: Although there's no (current) information on it, I'm still looking forward to Enthuware's 1Z0-817
book sequel (to 808) once I get past this current hurdle (no rush).
Ah...there is no update on the book as of now

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Tue Jun 11, 2019 10:22 pm
by Username987654
Thanks
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Wed Feb 19, 2020 2:32 pm
by BlackCat
Hi
Can you explain this point regarding append and insert methods to StringBuilder?
If you pass a null, the string "null" is appended to or inserted in the existing StringBuilder. No NullPointerException is thrown.
I am running code below and in both append and insert getting a compile error warning in Intellij for both
StringBuilder sb = new StringBuilder();
sb.append(null);
sb.insert(1, null);
For append:
Ambiguous method call.
append(String) and append(StringBuffer) in StringBuilder match
Insert is similar:
Ambiguous method call.
insert(int, String) and insert(int, char[]) in StringBuilder match
I am confused by this.
Please help
Many Thanks
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Wed Feb 19, 2020 6:08 pm
by admin
The error that you are getting is because of ambiguity in the method call. The compiler is not able to determine which method are you trying to invoke (append(char[]) or append(CharSequence ), both can accept null.)
The point explained in this section can be observed using this code:
StringBuilder sb = new StringBuilder();
Integer s = null;
sb.append(s);
System.out.println(sb);
May be it should be made clearer.
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Thu Feb 20, 2020 4:47 am
by BlackCat
OK many thanks I understand now - so in your example there is no ambiguity because it uses append(Object) method?
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Thu Feb 20, 2020 4:53 am
by admin
Correct.
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Wed Jun 12, 2024 1:53 pm
by raphaelzintec
tnx for mister deskmush, i finally understand better the topic over sting and stringbuilder especially the method chaining, cuz i got hard time understanding trough books from other authors
also thnks to BlackCat for your question, very usefull
But there is only one thing i don't get is why setting capacity for StringBuilder if it does it automatically, plus noone know the capacity of an entire html page lol
I'm pretty sure noone use capacity and just relay on the auto allocation of JVM
Best regards and tnx in advance for the answear
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Thu Jun 13, 2024 8:19 am
by admin
Glad to know our material is helpful in your preparation.
>But there is only one thing i don't get is why setting capacity for StringBuilder if it does it automatically, plus noone know the capacity of an entire html page lol
It is just a way to optimize the operation of the StringBuilder if you already know the approximate size of the final string. You are right, it is not used very often.
Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]
Posted: Thu Jun 13, 2024 9:22 am
by raphaelzintec
thankx a lot