17 comments

[ 6.1 ms ] story [ 52.9 ms ] thread
What is the difference between a string and a String? I've written Java for 15 years, and I've never heard of a lowercase-s string before.
I scrolled to the comments section of this article looking for exactly that! Do tell!
Maybe somebody named his String variable string?
I think this is probably it. It's the only thing I can think of.
It's been a while since I worked on some Java but I believe it has something to do with one being a type (like int) and the other being an instance of the class String. I was always taught "just use String" but I'd love to know the real difference.

Google is not proving fruitful for this question.

Yeah, I don't think "string" is a thing in Java. Maybe he was comparing it to a data type from another language?
He might be referring to Integer versus int and Long versus long.
It seems there isn't. There is only one `String' datatype in Java, instance of class String, and no primivite `string' type. String literals represent instances of class String, as per http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#...

Perhaps author meant JavaScript, which does have primitive `string' type, distinct from instances of class `String'?

(comment deleted)
Maybe he's anonymised his experience slightly by subbing Java for C#: C# has a string and a String, although string is just an alias to System.String, so they're really the same.

It's not terribly confusing, although syntax highlighters do treat them differently which I could see could confuse the uninitiated.

Maybe the answer is that String is a class and string has no special meaning in Java?

It could be that the author was shown a bit of code where an instance of a String was called string (I've seen this many times as an argument name especially in methods for converting or manipulating a String value). But since the code wasn't mentioned we have no idea,

Seems like a nasty trick question to ask of an intern who doesn't know Java!

I agree if it was asked with no context ("What's the difference between String and string?"), I wouldn't be sure how to answer either! (But in the OP, wasn't it the author who asked the question?)

It reminds me of an interview question I was asked once (thought not as bad as String/string): "What is the time complexity to print every node in a binary tree?"

You probably know this, but I think the author is confusing a string literal with a lowercase string.

String a = "hello";

String b = "hello";

a == b will evaluate to true.

String a = new String("hello");

String b = new String("hello");

a == b will evaluate to false.

String literals in Java are put in to a string "heap" (might not be the correct word) and reused. Strings objects create a new instance for each string.

The willingness to admit lack of knowledge separates the great from the good.
I've been writing software for 10 years now and still regularly feel out of my depth. I feel that if I'm not periodically jumping into the deep end of something then I'm stagnating.
Getting thrown into the deep end is the best part about programming, IMO. It keeps me on my toes and constantly learning new things.