Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Clarity about charAt() method in String

 
Narayana Bojja
Ranch Hand
Posts: 135
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have already seen some threads having discussion about this method.

chatAt method details according to Java API Document.

public int codePointAt(int index)

Returns the character (Unicode code point) at the specified index. The index refers to char values (Unicode code units) and ranges from 0 to length()- 1.

Parameters:
index - the index to the char values
Returns:
the code point value of the character at the index
Throws:
IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.



I have seen the below link

https://coderanch.com/t/640486/ocajp/certification/IndexOutOfBoundsException-StringIndexOutOfBoundsException

So , According to Java doc charAt method throws IndexOutOfBoundsException. But It acually throws StringIndexOutOfBoundsException.

But I have got one question in my mind.

If I got question related to this method in the exam, what should I choose either IndexOutOfBoundsException or StringIndexOutOfBoundsException .Because In one book IndexOutOfBoundsException  is the wright answer, In another book StringIndexOutOfBoundsException is the wright answer ??
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should always follow the JavaDoc. StringIndexOutOfBoundsException IS-AN IndexOutOfBoundsException, so the JavaDoc wasn't wrong when it said it could throw the latter, even if the actual type is a sub-class. The fact that a specific implementation may throw a StringIndexOutOfBoundsException does not mean that another implementation couldn't throw a different kind of IndexOutOfBoundsException.
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narayana Bojja wrote:If I got question related to this method in the exam, what should I choose either IndexOutOfBoundsException or StringIndexOutOfBoundsException

For an exam, listen to Stephan, because he speaketh not with forked tongue.

However. in the real world, the answer is probably 'neither', because you generally shouldn't allow methods (or the JVM) to throw exceptions that you can easily check for.

Unfortunately, exams are rarely concerned with real life...

Winston
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narayana Bojja wrote:If I got question related to this method in the exam, what should I choose either IndexOutOfBoundsException or StringIndexOutOfBoundsException .Because In one book IndexOutOfBoundsException  is the wright answer, In another book StringIndexOutOfBoundsException is the wright answer ??

I am 99.99% sure you won't encounter a question on the actual exam where you have to make a choice between those two exceptions. If you get a textual question, you'll probably get a list of possible exceptions and one of them will be IndexOutOfBoundsException or StringIndexOutOfBoundsException. And if both exceptions are listed in the list with possible exceptions, you probably have to select two correct answers. But it is much more likely that you have a code snippet using the charAt() method (or another method) with an out-of-bounds index and thus you have to select "An exception is thrown at runtime" as the correct answer.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic