charAt vs startsWith
May 12, 2006 9:33 PM   Subscribe

Java: What is the difference between a String starting with a certain character, and a String having that character as its prefix?

An exam simulator asks me how to determine whether the first character of a String is 'A'.

This is a correct answer:

if(s.charAt(0)=='A'){}

...but this is an incorrect answer:

if(s.startsWith('A')){}

Why?
posted by bingo to Computers & Internet (19 answers total)
 
Is this multiple choice, or not? I don't see why the second one is wrong at all, so if it's not multiple choice, I think they might just have neglected to include that as a valid answer.
posted by evariste at 9:42 PM on May 12, 2006


Best answer: single quoted characters are a diffrent data type then double quoted characters, or double quoted strings of characters.

'A' is the same thing as the number 65.

"A" is a string contaning one letter, A, like "AA" is a string containing two letters.

"s.startsWith('A')"

Won't compile because 'A' is not a string.

"s.startsWith("A")"

Would work fine.
posted by delmoi at 9:43 PM on May 12, 2006


http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#startsWith(java.lang.String)

"Note also that true will be returned if the argument is an empty string..."

I'm guessing that .charAt doesn't match on empty strings.
posted by C.Batt at 9:44 PM on May 12, 2006


I'm guessing that .charAt doesn't match on empty strings.

Is this multiple choice, or not? I don't see why the second one is wrong at all, so if it's not multiple choice, I think they might just have neglected to include that as a valid answer.

God, don't answer the question if you have no idea what you're talking about. CharAt takes a char parameter, startsWith takes a String parameter.

To clarify:

WORKS:
if(s.charAt(0)=='A'){}
if(s.startsWith("A")){}

DOSN'T COMPILE:
if(s.charAt(0)=="A"){}
if(s.startsWith('A')){}
posted by delmoi at 9:47 PM on May 12, 2006


oops, C.Batt's aswer was correct-ish, but didn't directly address the poster's main problem.
posted by delmoi at 9:51 PM on May 12, 2006


holy crap delmoi... chill out. m'kay... I'm glad you gave the right answer. You can have the gold star. Sheesh.
posted by C.Batt at 9:51 PM on May 12, 2006


In Java, the second answer should only be slower if the library is badly coded (which it could well be, I don't do Java).

Well, maybe if it could compile, which it can't, because it's syntactically incorrect, as I outlined above...
posted by delmoi at 9:52 PM on May 12, 2006


delmoi is correct*, and Googling tells me that the exam name you tagged is indeed multiple choice, so that answers my question about that.

*vehemently so :-)
posted by evariste at 9:55 PM on May 12, 2006


Response by poster: According to the answers in the simulator, the second option I gave above is wrong because 'the startsWith method checks if a string starts with a specified prefix string, not a character."

c.Batt: it's not an empty string (is it?)

delmoi: I guess that makes sense, although if that's really the answer, it means that the answer given by the simulator is wrong (and I guess it is...).
posted by bingo at 9:56 PM on May 12, 2006


Response by poster: ...or maybe the simulator has it right, and their explanation is so weirdly written that I'm not connecting it to what delmoi said...?
posted by bingo at 9:57 PM on May 12, 2006


bingo-a string is a different type than a char, apparently. When you see the phrase "prefix string", what you should take from it is "string".
posted by evariste at 9:59 PM on May 12, 2006


So, they have it right, and they wrote it weirdly.
posted by evariste at 9:59 PM on May 12, 2006


Response by poster: Ok, I get it now. I read that answer 20 times, but I could never figure out what they were actually saying. Now that I know, it seems obvious. Thanks, delmoi, and to all for contributing.
posted by bingo at 10:08 PM on May 12, 2006


1. delmoi gave the correct answer

2. I should've checked the method param types

3. A single character enclosed in ' (single quotes) is a character; 0 to [big number] of things enclosed in " is a string and they are quite different (where a string is a vector (linked list) of chars, iirc wrt Java)

4. My interpretation of the docs is probably quite wrong - my thinking was that s.startsWith("A") would match if (s == ""), which doesn't make any sense at all.

I'm going away now :-)
posted by C.Batt at 10:08 PM on May 12, 2006


3. A single character enclosed in ' (single quotes) is a character; 0 to [big number] of things enclosed in " is a string and they are quite different (where a string is a vector (linked list) of chars, iirc wrt Java)

Vectors are backed by arrays in Java, not linked lists. When you create a vector it has some extra space at the end, and once you fill that internal space, it (I guess) copies the array to a new one with more empty space at the end, or does something else. I'm not sure how java's Strings are actually implemented, but I think they actually use static arrays. In fact, strings are immutable, and every time you change one you create a new string, and the old one is left to be garbage collected, if nothing else points to it.
posted by delmoi at 10:30 PM on May 12, 2006


What delmoi says about Strings is correct. Java treats strings as immutable objects once defined, which is why most devs will hold their noses when they see strings concatenated in the 'scripting" way (plusses). They might then tell you to use a mutable class like StringBuffer if you wish to build your strings, but in practice this is overkill (thread-safe string concatenation is kinda silly when you're usually doing your manipulations in a single function). Which is why Sun went ahead and designed the StringBuilder type.
posted by Civil_Disobedient at 2:42 AM on May 13, 2006


I'd like to add that this is a thoroughly stupid exam question. If you ever confuse the two, the compiler will tell you so and you get to fix it in two seconds.

Knowing method signatures by heart isn't a qualification for any kind of IT work I know.
posted by themel at 3:07 AM on May 13, 2006


Agreed, themel. In fact, just about any semi-intelligent IDE will prompt you Microsoft-squigly-underline-style that you've made a syntactical error before you even hit the compiler.
posted by Civil_Disobedient at 4:11 AM on May 13, 2006


Response by poster: For the record, the question is from the WhizLabs SCJA simulator. I don't know whether it accurately reflects the sort of questions that are on the exam. However, the exam does require you to know about certain details of the String class, including these particular methods.

Interestingly, the Bates/Sierra SCJP (different exam) study guide speaks a bit to Civil_Disobedient's point about the immutability of Strings, but also blows off the distinction between StringBuffer and StringBuilder as not really worth knowing about (for the exam, anyway).
posted by bingo at 9:31 AM on May 13, 2006


« Older Equivalent of CDOSYS with Apache?   |   Help me be a temp-er Newer »
This thread is closed to new comments.