• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method charAt() in String class

 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this question on Enthuware.

Which of these statements concerning the charAt() method of the String class are true?

1> It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0).

2>It throws ArrayIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0).

There are other options as well but irrelevant to my question.

Now according to the author of the enthuware

"Both - ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException, extend IndexOutOfBoundsException and although in practice, the charAt method throws StringIndexOutOfBoundsException, it is not a valid option because the implementation is free to throw some other exception as long as it is an IndexOutOfBoundsException."

I clearly understood the explanation after reading the discussion on this question. But my questions now,

1>How many such methods from various classes ( String, StringBuilder, Array, ArrayList) should we check into JLS and also the source code of the class?

2>Does evaluator of 1Z0-803 exam agree with enthuware author? that, one has to go according to the JLS documentation but not with the actual body of the method from the class?

Thanks everyone in advance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you don't need to know this for the exam. You should know a runtime exception is thrown, but you definitely don't need to know which one.

According to the String JavaDoc, an IndexOutOfBoundsException is thrown. That class is a superclass of StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException. That's already more than you need to know. You don't need to dig through the JLS or source code to know more details. Plus that is the specific implementation for source code. Implementation could change in the future as long as it is some subclass of IndexOutOfBoundsException.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3820
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have had a lot of discussion internally about this question and while I agree with most of the stuff Jeanne said, I would have to disagree with one small bit - you do need to know which exception is thrown by the methods of the String and ArrayList class. There are questions in the exam that require you to know that, unfortunately. Just knowing that they throw RuntimeException is not sufficient. This is exactly the reason we have left the question in the question bank instead of taking the easy way out and remove the question to avoid uncomfortable support issues

As Jeanne said, you do not need to look into the source code of any JDK class. That is the whole point of the JavaDoc API descriptions. You need to know what the JavaDoc says about these methods and not what the source code of these classes do.

BTW, here is the link to the external discussion that we have at our forum about this question.

HTH,
Paul.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the external discussion, I understand that the 1st answer "It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0). " is not a correct answer. That's just wrong!

I would always mark this answer as a correct one! Because I prepared thoroughly for the OCAJP7 exam, I experimented with the charAt method and a few different indexes. When I used an invalid index (<0 or >=length), I got a StringIndexOutOfBoundsException. As this output of such a test confirms:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:658)
at Test23.main(Test.java:28)

And the 1st answer states "It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0). " and that's exactly what I'm experiencing. So this answer MUST be correct, without any doubt!

I'm preparing for the OCAJP7 exam, so I'm only concerned about the Java 7 version. It could be (although very unlikely) that Java API developers decide (in Java 10 for example) to throw another subclass of IndexOutOfBoundsException. I couldn't care less, I'm only interested in the behavior of Java 7 classes. And in Java 7 the charAt method throws a StringIndexOutOfBoundsException, nothing else!

If we start making assumptions of possible changes in the future in method implementations; it will be hard, very hard to create (and answer) questions. For example, the toString method of StringBuilder states "Returns a string representing the data in this sequence.". So what about this possible mock question.

What's the output of the following code? (Choose one option)

1/ abc
2/ The output can't be determined
3/ An exception at runtime
4/ Compilation fails

I think everyone agrees the correct answer would be 1 (and I also think everyone is hoping that the questions on the atual exam are as easy as this one ). But unfortunately, that's not the correct answer. The correct answer should be 2, because the output can't be determined. The documentation states a string representing the data is returned, but it could be that Java API developers decide to return the string in reverse order ("cba"), add a hyphen after each character ("a-b-c-"), increment each character with 5 ("fgh"),... These are all valid string representations of the data, so could be the output of calling the toString method. Hence the 2nd answer is the correct one.

Now that I shared my criticism, it's time to be a little bit constructive as well and suggest some changes to the answers to improve this question (and posible answers), so there is no confusion about the correct answers (and these answers also reflect the behavior of Java 7 classes).

Suggestion 1 (my favourite)
1> It throws StringIndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0).
2> It throws ArrayIndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0).
3> It could throw any subclass of IndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0).
Correct answers: 1 and 3

Suggestion 2
1> It will always (forever and ever) throw IndexOutOfBoundsException (or any of its subclasses) if passed a value higher than or equal to the length of the string (or less than 0).
2> It will always (forever and ever) throw StringIndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0).
3> It will always (forever and ever) throw ArrayIndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0).
Correct answers: 1

Suggestion 3
1> It throws IndexOutOfBoundsException (or any of its subclasses) if passed a value higher than or equal to the length of the string (or less than 0).
2> It throws StringIndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0).
3> It throws ArrayIndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0).
Correct answers: 1 and 2

Just my 2 cents! Feel free to ignore them.
Kind regards,
Roel

(Disclaimer: the correct answer of the mock question about StringBuilder is of course answer 1 "abc". I was just trying to make my point.)
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @Roel De Nijs . @Paul I have already been to the link that you have specified, as discussion on this question(about which I have mentioned in my original post), still had the doubt so came to this forum for help.

Kind Regards,
Prathima
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3820
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:Thanks @Roel De Nijs . @Paul I have already been to the link that you have specified, as discussion on this question(about which I have mentioned in my original post), still had the doubt so came to this forum for help.

Kind Regards,
Prathima

Yes, that is not a problem, Prathima. It is always better to hear multiple views and then form your own opinion.
I posted the link so that others can also see it.
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic