How are string positions indexed in this syntax?

QuestionsQuestions8 SkillsProBasic String OperationsNov, 08 2025
080

In Java, string positions are indexed starting from 0. This means that the first character of a string is at index 0, the second character is at index 1, and so on. For example, in the string "Hello":

  • H is at index 0
  • e is at index 1
  • l is at index 2
  • l is at index 3
  • o is at index 4

You can access a character in a string using the charAt() method. For example:

String str = "Hello";
char firstChar = str.charAt(0); // 'H'
char secondChar = str.charAt(1); // 'e'

If you try to access an index that is out of bounds (less than 0 or greater than or equal to the string length), it will throw a StringIndexOutOfBoundsException.

0 Comments

no data
Be the first to share your comment!