June 2, 2005

Strange String method behavior in java

I've got the feeling that Java is not really *write once, run everywhere anymore* for just ONE method in the java.lang.String class: the java.lang.String.replace(CharSequence target, CharSequence replacement) method. I found out that using this method with the same JDK version on both Microsoft Windows and Mac OS X (Jaguar - with JDK 1.5.0_02 as well as previous versions - Panther with JDK 1.4.2-50) on the first case lets one compile his / her class and on the second case doesn't compile. Have a look at the following code:

public class Test {
public static void main(String[] args) {
String str = "MISSISSIPI";

// Using replace(CharSequence target, CharSequence replacement)
// This method returns a new String resulting from replacing all
// occurences of oldChar in the concerned String with newChar.
// Please have a look at the Java APIs doc for more info on this.
System.out.println(str.replace("S", " "));
}
}

On compiling the above-listed code on my Windows XP machine, it compiles and runs fine. On compiling the same code (THE SAME) on my OS X machine, I get the following compile error:
Test.java:8 replace(char, char) in java.lang.String cannot be applied to (java.lang.String, java.lang.String), etc...

So someone tells me what is happening here. There are 2 replace methods in the String class; there is String.replace(char, char) and String.replace(CharSequence, CharSequence). From my understanding, the latter has not been implemented at all on the OS X JDK: does that make sense at all or I'm an idiot? For some reason, I wouldn't have known this if I hadn't tried to compile my project code on OS X because the primary development environment was MS Windows.

Now, let's move a bit further in finding out what the problem is. Write once, run everywhere right? So I can basically take the Test class bytecode from my Windows machine (since it compiled successfully there) and try to run it on OS X right? Well I tried and it gives me the following runtime error:

Exception in thread "main" java.lang.UnsupportedClassVersionError: Test (Unsupported major.minor version 49.0) etc....

Basically, this is the kind of error when a class has been compiled with a non-compatible version of the VM. But wait a minute! Any other classes (as long as they're not using the String.replace(CharSequence, CharSequence) run / compile pretty fine!!

HellloooO? Can I get some help here! Any useful comments will be more than WELCOME.

3 comments:

  1. You either use

    System.out.println(str.replace('S', ' '));

    or

    System.out.println(str.replaceAll("S", " "));

    ReplyDelete
  2. Sorry, my above post refer to JDK 1.4 (1.4.2_07 in particular), which don't have the method replace(CharSequence target, CharSequence replacement).

    But if you're using JDK 1.5 it should work. I've tested with JDK 1.5.0_06 on Linux and Solaris.

    ReplyDelete
  3. hi, check out ur java version by

    %java -version

    using terminal, is it JDK 1.5?

    Or check out ur IDE preference,
    it works for me using xcode.

    ReplyDelete