This is post is to find some interesting stuff with the java string class and its behavior with the empty string. Here is the code snippet and with explanation for the output in each cases.
1. System.out.println("Karthic".indexOf("K"));
Prints: 0, as expected the functions returns the index of the last occurrence of the target string in the source string. In this case it is zero.
2. System.out.println("Karthic".indexOf("k"));
Prints: -1, as expected the function returns -1 when the target string is not found in the source string.
3. System.out.println("Karthic".indexOf(""));
Prints: 0, Now comes the interesting part. This at first glance looks very similar to the case 2 as there seems to be no empty string in the source string. But that is not the case. Everybody (even in object world) starts their life with nothing and keep on adding different thing as the life goes on. String element is initialized by the compiler with an empty string and keeps on adding the character to it.
Anyway I went to the source code of the String.class and here is what I found. If the target string length is 0 then it return the fromIndex which is 0. (see code below). It is strange, they should have return -1.
if (targetCount == 0) { return fromIndex; }
public int indexOf(String str, int fromIndex) { return indexOf(value, offset, count, str.value, str.offset, str.count, fromIndex); }
static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; }
Well, can you prove your String doesn't begin with an empty string? :-)
String name = "Karthic"; String name2 = "" + "Karthic"; System.out.println(name.equals(name2)); // true
The occurrence of "" empty string is being assumed at left most and right most positions of a string as '0' and string.length() . And the implementation stands on this assumption. So the result for the following calls would be -
"Karthic".indexOf("") -> 0 "Karthic".lastIndexOf("") -> 7
Try the following example to understand better what is going on:
String name = "Karthic"; name = name.replace("", "."); System.out.println(name); // prints: .K.a.r.t.h.i.c.
Actually between 2 subsequent characters there is nothing means its an empty string coz empty string denotes nothing . for e.g. Karthic string between K and r we can say a is there but between K and a we will say 'nothing' is present that is empty string "" is present
if you try "Karthic".split("") ypu will get "", K, a, r, t, h, i and c & length is 8
first empty string is like start of string to first char i.e. K
int i = 0; for (String s : "Karthic".split("")) { System.out.println(++i + " : " + s); }
1 :
2 : K
3 : a
4 : r
5 : t
6 : h
7 : i
8 : c
So, we have the "rule" that says every string begins with nothing :)
Of course every string ends with nothing as well.
But let`s think about why the rule does not apply in this case. First of all think of what would happen if the compiler will just keep on going and displaying nothing? Think that the "" is not actually nothing because "" is not NULL. "" means allocated memory. The compiler ignores the "" if it is not followed by anything but nothing.
So there is a "" at the end of the string but it is not followed by anything and there is no use to waste memory on another "".
Finally " " does not mean an empty string. This is a string with space in it.
System.out.println("Karthic".indexOf(" ")); // prints: -1
This is very much same as case 2.
Comments on this blogs are welcome for further discussion.
very interesting one ... thank you... keep going the good work..
ReplyDelete