- slide 1 of 2
Every day we meet names and numbers—at home, at work, at school or at play. A person’s name is a type of data; so is a number. We can thus speak of the two data types called ‘name’ and ‘number.’ In the statement:
Mimi bought 3 dresses for $199.95
we can find:
- an example of a name: Mimi;
- two examples of numbers: 3 and 199.95
Usually, we find it convenient to divide numbers into two kinds:
(1) whole numbers, or integers;
(2) numbers with a decimal point, so-called real or floating-point numbers.
In the example, 3 is an integer and 199.95 is a real number.
Exercise: Identify the data types—names, integers and real numbers—in the following:
(a) Michael’s scoring average was 25.25 with a highest score of 45.
(b) Abigail, who lives at 41 Third Ave, worked 36 hours at $11.50 per hour.
(c) In his 8 subjects, Richard’s average mark was 68.5.
Generally speaking, programs are written to manipulate data of various types. We use the term numeric to refer to numbers (integer or floating-point). We use the term string to refer to non-numeric data such as a name, address, job description, title of a song or vehicle number (which is not really a number as far as the computer is concerned—it usually contains letters, e.g. PBN8652).
Programming languages in general, and Java in particular, precisely define the various types of data which can be manipulated by programs written in those languages. Integer, real (or floating-point), character (data consisting of a single character such as 'K' or '%') and string data types are the most common.
Each data type defines constants of that type. For example:
- some integer constants are 3, -52, 0 and 9813;
- some real (or floating-point) constants are 3.142, -5.0, 345.21 and 1.16;
- some character constants are 't', '+', '8' and 'R';
- some string constants are "Hi there", "The Art of Computer Programming" and "Java World".
Note that, in Java, a character constant is delimited by single quotes and a string constant is delimited by double quotes.
When we use a variable in a program, we have to say what type of data (the kind of constants) we intend to store in that variable—we say we must declare the variable. It is usually an error if we declare a variable to be of one type and then attempt to store a different type of value in it. For example, it would be an error to attempt to store a string constant in an integer variable.
In How To Declare Variables And Write An Assignment Statement In Java we will show you how declare and use data types to create programs.