Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

To print alphabets in Java, loop a char variable from 'A' to 'Z' and print each letter. Because char is a numeric type backed by ASCII values, the loop simply steps through codes 65 to 90 for uppercase and 97 to 122 for lowercase. A for loop is the clearest way, but while loops, ASCII casting, and streams work too.
The trick behind every alphabet program is that Java stores characters as numbers. A char is a 16-bit Unicode value, and for the English alphabet those values match the ASCII table: 'A' is 65, 'Z' is 90, 'a' is 97, and 'z' is 122. Because char is an integral type, you can compare and increment it just like an int, so a condition such as ch <= 'Z' and an operation such as ++ch behave exactly as they would with numbers. That single fact is what makes looping through the alphabet a two-line problem.
The most common approach initializes a char at 'A' and loops while it is less than or equal to 'Z', printing each letter followed by a space.
class PrintUppercase {
public static void main(String[] args) {
char ch;
for (ch = 'A'; ch <= 'Z'; ++ch) {
System.out.print(ch + " ");
}
}
}Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Only the start and end characters change: begin at 'a' and loop while the value stays at or below 'z'.
class PrintLowercase {
public static void main(String[] args) {
char ch;
for (ch = 'a'; ch <= 'z'; ++ch) {
System.out.print(ch + " ");
}
}
}Output:
a b c d e f g h i j k l m n o p q r s t u v w x y z
A while loop produces the same result and can read more naturally when the increment logic is more complex. Initialize the variable before the loop and increment it inside the body.
class PrintWhile {
public static void main(String[] args) {
char ch = 'A';
while (ch <= 'Z') {
System.out.print(ch + " ");
ch++;
}
}
}If you want a refresher on the construct itself, see this TestMu AI example on how to demonstrate a while loop in Java.
To print Z to A, start at 'Z' and decrement. To make the ASCII relationship explicit, loop an int and cast it to char:
class Variations {
public static void main(String[] args) {
// Reverse: Z to A
for (char ch = 'Z'; ch >= 'A'; ch--) {
System.out.print(ch + " ");
}
System.out.println();
// Using ASCII values 65..90
for (int i = 65; i <= 90; i++) {
System.out.print((char) i + " ");
}
}
}On Java 8 and above, IntStream.rangeClosed('A', 'Z') can generate the same sequence without an explicit loop, mapping each int to a char before printing.
Printing the alphabet in Java comes down to one idea: characters are numbers, so you can loop a char from 'A' to 'Z' exactly like an int. A for loop is the cleanest solution, while loops, ASCII casting, reverse order, and streams all give you flexibility once you understand the underlying character codes. Master this pattern and you have a foundation for far more advanced Java string and character manipulation.
Use a for loop with a char variable that starts at 'A' and continues while it is less than or equal to 'Z', incrementing each iteration. Because char is a numeric type in Java, the loop steps through the ASCII codes 65 to 90 and prints each letter with System.out.print(ch).
In Java, char is an integral type backed by a 16-bit Unicode value, so characters can be compared and incremented like numbers. 'A' equals 65 and 'Z' equals 90, which lets ch <= 'Z' and ++ch work exactly as they would with integers, stepping through the alphabet.
Loop an int from 65 to 90 and cast each value to char with (char) i before printing. Because 65 to 90 are the ASCII codes for A to Z, casting the number produces the matching letter, giving the same output as looping directly over a char variable.
Start the char variable at 'Z' and decrement it while it stays greater than or equal to 'A'. Using for (char ch = 'Z'; ch >= 'A'; ch--) prints the alphabet from Z back to A, since the loop walks the ASCII codes downward from 90 to 65.
Run two loops: one from 'A' to 'Z' for uppercase and one from 'a' to 'z' for lowercase. The lowercase letters occupy ASCII codes 97 to 122, so the same loop pattern works by simply changing the start and end characters.
Yes. From Java 8 onward you can use IntStream.rangeClosed('A', 'Z') and map each int to a char, then print the result. This stream-based approach avoids an explicit loop, though a simple for loop remains the clearest and most common way to print the alphabet.
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.

TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance