5 ms·
Out of My Depth
- pjungwir 13y agoWhat 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 13y agoI scrolled to the comments section of this article looking for exactly that! Do tell!
- vanderZwan 13y agoMaybe somebody named his String variable string?
- RossM 13y 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 13y agoYeah, I don't think "string" is a thing in Java. Maybe he was comparing it to a data type from another language?
- wasd 13y agoHe might be referring to Integer versus int and Long versus long.
- dexen 13y 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 13y ago[deleted]
- eterm 13y 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 13y 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 13y 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 13y 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.
- maresca 13y agoThe willingness to admit lack of knowledge separates the great from the good.
- myleshenderson 13y agoI'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.
- qanael 13y agoGetting thrown into the deep end is the best part about programming, IMO. It keeps me on my toes and constantly learning new things.