0

I am not 100% sure if this is a relevant question around here, but I find this thing interesting.

When splitting a string into an array of strings in Java like this:

String[] array = "yolo".split("");

The implementation in Java 7 and Java 8 is a little bit different.

In Java 8: array.length is 4 and the array looks like this ["y","o","l","o"]

In Java 7: array.length is 5 and the array looks like this ["","y","o","l","o"]

Is this a bug that wasn't fixed until Java 8 (I seem to remember that you would get Java 7 result in older versions of Java aswell)?

Shouldn't code like this be backward compatible, which means that the method should return the same result in Java 7 and Java 8?

4
  • I voted to close this question as duplicate but if you feel that it doesn't fully cover your question inform me about it so I could open it or maybe improve already posted answer. Commented May 13, 2014 at 22:21
  • Anyway "Is this a bug that wasn't fixed until Java 8" I wouldn't call it a bug since it was expected behaviour. What Java8 did was improvement. "Shouldn't code like this be backward compatible" generally it should, but since noone really wanted empty "" as first result of split on "" (or other zero-width regexes) people in 99.9% of cases ware either removing this first element, or trying to avoid creation of such result by splitting on something like (?<!^) - this will check if empty string we are splitting on is not at start of input. Commented May 13, 2014 at 22:29
  • I totally agree that it is an improvement :) Bug wasn't the right word i guess. This means that one might get some problems running code from the Java7-era on Java 8. Commented May 13, 2014 at 22:37
  • I assume that Java creators think that combined time spent on solving this problems will be ridiculously smaller than time earned thanks to this change :) Commented May 13, 2014 at 22:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.