Variables (Head First Java Ch.3) Flashcards
- Overloading (methods and constructors) - Mutable vs Immutable Class - "this" keyword - Data types - Stack vs. Heap - Arrays
Why isn’t it possible to store an int into a byte:
int x = 24;
byte b = x;
a byte is 8 bits, while an int is 32 bits. So it is not possible to store something large into something smaller
What are the rules for naming variables?
- It cannot START with a number. It can contain numbers afterwards
- It cannot be one of Java’s reserved names (keywords, primitive types)
What is stored in Stack?
local primitive data variables, and references to objects
What is stored in Heap?
objects themselves
How do we access a method through an object?
Using the dot operator(.) followed by the method name we wish to access . We can include arguments to pass to the method if applicable.
Book b = new Book() ;
Book c = new Book() ;
b = c;
Are these variables pointing to different objects?
Because of the last line of code, b and c are pointing the same “Book object”
How can we declare an array of objects?
- Declare array variable and set length:
Dog[] pets = new Dog[7];
- create new Dog objects and set them to array elements
pets[0] = new Dog();
pets[1] = new Dog();