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.
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.
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.
17 comments
[ 6.1 ms ] story [ 52.9 ms ] threadGoogle is not proving fruitful for this question.
Perhaps author meant JavaScript, which does have primitive `string' type, distinct from instances of class `String'?
It's not terribly confusing, although syntax highlighters do treat them differently which I could see could confuse the uninitiated.
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!
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?"
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.