Personal tools
You are here: Home Members martin Martins Weblog Checking a String for upper case characters
Document Actions
  • Print this page
  • Add Bookmarklet

Checking a String for upper case characters

Trying three programming languages for the Java Platform at the same time shows interesting features of the different languages. Here solutions for checking a String for upper case characters in Java, Groovy, and Scala.

The Task

A simple task, taken from the Programming in Scala book: Check whether a String contains any upper case character.

Solutions

Java

// Java
String aString = "abCde";
boolean upperFound = false;
for (char c : aString.toCharArray()) {
if (Character.isUpperCase(c)) {
upperFound = true;
        break;
    }
}
Explicit iteration, one could trade the break for a while loop with explicit index management. Not too difficult, isn't it?

Scala

// Scala
val aString = "abCde"
val upperFound = aString.exists(_.isUpperCase)

Shorter, clearer, no control structures. That's the way -- I like it.

Groovy

So what about Groovy. Should be very similar to the Scala solution, since Groovy has closures, too, shouldn't it?

Let's try the it the Scala way, nearly 1:1:

// Groovy (1)
def aString = "abCde"
def upperFound = aString.any{it.isUpperCase()}
// RUNTIME ERROR!

Why doesn't it work? Groovy treats aString as a sequence of Strings, not Characters, and String has no method "isUpperCase".

OK, make it more explicit:

// Groovy (2)
def aString = "abCde"
def upperFound = aString.any{(it as Character).isUpperCase()}
// RUNTIME ERROR!

This one is scary: Even though there is an explicit conversion to Character, the method isUpperCase on java.lang.Character is not chosen. Groovy complains, that there is a method isUpperCase(char) and a method isUpperCase(int).

Solution: explicitly use the static helper method on Character and convert the iteration value to Character.

// Groovy (3)
def aString = "abCde"
def upperFound = aString.any{Character.isUpperCase(it as Character)}

Conclusion

This is of course a kind of a micro-benchmark for the expressiveness of the languages. Runtime performance has not been evaluated, so no statement on that, here.

The Java solution is clearly the most verbose and least clear solution.

The Scala solution is extremely short and clear. It is also statically type checked, even though there is no explicit type information given. The compiler can infer the types starting from the string literal.

The Groovy solution is reasonable short and readable. But the solution was not straightforward. Why does the iteration on String yield String as element type? Why does the call to isUpperCase fail in Listing "Groovy (2)"?



_____
tags:
Tuesday, November 04, 2008 in Programming Languages  | Permalink |  Comments (0)
Martins Weblog
« January 2009 »
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Recent entries:
No Closures in Java 7?
iPhone bei simyo
Limited Visibility
To Type Or Not To Type - Functions in Scala and Groovy
Rational Numbers in Scala
More...
Categories:
Programming Languages (5)
Technology (3)
blogging (1)
fun (1)
model-driven development (1)
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: