Lecture 8 (Sherriff) - Data Types

Lecture Date: Friday, January 30

Writing a program is like building with Legos.

Follow me on this: a program can be a large, complex thing, or a relatively small, compact thing. So can the things you build out of Legos. However, even the biggest Lego sculpture has something in common with the smallest little Lego building - they are both made with the same kinds of individual pieces. Sure, there are special pieces that only do certain things, but for the most part, it’s (pardon the pun) all the same building blocks.

What are the component building blocks of writing programs? Data types.

We separate data types into two primary categories: primitive and complex.

The 8 primitive types in Java are:

  • boolean: true or false
  • byte: an integer between -128 and 127
  • short: an integer between -32,768 and 32,767
  • int: an integer number (up to 231)
  • char: a single character (represented as a number)
  • long: a really big integer number (up to 263)
  • float: a decimal number
  • double: a really big decimal number

These are the basic building blocks of Java programs. These data types come pre-made and ready to go in Java. We can use these to do whatever we want.

Some of you at this point might think: “Wait… what about String?”

String isn’t a primitive data type. It’s a complex type - a class. Technically, a String is just a long line of chars. We also know String is a complex type because it is capitalized.

Today, we’ll learn the 8 primitive types, what they are used for, and how they differ from complex types.

We will make sure to cover:

  • Data type mixups
  • Casting
  • Unicode / int and char
  • Using the Math class
  • How String is different - calling methods from a class

Example Code: DataTypes.java