views:

881

answers:

8
+34  A: 

"" is an empty string containing no characters. There is no "empty character", unless you mean a space or the null character, neither of which are empty strings.

You can think of a string as starting with an infinite number of empty strings, just like you can think of a number as starting with an infinite number of leading zeros without any change to the meaning.

1 = ...00001
"foo" = ... + "" + "" + "" + "foo"

Strings also end with an infinite number of empty strings (as do decimal numbers with zeros):

1 = 001.000000...
"foo" = "foo" + "" + "" + "" + ...
Cameron
+1 nice words..
org.life.java
To make the analogy line up fully you should use 1.0, that way adding zeros to the end has no effect as well. :)
Hostile Fork
@Hostile Fork: Good point, editing.
Cameron
Actually, beginning a `String` with a `0` in Java produces an entirely different effect. See - http://stackoverflow.com/questions/1705645/java-int-division-confusing-me
jjnguy
`0200 != 200` in Java.
jjnguy
@Justin, I believe Cameron was referring more of an abstract concept (i.e. in mathematics 1 = ...0000001 = 1.00000000...) and so forth. One hint is "foo" = "foo" which is illegal in Java, for example :) And he would actually have used "foo" == "foo" also...
Yanick Rochon
@Justin: Well yes, in programming languages, prefixing an integer with a `0` generally means that the number is being expressed in octal (not decimal). However, I was using numbers as an analogy, not to express proper Java code ;-) Thanks for pointing this out! Along the same lines, `=` can't be used for comparison in Java either.
Cameron
@Cam, touche...
jjnguy
@Hostile Fork, adding 0's the the end is incorrect as 1.0 is not the same 1.00. The training 0's indicate precision. 1.01 can be rounded and written as 1.0, which is not equal to 1.00.
Steve Kuo
Leading zeroes make no difference, but my old chemistry professor made a big deal about trailing zeroes... called them significant figures or some other similar silly thing :)
BigMac66
@Steve - Though it's shifting into pure math, context of the example here is also Java, and AFAIK (just as in pure math) it wouldn't react to the significant figures. Should translate to the same IEEE-754 bits, double precision. Which I think is 0100000000000000000000000000000000000000000000000000000000000000
Hostile Fork
@Steve, @BigMac66: I actually just meant for the context of my answer to be common sense (with a bit of math backing it), not Java specifically, and certainly not taking into account significant digits. There is no rounding in my numbers, so significant digits shouldn't make a difference anyway. 1.0 _is_ equal to 1.00, if no rounding takes place (which is the case here).
Cameron
Honestly though, the argument about significant figures has no bearing on mathematics anyway. Your significant figures are based on the precision of measurements, which is not a concept used in most math. The standard view in math is that 1 = 1.0 = 1.00 and so on.
Zoe Gagnon
Also there are an infinite number of empty strings between any two adjacent characters. This is not the case in your analogy. An empty string has zero characters, so you can insert it anywhere without changing the original string. And because of that, the empty string is in fact present at any index in a string (and an infinite number of times). Its just not visible because it has no characters.
Gnafoo
@Gnafoo: Yes, good point. My number analogy only stretches so far :-)
Cameron
@Cameron: I still liked it ;-). I thought of the following analogy: the empty string is a bit like liquid glue. You put something on the left and right side, to glue the string to the quotation marks. And you need glue between the characters, to keep the string from falling apart. And like glue, the empty string is an indefinite mass, you can't count it, but you can always split it apart into more empty strings.
Gnafoo
It should be noted that the behavior of `String.startsWith("")` returning `true` is more of a convention chosen by the developer [community], than the result of a comparison algorithm (while some implementations may produce that result). This is a choice I would have made also (it is common sense imo) but other implementations may decide to return `false` because *there is nothing to compare*.
ring0
+5  A: 

That "" is not a blank it's an empty string. I guess that the API is asking the question is this a substring of that. And the zero-length empty string is a substring of everything.

