String is data type or not | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

String is data type or not

25th Nov 2017, 1:59 PM
Ram Choudhary
Ram Choudhary - avatar
8 Answers
+ 3
Spam>> I'm very convinced you know the answer to this so stop spamming the Q&A
25th Nov 2017, 4:04 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 3
Why so much hate for this question? 😜 Java supports two data types: primitive data types and reference (object) data types. A reference data type, by contrast, is also considered a non-primitive data type, but a data type no less. In the literal sense of the question being asked, the answer would be: Answer: Yes. String is a data type. Perhaps, the use of the word "data type" was meant to imply "primitive data type." If so, my answer would be: Answer: No. java.lang.String is a class type, which is a reference (or object) data type. Taken from: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html "The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such." @Ram: For a more comprehensive review of Java data types, take a look at the details in these links: - https://stackoverflow.com/a/16600781/5087494 - https://www.sitepoint.com/beginning-java-data-types-variables-and-arrays/ - Scroll down to the section titled "Data type" - https://www.tutorialspoint.com/java/java_basic_datatypes.htm - https://medium.com/omarelgabrys-blog/primitive-data-types-in-c-vs-java-5b8a597eef05#cdcb - https://chortle.ccsu.edu/java5/Notes/chap26/ch26_2.html
30th Nov 2017, 2:56 AM
David Carroll
David Carroll - avatar
+ 2
@Martin: I actually meant to add my 😜 emoji to the first line in that post with the intent of making it more "tongue-in-cheek". It was, more or less, a general commentary to what I originally _sensed_ to be a collective "tone" of frustration from various responses and their corresponding upticks. When this was called out as "spam" and seconded by another person, I thought maybe @Ram had a history of posting spam. After seeing this was his first Q&A question, I thought maybe the outcry reflected a recent surge of similar questions I was unaware of. Interestingly, I can see now that the initial spam comments set a different tone for how I read your answers, which came off a bit harsh and pedantic in that context. As uncharacteristic as I thought that was for you, I assumed it was valid for reasons unknown to me. Hence, my question... "Why all the hate? 😜" While revisiting your answers, with all this in mind, the tone completely transformed from one I mistook as condescending to one that is helpful and informative. It's amazing the impact context can have on tone. On a side note, I am fighting off the flu and wonder if that has anything to do with it. 🤧 Apologies for any unintended drama.
30th Nov 2017, 5:01 AM
David Carroll
David Carroll - avatar
+ 2
@Martin... You bring up an interesting point on whether or not the countless class types (existing or yet to be created) should be considered distinct data types. I'm going to go out on a limb and make a wild guess that you have deeper roots in Java than you do in C# (or rather .NET). Otherwise, the idea of correlating class types as distinct data types would likely be your default position. It certainly is the case for me. 🤓 Let me explain with a little more background. -------------------- As we've already established, data types in Java are categorized as either primitive types or reference types. The 8 primitive types in Java are value types and have no class definition. Each of these primitive types have corresponding wrapper classes. Example: (in Java) - The wrapper class for the primitive data type `int` is `java.lang.Integer`. As you stated earlier, these are not the same. This is very different in .NET. -------------------- For starters, data types in .NET are categorized as either value types or reference types: - Value Types: structs and enumerations - Reference Types: classes, interfaces, and delegates In .NET, primitive types are defined as struct types, which are very similar to classes. If you take a closer look at an `int` in C#, you will see that it is actually a keyword alias that maps to the `System.Int32` struct type. Therefore, `System.Int32` is not an object wrapper for `int`. Rather, they are one and the same. Structs fall into 3 categories in .NET: - Numeric types (Integral Types, Floating Point Types, and decimal) - bool - User defined structs Wrapping Up: -------------------- Since primitive types, in .NET, are very similar to class types, it is very natural to consider any of the following to be distinct data types: - System.Int32 (or int) - System.String (string) - System.Object - System.[Anything] - Foo.SomeClass - Bar.SomeStruct - Baz.SomeEnum - Qux.SomeDelegate - Thud.SomeInterface - Blah.YouGetThePoint
30th Nov 2017, 6:48 AM
David Carroll
David Carroll - avatar
+ 2
@Nicky... The confusion here is whether or not the question assumes "data type" to mean primitive data type or can it be considered a reference data type. My more detailed answer explains that from a .NET perspective, the concept of primitive data types carry a different meaning from that of Java. I imagine the context of this question is slightly different depending on one's background being stronger in .NET or Java. I tried looking for a definitive position on this from Java documentation. However, it's not clearly or consistently stated. In fact, there are numerous references where the term "data type" is equally applied to both primitive and reference types. Due to this ambiguity, I definitely see this as a valid question. However, it would be great if @Ram, the poster, would clarify the context of what he meant with the meaning of "data type" here. At the very least, I've certainly learned more about the subtle differences of how people interpret this question. 🙄
3rd Dec 2017, 12:37 AM
David Carroll
David Carroll - avatar
+ 1
Yes, String int boolean float double char long and many more.
25th Nov 2017, 2:03 PM
Momo Belia
Momo Belia - avatar
+ 1
yep
28th Nov 2017, 11:24 AM
Hasintha Lahiru
Hasintha Lahiru - avatar
0
yup
25th Nov 2017, 4:25 PM
Bijesh Basnet
Bijesh Basnet - avatar