Sign in with
Sign up | Sign in
Your question

Variable Types

Last response: in Applications
Share
February 12, 2012 5:24:47 AM

Hi, I'm trying to understand how an application written in either a compiled or interpreted language know its variable's type. I understand that with compiled languages you define the type in your code and in an interpreted language the interpreter decides the type of the variable based on the "value" you assign to it. What I really want to know is how, at run-time, does the program know what the variable's type is. My assumption at this point is that the variable has a reference to its type, if this is the case, where is the type stored?

Thanks in advance for any help.

More about : variable types

February 12, 2012 4:14:21 PM

There is no easy way to explain your question, since it varies on what level you observe the program.
You should take a better look at how compilers work.
http://en.wikipedia.org/wiki/Compiler
In short, the program has no idea what type the variable is. The compiler is the one that assigns the type to the variable and this is done in the syntax check pass. After the syntax passes corectly, the code is interpreted to whatever is needed for. Types are lost in the midle of the compilation since here it is all translated into some sort of intermidiate language (java byte code in java for instance or microsoft intermidiate language in C# and VB) that can be translated into assembly code during runtime (and that is why that code makes slower programs than C/C++ that is often directly translated into runtime program).
In the end all is binary instructions and data where the processor running the progam has no knowledge of types and just sees everything as instructions and memory adresses( be it registers or memory adress).

Hope that at least partialy awnsers your question
m
0
l
a b L Programming
February 12, 2012 4:30:01 PM

That's not entirely true for Object Oriented languages such as C# or Java, where the objects do actually know what type they are. You can determine this information by a process known as reflection. The type will be stored as a pointer to the class definition. Not only does this ensure that objects are assigned to variables of the correct type, but it also allows objects to know what methods they support (or what messages they respond to).
m
0
l
Related resources
February 12, 2012 4:49:53 PM

And yet partialy it is. I don't know about java, but in C# everything during runtime is interpreted as an universyl type "object" unless you define it as a generic type. Since the object class is the ultimate base class in .NET, everything is an object unless otherwise defined.
m
0
l
a b L Programming
February 12, 2012 5:06:14 PM

Not true. In C# all objects are indeed subclasses (perhaps at several removes) of Object, but they know what type they are. Hence the method GetType, which exists in all classes, that retuns the type of an object. This is a run-time call, not something determined by the compiler.

Without this polymorphism wouldn't work.
m
0
l
February 12, 2012 6:01:06 PM

GetType returns the type depending on the constructor used to construct the variable, not the type for variable definition. It is a runtime call the same as ToStirng method, so you can anticipate the object Type when handeling data send by the event.
But I think we got off topic a bit and are looking at the same thing from different points.
The questions were:
"What I really want to know is how, at run-time, does the program know what the variable's type is."
Awnser no. The program doesnt see types or object or anything like that, The object themselves have the data about themselves in their metadata (example GetType in C#)
"My assumption at this point is that the variable has a reference to its type, if this is the case, where is the type stored?"
Type is stored in the metadata of the class instance during construction.
m
0
l
a b L Programming
February 12, 2012 6:29:16 PM

You seem to have accepted that the types of objects are not all known at compile time but only at run time. And variables are just pointers to objects; you don't necessarily know what type of object they point to at compile time. That's good enough for me; variables have to know at run time what type (i.e. a pointer to what) they are at run time.

If not, explain polymorphism.
m
0
l
February 12, 2012 6:44:57 PM

Ijack said:
You seem to have accepted that the types of objects are not all known at compile time but only at run time. And variables are just pointers to objects; you don't necessarily know what type of object they point to at compile time. That's good enough for me; variables have to know at run time what type (i.e. a pointer to what) they are at run time.

If not, explain polymorphism.



I didn't accept it, this is true from the start.
I just didn't express myself right at the start, as I said, we were looking at the same thing the correct way from different points. I was trying to widely generalise things from a compiler point of view. Things can allways be interpreted in a number of ways depending what is your initial point of view.
m
0
l
February 12, 2012 7:11:16 PM

Thanks Ijack and theDanijel, this has been very helpful, and interesting!
m
0
l
!