A data type (データ型) is a classification that fixes what kind of data a variable or value represents, along with the range of values it can take and the operations it allows.
Data types (basic types in this unit)
Integer type
Real type
Boolean type
Character type
Once the type is set, it determines how the value is stored in memory, how large a range of values fits, and which operations can be applied. This unit covers 4 basic types: integer, real, Boolean and character. Giving each variable a type makes it easier to catch the mistake of storing a value of the wrong kind.
AnalogyIt is like labeling each drawer of a filing cabinet with the kind of item it may hold. Because of that rule, you always know how to handle whatever you take out.
How to rememberThe Japanese 型 (kata) is simply a translation of "type", which means a kind or category.
Exam points
Mix-upData type (データ型) is the collective name for all the individual types, while the integer type (整数型) and the character type (文字型) are each one type within it.
The integer type (整数型) is a data type for whole numbers with no fractional part, negative numbers included.
It suits values that never have a fraction, such as counts, repetitions, or the position number of an element in an array. When one integer is divided by another, it can also be used to get the quotient and the remainder as integers.
AnalogyThink of the steps of a staircase. There is a first step and a second step, but no step 1.5.
How to remember"Integer" comes from a Latin word meaning "untouched, whole".
Exam points
Mix-upThe values an integer type (整数型) can hold have an upper and a lower limit, and that range depends on the language implementation. A calculation result outside the range cannot be represented correctly.
The real type (実数型) is a data type for numbers that can have digits after the decimal point.
It is used for values with fractions, such as lengths, averages and ratios. Inside the computer it is usually stored as a floating-point number, so a value like 0.1 that has no exact binary form carries a tiny error.
AnalogyIt is like reading a length that falls between the marks on a ruler. The reading may still be off by a very small amount.
How to remember"Floating" in floating-point refers to the decimal point moving its position to fit the size of the number.
Exam points
Mix-upThe real type (実数型) and the integer type (整数型) both represent numbers, and they differ in whether a fractional part is allowed.
Integer Type vs. Real Type
Aspect
Integer Type
Real Type
Key differenceValues held
Whole numbers with no fractional part
Numbers with digits after the decimal point
Examples
-3, 0, 42
2.5, -0.75
Internal form
Stored exactly as a binary integer
Usually stored as a floating-point number, which may carry an error
The Boolean type (論理型) is a data type that takes only one of 2 values, true or false.
Comparison
Result (Boolean value)
8 > 5
true
4 = 9
false
6 ≦ 6
true
An expression that compares sizes or equality yields a Boolean value, which serves as the condition that steers processing in selection and repetition. In Japanese it is also called Boolean type (ブール型), while the formal term 論理型 literally means "logical type".
AnalogyThink of a light switch. It is either on or off, with nothing in between.
How to remember"Boolean" comes from George Boole, the mathematician who turned calculations with true and false into a system.
Exam points
Mix-upThe Boolean type (論理型) holds truth itself as its value, and it is a separate type from the number or character types being compared.
The character type (文字型) is a data type whose value is a single character, such as a letter, digit, symbol or kana.
Inside the computer, each character is represented by a number called a character code. A value made of several characters in a row is handled as a string; pseudo-language and similar notations give it a separate type, the string type.
AnalogyIt is like a single piece of movable type. Even a piece shaped like a digit is a symbol for printing, not a number for calculating.
How to rememberMany programming languages abbreviate "character" to char in code.
Exam points
Mix-upThe character '5' is a character represented by a character code (文字コード), a different value from the number 5. To calculate with it as a number, convert it to a numeric type first.
Integer Type vs. Character Type
Aspect
Integer Type
Character Type
Values that look alike
The number 5
The character '5'
Internal form
The size of the number in binary
The number assigned by the character code
Key differenceArithmetic
Can be used in arithmetic as is
Must be converted to a numeric type first
Key points
Choosing a data type fixes how a value is stored, what range of values fits, and which operations are available.
The integer type has upper and lower limits on its values, and the range depends on the implementation.
The real type is usually stored as a floating-point number, so some values carry a tiny error.
The result of a comparison is a Boolean value, used as the condition for selection and repetition.
The character type holds one character, and a value made of several characters is handled as a string.