Why Different Data Types Exist

Different data types exist because the computer has to store and manipulate different types of data in different ways. The computer needs to reserve a lot less space to store the number 4 than it needs for a very long string (this entire book could be one big string). Sometimes different types of data don’t play well together. For example, Listing 5.1 shows how you can add numbers to get numbers, you can add strings to get concatenated strings, and you can add numbers and strings to get ... strings? Getting '22' when you add 2 and '2' is a little nonsensical, but would 4 make any more sense? The difference between the string '2' and the number 2 is very significant to a computer, and confusing them can lead to strange ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.