Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

charAt()

Example

Retrieve the character at the initial position (0) of the string.

String myStr = "Hello";
char result = myStr.charAt(0);
System.out.println(result);

Definition and Usage

The charAt() method retrieves the character positioned at the specified index within a string.

The index of the first character is 0, the second character is 1, and so on.

Syntax

public char charAt(int index)

Parameter Values

Parameter

Description

index

An integer value indicating the index of the character to be returned.

Technical Details

Returns:

character value located at the specified index within this string. The initial character is situated at index 0.

Throws:

IndexOutOfBoundsException occurs if the index is negative or exceeds the length of the specified string.