In Java, Character.isUpperCase(ch) returns true when the character is uppercase: A to Z, and capital letters of other scripts, such as Greek capital alpha (U+0391) and E with acute (U+00C9).
Formally, the category is UPPERCASE_LETTER; since Java 7, characters with the Unicode Other_Uppercase property count too. Digits and punctuation have no case and return false.
A static method of java.lang.Character (Java 1.0+): call it on the class, as in Character.isUpperCase(...); you do not need a Character object. No import is needed, because java.lang is always available. The int codePoint overload was added in Java 5.
Unicode version.
The answer comes from the Unicode Character Database built into your JDK, and each Java release follows a specific Unicode version (Java 8, for example, uses Unicode 6.2). The characters used on this page have had the same properties for many Unicode versions, so the examples print the same result on any current JDK.
A word counts as all caps when it has at least one letter and none of its letters is lowercase. Digits are ignored.
Java
public class CharacterIsUpperCaseAllCaps {
public static void main(String[] args) {
String[] words = {"NASA", "Java", "HTTP2", "ok"};
for (String w : words) {
boolean anyLetter = false;
boolean allUpper = true;
for (char c : w.toCharArray()) {
if (Character.isLetter(c)) {
anyLetter = true;
if (!Character.isUpperCase(c)) {
allUpper = false;
}
}
}
System.out.println(w + " all caps? " + (anyLetter && allUpper));
}
}
}
Output
NASA all caps? true
Java all caps? false
HTTP2 all caps? true
ok all caps? false
How It Works
Letters only. The inner if skips non-letters, so the 2 in "HTTP2" cannot spoil the result.
One lowercase letter is enough. In "Java" the letter a fails isUpperCase and sets allUpper to false.
Need at least one letter.anyLetter keeps a string such as "123" from counting as all caps.
Example 2 — Split a camelCase Name into Words
Insert a space before every uppercase letter except the first character.
Java
public class CharacterIsUpperCaseCamel {
public static void main(String[] args) {
String name = "parseHttpResponseCode";
StringBuilder words = new StringBuilder();
int count = 1;
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (i > 0 && Character.isUpperCase(c)) {
words.append(' ');
count++;
}
words.append(c);
}
System.out.println(words + " (" + count + " words)");
}
}
Output
parse Http Response Code (4 words)
How It Works
Capitals mark word starts. H, R, and C are uppercase, so a space is appended in front of each.
Skip index 0. The i > 0 guard stops a leading space if the name starts with a capital, as in "ParseHttp".
Count while splitting.count starts at 1 for the first word and grows with every space: 4 words.
Example 3 — Uppercase Letters in Other Scripts
Greek and accented capitals are uppercase too; a digit and a titlecase digraph are not.
Java
public class CharacterIsUpperCaseScripts {
public static void main(String[] args) {
char[] chars = {'\u0391', '\u03B1', '\u00C9', '9', '\u01C5'};
for (char c : chars) {
System.out.println((int) c + " upper=" + Character.isUpperCase(c));
}
}
}