[HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

[HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post 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.
?

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post 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.
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post 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.

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post by admin »

ok, I see your point. Will improve.
817 mock exams are still a few weeks away from release.
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post 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).

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post 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 :)
If you like our products and services, please help us by posting your review here.


BlackCat
Posts: 8
Joined: Wed Jan 29, 2020 11:09 am
Contact:

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post 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

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post 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.
If you like our products and services, please help us by posting your review here.

BlackCat
Posts: 8
Joined: Wed Jan 29, 2020 11:09 am
Contact:

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post by BlackCat »

OK many thanks I understand now - so in your example there is no ambiguity because it uses append(Object) method?

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 334, Sec. 12.2.2 - stringbuilder-api]

Post by admin »

Correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 55 guests