Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 06, 2016, 02:21:23 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: What Every Computer Scientist Should Know About Variables  (Read 1668 times)

rkarman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 27
    • View Profile
    • Arca Eclipse (chatserver for ares)
    • Donate to Member
What Every Computer Scientist Should Know About Variables
« on: August 15, 2006, 12:18:32 PM »
i'm sure many of you think this is an obvious story, but i'm going to tell it anyway for all who didn't know yet :-p


there are 2 types of variables in programming, one is called a value type the other is called a reference type. very nice that you have them both, but what does it actually mean? well, a value tpye variable is actually quite easy to explain, it's just a variable that can hold a value. then the question comes, what is a reference type variable then? well a reference type variable is a variable that has a value (of course) and also a reference to itself.

now the reason why we need 2 types of variables becomes clearer when you understand what happend when a variable is passed to a method or function.

lets look at the normal situation first:
when you pass a value type variable to a method, the actual value of the variable is copied into the function. so the function gets a new copy of the value to work with. this means that if you change the value of the variable inside your method, the origional value of the variable outside your method is not changed at all.
when you pass a reference type variabel to a method, the reference to the variable is copied into the function. so the function gets a new copy of the reference, but not of the value. this means that if you change the value of the variable inside your method, the origional value of the variable outside your method is changed.

now you can also pass variables "by value" or "by reference" in some languages. usualy this mean that reference type variables are always passed by reference, even if you declare the method to pass "by value".
for a value types it means that a reference can be created on the fly when you pass the variable "by reference"

now there is one more tricky thing about all this reference and value business, if you want to know if changing your variable inside a method will change it outside the method too then you need to know about something we call "immutable variables" too (yes i swear we are done after this one). basically when a variable is immutable it means that it can not change. of course it's not the same as a constant though, because you can make a new copy of this type of variable and change the reference to point to this new copy. this way it looks like your variable changed, and it also looks like other references (that did not get updated) still have the old variable value (in fact they have the complete old unchanged variable).

phewww, i bet some of you need to read that 2 times :-P


so now some ways these are all used
value type: usually has small data (like numbers) and can not be NULL, stucts/stuctures
reference type: usually has lots of data (like strings), classes
immutable: usually normal reference type variables

not in all languages this is the same though, just in most. C for instance has the char type which is basically a string like type, it is not immutable however (this is one of the reasons why C is called a low level language by some)