Variables (Head First Java Ch.3) Flashcards

- Overloading (methods and constructors) - Mutable vs Immutable Class - "this" keyword - Data types - Stack vs. Heap - Arrays

1
Q

Why isn’t it possible to store an int into a byte:
int x = 24;
byte b = x;

A

a byte is 8 bits, while an int is 32 bits. So it is not possible to store something large into something smaller

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the rules for naming variables?

A
  1. It cannot START with a number. It can contain numbers afterwards
  2. It cannot be one of Java’s reserved names (keywords, primitive types)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is stored in Stack?

A

local primitive data variables, and references to objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is stored in Heap?

A

objects themselves

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do we access a method through an object?

A

Using the dot operator(.) followed by the method name we wish to access . We can include arguments to pass to the method if applicable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Book b = new Book() ;
Book c = new Book() ;

b = c;

Are these variables pointing to different objects?

A

Because of the last line of code, b and c are pointing the same “Book object”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we declare an array of objects?

A
  1. Declare array variable and set length:

Dog[] pets = new Dog[7];

  1. create new Dog objects and set them to array elements

pets[0] = new Dog();
pets[1] = new Dog();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly