Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double value used as argument for the StringBuilder's deleteCharAt() method

I'm preparing for Oracle Associate Certification and am reading the following book :

Oracle Certified Associate, Java SE7 Programmer Study Guide

At the page 77, there is a knowledge test with the following question :

Given the following declaration :

StringBuilder sb = new StringBuilder();

Which of the following are valid uses of the sb variable?

a. sb.append(34.5); b. sb.deleteCharAt(34.5); c. sb.toInteger(3); d. sb.toString();

I assumed the right answers were a & d.

But here's the answer from the book :

a, b, and d There is no StringBuilder toInteger method.

With no other explanation.

So, I went to check the Source Code and here it is :

279    public StringBuilder deleteCharAt(int index) {
280        super.deleteCharAt(index);
281        return this;
282    }

Taking an int as an argument and the method is not overloaded anywhere else.

Do you know an explanation to this "correction" in the book?

like image 485
Yassin Hajaj Avatar asked Dec 11 '25 19:12

Yassin Hajaj


2 Answers

It is a mistake in the book. Your answer is correct, it should be only a & d. Here is a link, https://www.packtpub.com/books/content/support/10143 and find the following errata heading (about midway in the page): Errata type: Technical | Page number: 297 | Errata date: 5 Feb 2013

like image 147
rby Avatar answered Dec 13 '25 09:12

rby


For the example given above sb.deleteCharAt(34.5)

The compiler says

The method deleteCharAt(int) in the type StringBuilder is not applicable for the arguments (double)

Then the book's answer is wrong.

like image 22
Rafik BELDI Avatar answered Dec 13 '25 07:12

Rafik BELDI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!