Learning a New Programming Language Part 3: Syntax Differences - Defining Variables
(Page 2 of 7 )
Having some sort of namable container for data in a programming language is a no brainer. What varies from one language to another is the way they are created and the syntax that is used for them. This brings us to a major concept in programming, called typing.
When you are packing things in boxes to move somewhere, you usually try to use the right box for the items you wish to pack. If you want to pack a small item you generally don't use a large container; if you have a large item, you can't very well use a container that is too small.
This idea is enforced in many languages through typing (as in the type of data stored). Some languages have what is called "static" typing. In static typing you must specify when declaring a variable what type of data you intend to store there.
Java is an example of a statically typed language. In Java, variables, including arguments to methods and return values from methods, must be declared with a type. Since methods use the keyword "return" to return values, the type for the return value is specified before the method name.
int myMathMethod(int myNumber)
return myNumber + 8;
someNumber = 6;
// call the method
result = myMathMethod(someNumber);
//result in this case would be 14
It is good practice when coding a class in pretty much any language to declare and then assign values. Declarations are usually done at the top of the file, while assignments are done anywhere you need them.
The type is specified first, followed by the name of the variable. In this case all the types are "int" or integers. There are a few types such as integers, double, long, float, boolean and char that are pretty common among most programming languages. Some languages have more types than others.
Wether or not this kind of rigid adherence to typing data actually affects the memory footprint of an application depends on the compiler or interpreter. Static typing has the benefit of exposing potentially fatal (fatal to your application, not to you of course) bugs in your code when data gets used in an erroneous way.
What do I mean? Well, say for instance that, in the midst of some complicated code, an integer variable is multiplied by a string. Whatever output, if any, you might get from such an equation would probably be of no use to you.
You wouldn't knowingly code things this way, but if you were to accidentally reassign a string to a variable that had been an integer, and then later use it in that equation, static typing rules would catch the error.
Weak vs. Strong Typing
Okay, so what if you want to take a number generated from your code and make it part of a string that can be displayed to the user?
string msg = "You have " + mynumber + " messages";
This a perfectly logical mixing of data types, but would it work? In a language that uses strong typing it would not. With strong typing you must explicitly convert data from one type to another before you can use it in this way. In this example you would have to convert the "mynumber" integer variable into a string. Weakly typed languages are more forgiving and attempt to make the conversion for you.
Thinking about types does take a little getting used to if you haven't had to before (we are creatures of habit after all) but it can help keep you up on hard to track bugs in your code. On the other hand, I have known a few people with particularly good memories that seem to be able to keep what variable is what in their head even with large projects. Go figure.
More Web Hosting How-Tos Articles
More By Chris Root