Goals For This Lesson
After completing this lesson students should be able to:
- understand the common uses of certain datatypes.
- identify and classify data using datatypes
- describe objects in the real world using datatypes
Why Do We Use Datatypes?
Datatypes help us as programmers represent different forms of information in the programs we write. The main datatypes that you will come across in Java are the String, int, char, double and boolean datatypes; these datatypes are also found in most other higher-level programming languages. These datatypes are used with a programming construct called a variable which stores data based on which datatype it is associated with, i.e. a String variable can only hold Strings, while a boolean variable can only hold booleans.
ints and doubles
This concept allows us as programmers to write code that works with numbers; both whole numbers, in the case of the int datatype, and decimal numbers, in the case of double datatype, (when precision is important).
Strings and chars
Strings are used in cases where we want to store names, street addresses or complex encrypted information. When a single character needs to be stored (this could be a single letter, a single symbol or even a single-digit number) chars are used by programmers in these cases.
The boolean datatype assists us when we want to store information which we use in the decision making parts of the program (control flow).