Java String charAt() method example

HQ » Java Tutorial » java.lang » String » charAt() method example
Java String charAt() method example 2018-02-19T12:36:12+00:00

java.lang.String charAt()

Description :

This java tutorial focuses on the charAt() method of java.lang.String class. This method returns a char value specified on the index input. So basically this article just shows an example on practical usage of charat method.

Practical Usage :

During my career as a developer, I have encountered many instances that we have to deal with a fixed width String input and every character on the String passed to our java code signifies something. This method will handle such scenario.

Method Syntax :

public char charAt(int index)

Parameter input :

  • index – This method accepts only one input which is index in int data type.

Method Returns :

This method returns a char datatype at based on the index input as the method parameter.

Exception :


– The charAt() method of String class throws this exception whenever the index provided on the method argument is outside the range of the String. The maximum index argument is expected to be String length -1.

Java Code Example :

This example source code gets the character gender from a String source using the charAt method. The format of the source string is AgeGenderName, thus our code get the Gender character and then gives an output interpreted from the gender character.

 Sample Output :

Running the charAt() example source code will give you the following output

java string charAt method example

java string charAt method example

Exception Scenario :

Suggested Reading List:


Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.