Lesson Weekend

In the last lesson we learned we can use the getClass() method to determine what class an object belongs to. Let's try calling getClass() on a few other data types, too.

First, let's check what class the number 15 belongs to:

> 15.getClass();

ERROR: not a statement
    15.getClass();;
    ^

ERROR: ';' expected
    15.getClass();;
       ^

ERROR: ';' expected
    15.getClass();;
       ^

Hey! This is the same error we received when we tried to use toUpperCase() on a number! What gives?

Much like we cannot call methods meant for strings on anything that isn't a string, we cannot call a method meant for objects belonging to a class on things that simply don't belong to a class.

Remember when we said that almost everything was an object in Java; but that there were a few exceptions we would cover later? These are the exceptions.

Primitives

There are several types of data even simpler than objects in Java. These are called primitives. There are eight different types of primitives. But for now we'll only focus on the following three :

  • boolean - true or false
  • int - Short for "integer". A number.
  • float - Short for "floating point number". Essentially just a number with a decimal point in it. Like 3.14.

Primitives can only represent a very specific set of data and cannot have methods called on them. They're even simpler than objects and don't belong to a class. That's why we couldn't call getClass() on 15. Because it simply doesn't belong to one! We won't use the other 5 types of primitives until later on.

Wrapper Classes

However, you may notice some inconsistency here. Throughout this section of homework we've declared integers like this: Integer number = 1;. We used the uppercase term Integer, and not the lowercase primitive data type int. Yet, we were able to perform math on these Integer types, assign them to variables, and everything seemed to work just fine.

Primitive data types also have what are called wrapper classes. Primitives are even simpler than objects, but these built-in wrapper classes can literally wrap the primitive into an object. We've been using the Integer wrapper class to represent integers and Boolean to represent booleans. Wrapper classes turn primitives into full-blown objects that we may call methods on.

Primitives and their corresponding wrapper classes can easily be translated into one another too. We can always save primitive data into its wrapper class like this:

int age = 31;                     
Integer myRealAge = age;    
  • Here, int age = 31' creates an int primitive. This is not an object.
  • Next, the line Integer myRealAge = age; saves the age primitive to the Integer wrapper class. myRealAge is now a proper Integer object.

We can do the same with boolean primitives, too. Consider the following example:

boolean lying = false;                        
Boolean isHeLyingAgain = lying;       
  • boolean lying = false; creates a boolean primitive. Again, this is not an object.
  • The line Boolean isHeLyingAgain = lying; turns the lying primitive we defined in the previous line into a Boolean object named isHeLyingAgain. This is now a proper object, and may have methods called upon it.

Choosing Between Primitives and Wrapper Classes

So, how do we know when to use a primitive, and when to use a wrapper class? Remember, primitives cannot have methods called on them, because they are not objects. Whereas wrapper classes turn these primitives into objects. Therefore, instances of wrapper classes can have methods called upon them.

When deciding whether to use a primitive type or use a wrapper class to make an object type, the main advantage to sticking with primitives is performance as described here.

Going forward, when creating methods, we should be using primitive data types for parameters rather than their wrapper classes, and only convert primitives to objects when we must be able to call a method upon them. This is considered best practice in Java development.

Don't worry if this is still a little confusing. Just pay special attention to when we use primitives, and when we use wrapper classes as you follow along throughout the course. During these first few weeks we'll make sure to explain why we're using each.

Primitives and Corresponding Wrapper Classes

prims-objects-wrappers-java

If you are interested, take a skim through the top part (the bulleted list) of this overview of what primitive data types are and what kind of data they can hold.