charAt() - String Method in Java


This article is a part of String Methods in Java.
charAt() returns the character at the specified index in a String. The parameter is of int type, representing the index of the character to return.


Return type

The return type for the charAt() method is char. Let us look at an example code to understand it better.

Example


Output

Let us look at the output of the above example code.

String Methods charAt()
Fig.1- String Methods : charAt()

IndexOutOfBound exception in charAt()

When we pass negative index or an index greater than length()-1 then the charAt() method throws IndexOutOfBound exception. Let us look at the code for this case.


Output

Let us look at the output of the above example code.

String Methods charAt() IndexOutOfBound
Fig.2- String Methods : charAt() IndexOutOfBound

Since, the index is a negative number the method throws an exception.

Read More

Author : Satyam Kumar -



You will also like:


Snowflake Cloud Data Warehouse

What is Snowflake, and what can you do with Snowflake? In video article.

Learning Journal

Tail Recursion

Tail recursion is another concept associated with recursion. Learn with examples.

Learning Journal

Array Concatenation in Java

Learn how to join two or more arrays together in Java by various methods.

Learning Journal

What is Programming and Programming Language

"PROGRAMMING" is the word which we all come across quite often. Let us see what that means.

Learning Journal

Kafka Core Concepts

Learn Apache Kafka core concepts and build a solid foundation on Apache Kafka.

Learning Journal

Pattern Matching

Scala takes the credit to bring pattern matching to the center.

Learning Journal