VCE IT Lecture Notes by Mark Kelly, McKinnon Secondary College
Data Types |
When a program needs to store values while it's running, it stores the values in variables. They're called variables because their values can vary (unlike constants whose values never change - like PI = 3.14159.) There are various types of variables, each designed to contain a certain type of data. In some cases you have a choice of data types; in other cases you have no choice if you expect your program to work properly. For example uou could store the number 3.67 in either a single precision variable or a double precision variable (the former is simple more efficient in terms of storage space than the latter); but when storing a date, you either store it in a date variable or you lose the ability to treat it as a date object. |
|
Typical data types found in various languages: Boolean - stores only a True or False value. Very efficient storage indeed. Ideal for flags which remember a yes/no true/false state. Byte - stores values between 0 and 255 in one byte. Efficient storage for small numbers. Integer (or Short)- stores values from -32768 to 32767. It can have no fractional part (decimal places) Long (Integer) - stores values from -2,147,483,648 to 2,147,483,647, again with no decimal component. Decimal - a number with a fractional part. Float or Real - floating point, like decimal, single or double. Single Precision - stores values from -3.402823e38 to -1.401298e-45 for negative values and from 1.401298e-45 to 3.402823e38 for positive values. Allows decimal places. Double Precision - for storing values from -1.79769313486232e308 to -4.94065645841247e-324 for negative values and from 4.94065645841247e-324 to 1.79769313486232e308 for positive values. The 'e' means 'times 10 to the power of'. In other words 4.94065645841247e-324 is roughly 4.9 followed by 324 zeroes. It allows extreme precision. String - stores text composed of any ASCII characters (alphabetic letters, numerals, punctuation and control codes like carriage return, backspace, bell) up to about 2 billion characters. While it can store numerals, it cannot interpret the value of numbers and instead treats them as plain text. So if you add "1" and "2" you'll get "12" instead of 3! Char - can hold a single ASCII character. Date - holds a complete date value (including day, month and year). Date Time (or timestamp) - holds a complete date (DDMMYYYY) and time of day (hours, minutes and seconds) Complex numbers (e.g. in FORTRAN) store a real and imaginary pair of values. Different languages like C offer signed or unsigned variants of numeric data types. "Signed" versions allow a negative sign. e.g. Signed Char can hold values from -128 to +127 where unsigned Char can hold 0 to 255. If the values to be stored cannot be negative, unsigned variants double the capacity of valid entries. C has a special data type designed to hold a pointer to a memory location. |
Some languages are strongly typed and others are weakly typed. In a strongly typed language, all variables must have their type declared before values can be stored in them. Such languages are strict about exchanging values between variables of different types. Weakly typed languages don't force programmers to declare variables beforehand, and they infer (decide on by guessing) the type of a variable by how it's being treated. .e.g. Some languages allow relatively free data exchanges (e.g. C does not mind if you copy a value from a pointer variable into a long integer or a char to a short integer). Other languages would require the programmer to explictly convert from one type to another (to prevent unintentional casting) e.g. Implicit casting is when a value is converted from type type to another simply by assigning it e.g. Languages ComparedJava, C, C#, Pascal, Ada - require all variables to have a defined type and support the use of explicit casts (deliberate conversions) of types. Smalltalk, Ruby, Python, Lisp and Self are all strongly typed in that undeclared variables are prevented at runtime and they allow little implicit type conversion. Visual BASIC is a mixed bag. It supports statically typed variables, as well as the Variant data type that can store data of any type. Its implicit casts are fairly liberal where, for example, one can sum string variants and pass the result into an integer. In general, weakly typed languages offer a faster development rate at the risk of accidental improper data conversions (e.g. accidentally saying something like A=B + C and forgetting that B is a date type, resulting in rubbish results. |
|
Back to the IT Lecture Notes index
Back to the last page you visited
Created 13 Sep 2010
Last changed: September 13, 2010 11:39 AM
VCE IT Lecture notes copyright © Mark Kelly 2001-