I have been trying to understand the use of "primitives" in Java and C# and the difference between them (if any). I have asked a series of questions on SO and some of the answers seem to confuse the issue rather than clarify it. Some answers (and some MS documentation) appear to provide contradictory statements. From SO
- http://stackoverflow.com/questions/1599176/what-are-first-class-objects-in-java-and-c
- http://stackoverflow.com/questions/1597999/are-primitives-different-in-java-and-c
and from MS: http://msdn.microsoft.com/en-us/library/ms228360%28VS.80,lightweight%29.aspx
- "structs are very similar to classes"
- "the Int32
class wraps the int
data type"
- "On the other hand, all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided. For instance, int
is the short name for System.Int32".
My confusion lies largely with C# (I have programmed java for some while).
EDIT The following paragraph has been confirmed to be correct by @Jon Skeet
- Java has two types (primitive and class). The words "value type" could be a synonym for primitive (although not widely used) and "reference type" for class. Java "wraps" primitives (int) in classes (Integer) and these classes have the complete power of any other class (can be null, used in collections, etc.)
EDIT @Jon has given a very clear statement on C# so I will delete my suggested truths and refer to his answer.
Further question: Is there a consensus on what the actual use of these terms should be? If there is a consensus I'd be very grateful for it spelt out explicitly. Otherwise I assume the terminology is muddled and therefore of limited use.
SUMMARY Thanks for very clear answers. There is a consensus (see accepted answer from @Jon) among those who really understand this and the MS docs are consistent (although they refer to Java in places and I misread these for C#)