4 ms·
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.
by pjungwir 14y ago
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.
- johnnyg 14y agoI scrolled to the comments section of this article looking for exactly that! Do tell!
- vanderZwan 14y agoMaybe somebody named his String variable string?
- RossM 14y agoIt'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.
- vineel 14y agoYeah, I don't think "string" is a thing in Java. Maybe he was comparing it to a data type from another language?
- wasd 14y agoHe might be referring to Integer versus int and Long versus long.
- dexen 14y agoIt 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#jls-4.3.3 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'?
- deleted 14y ago[deleted]
- eterm 14y agoMaybe 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.
- jakejake 14y agoMaybe 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!
- pjungwir 14y agoI 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?"
- mbrameld 14y agoYou 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.