djna
+11  A: 

Seems like there is a misunderstanding in your code. Your statement s.startsWith("") checks if string starts with an empty string (and not a blank character). It may be a weird implementation choice, anyway, it's as is : all strings will say you they start with an empty string.

Also notice a blank character will be the " " string, as opposed to your empty string "".

Riduidel
Funny to hear Java guys call spaces "blanks". What is a tab, may I ask, is that a "blank" too?
PP
I don't think it's weird at all. As a matter of fact I can't come up with any sensible definition of how `startstWith` should behave, that would have `s.startsWith("")` return false, without treating `""` as a special case.
sepp2k
@sepp2k I don't really think it's weird. In fact, personnally, the only implementation I can come up for this method is considering the result is true, then iterating over characters to ensure it is true. So for an empty string, result is logically true.
Riduidel
@PP - don't be picky. 1) A lot of SO folks don't have English as their first language. 2) Blank is an acceptable alternative to space. 3) You know what he / they mean ... really.
Stephen C
@PP - What are "Java guys"?
Ishtar
@Stephen_C there seems to be some kind of slang attached to the use of "blank", no ? Can you unobfuscate it ?
Riduidel
@Ishtar - "Java guys" are people who, typically, will be complete newbies at programming - as can be evidenced from their code.
PP
@PP I prefer to be your newbie than anything else, as you seems to "Believes good developers have acquired humility" :-) (never give others the stick to be beaten :-D)
Riduidel
@PP Just because Riduidel calls the space character a "blank" doesn't mean most Java guys do so
Steve Kuo
+2  A: 

A blank is (" "), that's different from an empty string (""). A blank space is a character, the empty string is the absence of any character.

Nathan Hughes
Funny to hear Java guys call spaces "blanks". What is a tab, may I ask, is that a "blank" too?
PP
@PP: I was using your terminology, it probably is more accurate to refer to spaces and tabs.
Nathan Hughes
+2  A: 

The empty String ("") basically "satisfies" every string. In your example, java calls

s.startsWith(""); 

to

s.startsWith("", 0);

which essentially follows the principle that "an empty element(string) satisfies its constraint (your string sentence).".

From String.java

/**
     * Tests if the substring of this string beginning at the
     * specified index starts with the specified prefix.
     *
     * @param   prefix    the prefix.
     * @param   toffset   where to begin looking in this string.
     * @return  <code>true</code> if the character sequence represented by the
     *          argument is a prefix of the substring of this object starting
     *          at index <code>toffset</code>; <code>false</code> otherwise.
     *          The result is <code>false</code> if <code>toffset</code> is
     *          negative or greater than the length of this
     *          <code>String</code> object; otherwise the result is the same
     *          as the result of the expression
     *          <pre>
     *          this.substring(toffset).startsWith(prefix)
     *          </pre>
     */
    public boolean startsWith(String prefix, int toffset) {
    char ta[] = value;
    int to = offset + toffset;
    char pa[] = prefix.value;
    int po = prefix.offset;
    int pc = prefix.count;
    // Note: toffset might be near -1>>>1.
    if ((toffset < 0) || (toffset > count - pc)) {
        return false;
    }
    while (--pc >= 0) {
        if (ta[to++] != pa[po++]) {
            return false;
        }
    }
    return true;
    } 
The Elite Gentleman
+7  A: 

"Hello" starts with "" and it also starts with "H" and it also starts with "He" and it also sharts with "Hel" ... do you see?

invariant
+3  A: 

For folks who have taken automata theory, this makes sense because the empty string ε is a substring of any string and also is the concatenation identity element, ie:

for all strings x, ε + x = x, and x + ε = x

So yes, every string "startWith" the empty string. Also note (as many others said it), the empty string is different from a blank or null character.

NullUserException
A: 

An empty string is not a blank character. Assuming your question with empty string, I guess they decided to leave it that way but it does seem odd. They could have checked the length but they didn't.

fastcodejava