Tuesday, February 3, 2015

Chapter 4

In Chapter 4 of Headfirst Java, I learned about Instance and Local variables, and how they act. Instance variables are declared as part of the class and have default values, but Local variables are defined within a method and have no initial value. For example...

public class Dog{
    public int size;
    public void printSize() {
        System.out.print(size);
    }
}

Calling the printSize() method on this Dog class will return 0, since the default value of ints are 0. However, you can't do this...

public class Dog{
    public void printSize() {
        int size;
        System.out.print(size);
    }
}

This code won't compile because the size variabe is Local and has no default value.

I also learned that there are two different ways to compare variables, as I discovered in the cat simulator program. The == operator  compares variables on a binary level, meaning the variables have to be exactly the same values and be in the same "heap", whereas you can use .equals() to compare similar objects from different heaps (such as strings with the same characters).

1 comment:

  1. Mysuru Casino - The HERZAMMAN
    Mysuru filmfileeurope.com Casino wooricasinos.info - The Home of the herzamanindir Best of the Slots! Visit us to Play the best worrione.com slots 메이피로출장마사지 and enjoy the best table games in our casino. Visit us

    ReplyDelete