charAt()

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

An Immutable String

Because Strings are immutable, you can safely give this answer even though you don't know what mysteryMethod() does. Once a String has been constructed, you know what is in it.

charAt()

The charAt(int index) method of String returns the character at the specified index. Index 0 is the first character of the String, and the last character is at index length()-1. For example,

String eg = "An Immutable String";

char ch0  = eg.charAt(0);   // ch0 gets 'A'
char ch1  = eg.charAt(1);   // ch1 gets 'n'
char ch5  = eg.charAt(5);   // ch5 gets 'm'

char ch18 = eg.charAt( eg.length()-1 );   // ch18 gets 'g'
char ch17 = eg.charAt( eg.length()-2 );   // ch17 gets 'n'

Notice that the method returns a value of type char.

QUESTION 3:

(Design Question: ) How could you make a string that contains the characters of another string in reverse order?