Wednesday, January 28, 2015

Head-First Java - Chapter 3 - What I Learned

This chapter was all about variable types and their functions. It helped to distinguish between objects and classes, and showed how the whole "Blah blah1 = new Blah();" thing works in terms of using reference variables to control objects. Some other things I got out of this were:


  • Primitive variables come in eight flavors - four relating to integers, two relating to non-integer real numbers (i.e. with decimal points), one for booleans, and one for characters
    • Booleans (boolean), like always, are either True or False
    • Characters (char), can take up 16 bits and assign variables to letters
    • The four integer variables are byte, short, int, and long. They can hold 8 bits, 16 bits, 32 bits, and 64 bits, respectively. They're all used for numerical values, but each are used depending on the size of the value
      • An important thing to note is that a good analogy for these numeric variables are different sized cups. You can redefine smaller values that fit into smaller types as bigger types, like how you can pour something from a small cup into a larger cup. However, you cannot redefine values in bigger types as smaller types, even if they'd technically fit in the bit-constraint. The JVM doesn't want to pour stuff from a big cup into a smaller cup, because it doesn't know if it'd fit or not and prefers to err on the side of caution.
    • Numbers with decimal points (floats, non-integer numbers) have two primitive variable types - floats, which can hold 32 bits, and doubles, which can hold 64 bits. I have no clue why they call the larger version of the float a double - it seems like a pretty unspecific choice of diction for such a particular data type
  • Variables have to start with a letter, underscore, or a dollar sign
    • This helped explain to me why I oftentimes see the $ symbol in Java code
    • Numbers can only be used after the first character in a variable name
  • There are also certain names that cannot be used for a variable, because they are used elsewhere in Java (e.g. class, finally, assert, etc.)
  • Object reference variables are basically remote controls
    • Imagine a line of code that reads "Dog d = new Dog();". This creates a Dog object reference variable, referencing a dog object. This variable holds a sort of remote control called "d", which controls a Dog() object
    • The next line could read "d.bark();", which tells the remote to hit the "bark" button, or call the bark() function on the specific Dog() object that the reference variable is referring to
  • The bit depth of object reference variables aren't relevant
  • As long as a reference variable is assigned to a particular object, that object stays active. If an object remains inactive, it is sent into the "garbage heap" and is no longer reference-able or usable
  • An array variable creates a sort of "tray of cups", with each cup being a variable that holds a particular value

Here's some dog code:


class Dog {
    String name;
    public static void main (String[] args) {
        //make a Dog object and access it
        Dog dog1 = new Dog();
        dog1.bark();
        dog1.name = "Bart";

        //now make a Dog array
        Dog[] myDogs = new Dog[3];
        //and put some dogs in it
        myDogs[0] = new Dog();
        myDogs[1] = new Dog();
        myDogs[2] = dog1;

        //now access the Dogs using the array references
        myDogs[0].name = "Fred";
        myDogs[1].name = "Marge";

        //Hmmm... what is myDog[2] name?
        System.out.print("last dog's name is ");
        System.out.println("myDogs[2].name);

        //new loop through the array, and tell all dogs to bark
        int x = 0;
        while(x < myDogs.length) {
            myDogs[x].bark();
            x = x + 1;
        }
    }

    public void bark() {
        System.out.println(name + " says Ruff!");
    }

    public void cat();
    public void chaseCat();
}
 

No comments:

Post a Comment