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

In summary, variables are declared and typed to help the compiler with error checking and memory allocation.
  • #1
jackylaucf
3
0
Hiya! I am a beginner in programming and I am 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!
 
Technology news on Phys.org
  • #2
jackylaucf said:
Hiya! I am a beginner in programming and I am 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!

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.
 
  • #3
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:
  • #4
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".
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
jtbell said:
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.

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.
 
  • Like
Likes TumblingDice
  • #9
SteamKing said:
Variable typing is also a hold-over from the days when computers had tiny memories.
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).
 
  • #10
SteamKing said:
Variable typing is also a hold-over from the days when computers had tiny memories.

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.
 
  • #11
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).
 
  • #12
jackylaucf said:
Hiya! I am a beginner in programming and I am 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!

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
 
  • #13
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.
 

What is the purpose of typing variables in their declarations?

Typing variables in their declarations is important because it allows the computer to allocate the appropriate amount of memory for the variable and determine what type of data it will hold. This helps to avoid errors and improve program efficiency.

What types of errors can occur if variables are not properly typed in their declarations?

If variables are not properly typed in their declarations, it can lead to syntax errors, type mismatch errors, and even logical errors in the program. This can result in the program not running correctly or crashing.

Why is it important to specify the data type of a variable?

Specifying the data type of a variable is important because it ensures that the variable will only hold data of a specific type. This helps to prevent unexpected results and errors in the program.

What are the advantages of using typed variables?

Using typed variables can improve program efficiency, reduce errors, and make the code more readable and maintainable. It also allows for better control and manipulation of data within the program.

Are all programming languages case-sensitive when it comes to variable declarations?

No, not all programming languages are case-sensitive when it comes to variable declarations. Some languages, such as Python, are case-sensitive, while others, like Visual Basic, are not. It is important to understand the rules for variable declaration in the specific programming language being used.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
7
Replies
235
Views
9K
Replies
63
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
16
Views
4K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top