Learning Java: Day 3

Variables
Today we are covering Chapter 3 of "Head First Java".

Remember variables from algebra? You know, x + 3 = 10, you solve for the unknown x, so x = 7? Programming uses variables, too.  Remember class and objects from yesterday's lesson? Variables can be objects. The java book defines a variable as a container that holds something. There are two kinds of variables in Java: primitives and references.

A primitive variable can be one of several types: a number (byte, short, int, long, float, double), or a "yes/no, true/false, on/off" answer (boolean), or an alphabetic letter (char). If you really are into this stuff, here's the mnemonic for remembering primitive types: Be Careful! Bears Shouldn't Ingest Large Furry Dogs. (Yep, I know, you're only reading this to be kind and have no interest in learning Java...) Primitive variables are different sizes according to the type.

The other type of variable is an object reference variable. This type of variable is just one size no matter if the object is a bacteria, a movie, a galaxy, or something entirely intangible because this variable only holds the reference to the object rather than the object itself. It's kind of like the phone number to contact the object when the object needs to do something.

You have to tell Java what type, what value and what name your variable has. Kind of like ordering in expensive coffeehouses, "I'd like a grande Salted Caramel Hot Chocolate" (can you tell I'm not a coffee drinker?) To put it in Java terms it would be a "Chocolate (type)  hotSaltedCaramel (name) = grande (value)".  Try that at Starbucks!

A school of fish is a Fish Array
Other things learned in this chapter are arrays (a litter of puppies is a Dog array, a dozen eggs in a carton is an Egg array, dresses in a closet is a Dress array, etc.), how an array is always an object (not plural, an array is a singular object even though it may contain multiple things), and using the dot operator to access the parts of an object.

We end this chapter with exercises and puzzles.

Wow, this took me 4 hours to get through. I can feel my neurons humming...


0 comments:

Post a Comment