0

I have an exam question from a past paper that I'm trying to answer:

Discuss variables of type primitive, reference and static in the context of a programming language. Give suitable examples [8].

The answer I have so far is:

A primitive type is an object which the language has given a predefined value. These types include int, bool and float. Reference type objects refer to these primitive types in a particular sequence when instantiated. Examples of these are strings and arrays. The static keyword, when assigned to a variable, means that there is only one instance of this variable and the value assigned applies to all references of the variable.

I'm fairly new to programming so I don't know if this is exactly right, so if anyone could give me some tips on how to improve the mark I would get for this question I'd greatly appreciate it.

Adam Higgins
  • 705
  • 1
  • 10
  • 25
  • 5
    *Reference type objects refer to these primitive types in a particular sequence when instantiated*. I would disagree. I can create a reference type that refers to nothing but other reference types, and its still a reference type. – Ron Beyer May 05 '15 at 20:05
  • So would i change it to: Reference type objects refer to other object types in a particular sequence when instantiated. – Adam Higgins May 05 '15 at 20:07
  • Possibly related: http://stackoverflow.com/questions/8790809/whats-the-difference-between-primitive-and-reference-types – Habib May 05 '15 at 20:07
  • 2
    *A primitive type is an object which the language has given a predefined value.* No, even a reference has a predefined value. There are no primitive types in C#... There are built-in types. There are primitive types in .NET. see http://stackoverflow.com/questions/16589111/primitive-types-versus-built-in-value-types – xanatos May 05 '15 at 20:07
  • 1
    @Habib No, value types in the CLR can be user defined (`struct`) – jdphenix May 05 '15 at 20:08
  • 1
    @jdphenix, yes, the linked question is related to Java' – Habib May 05 '15 at 20:09
  • 1
    *The static keyword, when assigned to a variable**field**, means that there is only one instance of this variable**field** and the value assigned applies to all references of the variable.* There is no need to *all references of the variable.*. The value of the static field is connected to the type. You can use it without an instance. – xanatos May 05 '15 at 20:10
  • 1
    Here there is the list of the builtin types of C#: https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx To those I would add the "special case" of `void` (`System.Void`) – xanatos May 05 '15 at 20:14

3 Answers3

1

You are on the right track for sure, but you are missing some fundamental concepts about these. Also, the 3 are not mutually exclusive:

A primitive type is simply a syntax shortcut defined by the compiler for Framework Class Library or FCL types.

A reference type is a pointer that represents an instance of a class. The objects they point to are allocated on the heap and the value of the variable is the memory address of that object rather than the class itself.

Static is not a type at all, but really defines where and when fields, properties, methods, and classes can be used. A static variable lives on the class rather then an instance. A static constructor is called the first time you access the class. A static method can be called from the class definition. That explains the persistence you see on static variables as you create and destroy them.

Matthew Frontino
  • 496
  • 2
  • 12
  • I'm going to sound a bit daft when I say this but what is the CLR – Adam Higgins May 05 '15 at 20:14
  • 1
    @AdamHiggins It is the virtual machine your C# code runs on. If you are familiar with the JVM, the concept is similar. – jdphenix May 05 '15 at 20:15
  • 1
    You're using the term "primitive type" incorrectly; you're talking about "build in types". – Servy May 05 '15 at 20:18
  • 1
    Reference types do not go on the heap. That is just as incorrect as saying value types go on the stack. Reference types are references. It so happens that, in C#, those references will always point to an object that exists in the heap. The value of the reference itself could be just about anywhere. – Servy May 05 '15 at 20:19
  • @Adam Higgins - Common Language Runtime, and no -- not daft at all. – jinzai Mar 03 '16 at 17:34
1

A primitive type is an object which the language has given a predefined value

Why? Even references can have predefined values as noted. For primitive (built in) types you may want to say these are types that a language provides built in support for. What your instructor might be glad to hear about is if you say that most primitive types are also value types in C# and you might want to discuss value types semantics (e.g., value type variable directly contains value - whereas a reference variable just contains an address to some object in memory).

About reference types again you may say that a reference variable doesn't contain the value or object directly - rather just a reference to it. Now again you may want to discuss reference semantics. For example if you have two reference variables pointing to same object - and you change the object from one reference change will be visible from another reference too - because both references point to same object. This is not the case with value types. If you assign same value type object to two different value type variables and change one variable - this change will not be visible in the second value type variable because each of them holds the value directly (e.g. each will have its own copy of the value type variable it was assigned to).

Static types you have already described.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • 1
    C# has no definition for the term "primitive type". You appear to be inventing your own definition, or using the term to refer to something else entirely (perhaps you mean "built in type"?). – Servy May 05 '15 at 20:17
  • 1
    @Servy:hmm I translated it all to C# point of view and assumed value type = primitive type? – Giorgi Moniava May 05 '15 at 20:19
  • @Servy Its just what the exam question says, should i just call it a built in type when answering then? – Adam Higgins May 05 '15 at 20:19
  • 1
    @AdamHiggins Yes, it is what the exam question says. It's not a sensible question. How you would choose to respond to that is up to you, so long as you realize that you're making incorrect statements in response to a question using terms inappropriately. – Servy May 05 '15 at 20:21
-1

The answer to that question, in my opinion -- has not a thing to do with OOP and everything to do with the compiler and microprocessor.

The simplest and most accurate definition of the term that subsumes all of the qualities of a primitive type -- as I understand it -- is:

A primitive type must fit into the register used for operations on it -- IOW, in an X86 system -- the Accumulator.

So, primitive types are limited to the size of the Accumulator and can be operated upon by native processor instructions. (Basic math and Boolean/bit-shifting operations). Yes, it fits into heap memory and on the stack, but those are still essentially 8-bit entities and the registers are not.

OOP languages do not use primitive types for their managed memory processes, they use structures that mimic primitive types. (Even in .NET, when you use the keyword int -- it uses System.Int32 to wrap that.)

jinzai
  • 436
  • 3
  • 9
  • Down vote with no explanation? Not very sporting of you. I consider my answer constructive, informed and correct within the context I wrote it. It adds to the discussion, whereas an anonymous down vote -- does not. – jinzai Mar 03 '16 at 22:13
  • Okay -- why did you dock me two reputation points for this unsubstantiated down vote? I am quite prepared to defend the comment -- and you have taken away my ability to comment anywhere by docking me two points instead of one. My argument is valid -- it actually includes floats, singles and doubles, as they are handled in the FPU, which is an 80 bit architecture. The .NET Decimal type is NOT a primitive type -- for that reason. Primitive type predates OOP as a term -- and in the C language standard, the processor architecture IS mentioned as a mitigating factor. – jinzai Mar 07 '16 at 22:41