• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

charAt() Query

 
Kancharla Madhu
Ranch Hand
Posts: 109
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following statement is with respect to chartAt() of String,This statement looks true to me but it was given as false in one of the mock test. It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0). Could someone confirm whether it is true or false.
 
Paul Clapham
Sheriff
Posts: 20990
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's false.

But you would like to know why, wouldn't you? So don't guess, go and look at the API documentation for the method. It tells you there what exceptions are thrown and when.
 
Kancharla Madhu
Ranch Hand
Posts: 109
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the below code snippet the statement is TRUE . ( It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0). )

 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class String / charAt says -


Throws:
IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.


Regards,
Dan
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kancharla Madhu,

String str="abcd"

Try to print index of a b c d you will get 0 1 2 3 respectively. If you try chatAt() method other than 0 1 2 3 you will get IndexOutOFBoundException.

Regards,
James
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic