charAt() method in Java return a character on the specific index of the string. Here are some of the best examples that will clear your concept about the charAt() method.

Method signature:

The signature for a charAt() method is as shown below.

public char charAt(int index)

Method parameters and return type:

Return type: It returns a character.

Parameters: It takes an integer value as a parameter.

Throws: It also throws NullPointerException if the index is less than 0.

Example 1:

In the first example, we have used charAt() method to print the characters on the specific indexes of the string “Hello world.” Here we will have passed the index number, and the method has returned the character on that index.

Source code:

public class Example1 {
    
    public static void main(String[] args) {
        
        String sentence= "Hello world";
        System.out.println("Character at index 0 is:" + sentence.charAt(0));
        System.out.println("Character at index 4 is:" + sentence.charAt(4));
        System.out.println("Character at index 7 is:" + sentence.charAt(8));
    }
}

Output:

run:
Character at index 0 is:H
Character at index 4 is:o
Character at index 7 is:r
BUILD SUCCESSFUL (total time: 2 seconds)

Example 2:

In this example, we have compared two strings. We have found the same characters on the same indexes of the strings using charAt() method.

Source code:

public class Example2 {
    
    public static void main(String[] args) {
        
        String sentence1= "Sara is a Java developer";
        String sentence2= "Mike is a Python developer";
        
        //Removing white spaces from the string
        sentence1=sentence1.replaceAll("\\s", "");
        sentence2=sentence2.replaceAll("\\s", "");
        
        //It we will loop till the end of the sentences
        int i=0;
        while(i<sentence1.length() && i<sentence2.length()){
            if(sentence1.charAt(i) == sentence2.charAt(i)){
                System.out.println("Same character "+ sentence1.charAt(i)+ " found at index: "+i);
            }
            i++;
        }
    }
}

Output:

run:
Same character i found at index: 4
Same character s found at index: 5
Same character a found at index: 6
Same character e found at index: 14
BUILD SUCCESSFUL (total time: 1 second)

Example 3:

You can also change the string into the characters array using charAt() method. This example explains how you can convert String into a characters array.

Source code:

public class Example3 {
    
    public static void main(String[] args) {
        
        String sentence="Fight till your last breath!!";
        
        char charArray[]= new char[sentence.length()];
        
        for(int i=0; i<charArray.length; i++){
            charArray[i]=sentence.charAt(i);
        }
        
        System.out.print("Character array is: ");
        
        //Printing all elements of charArray
        for(int i=0; i<charArray.length; i++){
            System.out.print(charArray[i]);
        }
    }
}

Output:

Character array is: Fight till your last breath!!BUILD SUCCESSFUL (total time: 0 seconds)

References:

Happy Learning 🙂

Leave A Comment