Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Variables -- why is typing them in their declarations important?

  1. Sep 16, 2014 #1
    Hiya! Im a beginner in programming and im now having a java course in college... In the first few lessons, we are taught that programming involves declaration of variables and variables are classified into several categories (e.g. int, float, boolean, double, etc.). I wonder why we have to classify the variables. For example, why do we separate int and float? why not we put them into the same category,let say "number"? This question may sound stupid.. but as a beginner, i am really not clear about how the computer works

    Thank you so much!
     
  2. jcsd
  3. Sep 16, 2014 #2

    berkeman

    User Avatar

    Staff: Mentor

    Welcome to the PF.

    Declaring the type of each variable helps the compiler to do error checking, and also helps the compiler allocate memory for each variable.
     
  4. Sep 16, 2014 #3

    rcgldr

    User Avatar
    Homework Helper

    The typing of variable is needed for most programming languages. This avoids having to dynamically convert variables in memory, and if the variable type becomes larger, having to allocate memory, copy and covert the variable, then release the now unused old copy of the variable. Some languages, such as APL, which is interpretive, do perform dynamic typing of variables at the moment they are assigned values or updated.
     
    Last edited: Sep 16, 2014
  5. Sep 16, 2014 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    The reason for this will probably become more "obvious" as you learn more about the language, including how to define your own "types" of variables, and your own operations on them. Then, it's a big advantage that the compiler can tell when you accidentally write something meaningless, rather than your program just crashing when you run it.

    There are two different sorts of reason why int and float are different types of variable. One is that they are stored in different ways in the computer's memory, and they can be used to store different ranges of values. The other is that you can use ints for "counting" or "numbering" things, where a value that isn't an integer doesn't make sense.

    For example is you have a string of characters, you can access individual characters using their "distance" from the start of the string (0 = the first character, 1 = the next, 2 = the next, etc) but it doesn't make any sense to say "What is the value of character number 3.728" or "Set character number 0.5 to A".
     
  6. Sep 16, 2014 #5

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Variable typing is also a hold-over from the days when computers had tiny memories. If you have a boolean type variable, where the only possible values are 'true' and 'false', it makes little sense to allocate 2 bytes of memory for each type boolean variable. OTOH, a float could occupy either 4 bytes or 8 bytes of memory, depending on the precision desired, so you wouldn't want your integer or boolean variables being the same size as the floats: it would mean too much wasted memory.
     
  7. Sep 20, 2014 #6

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    What you have seen is just the tip of the iceberg. Java is a very powerful, modern language. There are a lot of types that a computer variable can be (complex number, matrix, data file entry, etc). And each type should be used and interpreted differently. The computer can interpret " x = a * B " differently, depending on whether x, a, and B are reals, complex numbers, matrices, etc. You can define your own types and tell the computer how to operate on that type in different circumstances. The line of code "email = formA * friend_name" could be multiplication of two numbers. But it could be telling the computer to get email template formA. fill it in with information (email address, first name, last name, etc.) of your friend identified in friend_name, and email the result to that friend.
     
  8. Sep 20, 2014 #7

    jtbell

    User Avatar

    Staff: Mentor

    If you're not required to declare variables, when you misspell a variable name it silently creates a new variable instead of using the one you intended to use. This often caused bugs in my programs when I was learning Fortran many years ago. Then I found out about the IMPLICIT NONE statement which forces you to declare all variables, and I used it religiously after that.
     
  9. Sep 20, 2014 #8

    phinds

    User Avatar
    Gold Member
    2016 Award

    Yeah, I've been using VB since it came out and it took me a while but I finally got used to making sure that all my modules start with the "option explicit" statement which does the same thing. In VB.NET it has become the default to it doesn't have to be declared all the time. I think all compilers should have that as the default with the option to turn it off it if you want, although I'm not clear why you would ever want.
     
  10. Sep 20, 2014 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    There's a lot more to variable typing than just limiting memory consumption. Specifying the type of a variable specifies the "rules of the road" for that variable. For example, 1+1=2 in the reals and the integers, but 1+1=1 in boolean algebra. Another example: 5/2=2.5 in real and rational arithmetic. This expression is undefined in integer arithmetic. Most computer languages give that integer expression a meaning, and in those languages 5/2 is typically equal to 2 rather than 2.5.

    Another aspect of type specification pertains to languages such as Java (this question is about Java) that allow overloading. Overloading means that multiple member functions can have the same name but different argument lists. Suppose class Foo defines two overloads of member function do_something. One overload takes an integer as an argument, the other, a floating number as an argument. The first overload will be called for foo_instance.do_something(42), but the latter will be called for foo_instance.do_something(42.0).
     
  11. Sep 20, 2014 #10

    nsaspook

    User Avatar
    Science Advisor

    This is true but variable typing of integers is important when programs interface with memory-mapped hardware (not something you would use a language like Java for with no way to access addresses of variables but most other languages do have this capability Address of a variable). A typical hardware interface register might have a configuration register of 16-bits, a status register of 8-bits and a I/O register of 32 bits in a contiguous memory space so having 8-bit byte,16-bit short,32-bit int types enable you to define and address only the bits you need to read/write/modify within that interface without effecting the status of the other bits. When using language like C you can make structures of the primitive types so one variable can be used to define and access the interface in a modular manner.
     
  12. Sep 20, 2014 #11

    mathman

    User Avatar
    Science Advisor
    Gold Member

    One distinction between int and float is that the machine instructions to do the arithmetic are different. Moreover the bit patterns for the numbers are different. 1 (integer) looks completely different from 1.0 (floating).
     
  13. Sep 20, 2014 #12

    SixNein

    User Avatar
    Gold Member

    We separate int and float because they are separated at a hardware level. See, for example, the following article on floating point units:
    http://en.wikipedia.org/wiki/Floating-point_unit
     
  14. Sep 20, 2014 #13

    Vanadium 50

    User Avatar
    Staff Emeritus
    Science Advisor
    Education Advisor

    I disagree with SteamKing that this is a holdover from the bad old days. This is a necessary decision by the architect of the language. When the programmer writes x = "2" + 1, what is the outcome?
    1. 3? (converts the string 2 to an integer)
    2. 21? (converts the integer 1 to a string)
    3. 0? (one byte past 2 is the null termination of the string)
    4. An error?
    All four of these options are implemented in some language or other.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Variables -- why is typing them in their declarations important?
Loading...