1

When I read about Java, I generally see variables described as either primitive type or object type.

When I read about C#, I generally see variables described as either primitive type or non-primitive type?

What is the difference between the terms object type and non-primitive type?

Bill
  • 15
  • 4
  • 1
    http://www.dotnetfunda.com/articles/show/1491/datatypes-in-csharp – OldProgrammer May 28 '16 at 21:14
  • 1
    That article mis-identifies most of the primitive types, from [MSDN](https://msdn.microsoft.com/en-us/library/system.type.isprimitive(v=vs.110).aspx) - "The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single." – Preston Guillot May 28 '16 at 21:34
  • 1
    http://stackoverflow.com/questions/16589111/primitive-types-versus-built-in-value-types – Matías Fidemraizer May 28 '16 at 21:42
  • In Java, variables can only be primitives or references. You can't have variables which are objects. – Peter Lawrey May 28 '16 at 21:56

2 Answers2

5

Part of this confusion may be in that, in C#, (mostly) everything inherits from Object. To refer to an Object type in the same way, would refer to every type in the language, and essentially be useless.

In C#, the primitive types are Boolean, Byte, Char, Double, Int16, Int32, Int64, IntPtr, SByte, Single, UInt16, UInt32, UInt64, UIntPtr. These types still inherit from object, though they are treated differently by the language. There are a few types that don't inherit from object, but they are not what you would consider primitives (ie Interfaces). The list of C# primitives can be acquired with this code, taken from here:

var primitives = typeof(int).Assembly.GetTypes().Where(type => type.IsPrimitive).ToArray();

A more appropriate dichotomy, if you wanted such a thing, would be value types versus reference types. When you begin considering that difference, then you can include things such as Enum types, and other values type, like structs.

Community
  • 1
  • 1
Travis
  • 1,044
  • 1
  • 17
  • 36
0

in Java:

the primitive variables are categorized in 8 data types: boolean,byte,short,int,long,float,double and char.Every primitive variable has his own range of space in memory.

The references variables,refers to objects(Array,String,ArrayList,StringBuilder,...), and doesnt matter the space of the object referred.

Differences:

1.references types can be assinged as null /primitives dont.

2.references types can be used to call methods when they dont point to null/primitives uses literals.

3.references types have all the same size / in primitives depends of the
data type

4.primitives declarations starts with lowercase/java classes with
Uppercase

Mahb
  • 1