Chapter 3 - Assignments Flashcards

Assignments: Class Members, develop Wrapper Code and Autoboxing Code Determine effects of passing variables to methods Recognize when objects become eligible for garbage collection

1
Q

Local Variables

A

Live on the stack

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

Objects and instance Variables

A

Live on the Heap

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

Integer literals

A

can be decimal, octal, or hexadecimal

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

Float literals end in

A

F or f

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

Double literals end in

A

D or d

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

Boolean literals

A

are true and false

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

scope

A

refers to the lifetime of a variable

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

4 basic scopes

A
  1. Static variables live basically as long as their class lives
  2. Instance variables live as long as their object lives
  3. Local variables live as long as their method
  4. Block variables live until the block completes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Literal integers

A

are implicitly ints

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

integer expressions

A

always end in a int-sized result, never smaller

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

floating point numbers

A

are implicitly doubles (64 bits)

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

Narrowing primatives

A

truncates the high order bits

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

Compound assignments (eg +=)

A

perform an automatic cast

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

A reference variable

A

holds bits that are used to refer to an object

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

Reference variables

A

can refer to subclasses of the declared type but not to the superclasses

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

When creating an object (new keyword), 3 things happen

A
  1. ) Make a reference variable
  2. ) Create a new Object
  3. ) Assign object to the reference variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Array objects that are uninitialized

A

when instantiated, objects within the array are not initialized automatically, but all reference get the default value of null.

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

Array primatives

A

when instantiated receive their default values

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

Instance variables

A

always initialized with their default value

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

local/automatic/method variables

A

never given a default value, if you attempt to use one before initializing you will get a compiler error

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

Methods

A

can take primitive and/or object references as arguments

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

Method arguments

A

are always copies

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

Method arguments

A

are never actual objects (they can be references to objects)

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

A primitive argument

A

is unattached copy of the original primative

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

A reference argument

A

is another copy of a reference to the original object

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

Shadowing

A

occurs when two variables with different scopes share the same name

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

Arrays

A

can hold primitives or objects, but the array itself is always an Object

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

When you declare an array

A

the brackets can be left or right of the name

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

Array size

A

it is never legal to declare the size of an array in the declaration

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

You must include the size of an array when you construct it

A

(using new) unless you are creating an anonymous array.

31
Q

Elements in an array of objects

A

are not automatically created, although primitive array elements are given their default values

32
Q

You will get a NullPointerException

A

if you try to use an array element in an object array if that element does not refer to a real object

33
Q

arrays

A

are indexed beginning with 0

34
Q

An ArrayIndexOutOfBoundsException

A

occurs if you use a bad index value

35
Q

Arrays have a length variable

A

whose value is the number of array elements

36
Q

Last index in an array

A

is always one less than the length of the array

37
Q

Multidimensional Arrays

A

are just arrays of arrays

38
Q

The dimensions in a multidimensional array

A

can have different lengths

39
Q

Array primitives

A

can accept any value that can be promoted implicitly to the array’s declared data type.

40
Q

An array of objects

A

can hold any object that passes an IS-A test for the declared type of array.

41
Q

If you assign an array to a previously assigned array reference

A

the array you are assigning must have the same dimension as the reference your assigning it to

42
Q

You can assign an array of one type

A

to a previously declared array of one of its supertypes.

43
Q

Static initialization blocks

A

run once when the class is first loaded

44
Q

Instance initialization blocks

A

run every time a new instance is created. They run after ALL super-constructors, and before the constructor’s code has run

45
Q

If multiple init blocks exist in a class

A

they follow the previous 2 rules on initialization AND they run in the order in which they appear in the source file.

46
Q

The wrapper class

A

correlate to the primitive types

47
Q

Wrappers have 2 main fuctions

A
  1. to wrap primitives so they can be handled like objects.

2. to provide utility methods for primitives (usually conversions)

48
Q

The 3 most important method families are

A
  1. xxxValue(); takes no args, returns primitive
  2. parseXXX();takes a string, returns primative, throws NFE
  3. valueOf; takes a String, returns a wrapped object, throws NFE
49
Q

Wrapper constructors

A

can take a string or a Primitive, except for Character, which can take a char.

50
Q

Radix refers to

A

bases other than 10; octal is radix = 8, hex = 16

51
Q

Boxing

A

as of Java 5, allows you to convert primitives to wrappers or to convert wrappers to primitives automatically.

52
Q

Using == with wrappers created through boxing

A

those with the same small values, typically lower than 127 will be ==; larger values will NOT be ==

53
Q

Primitive widening

A

uses the “smallest” method argument possible

54
Q

used individually, boxing and var-args

A

are compatible with overloading

55
Q

You CANNOT widen from one wrapper type to another

A

IS-A fails

56
Q

You CANNOT widen and then box

A

An int can’t become a long

57
Q

You can box then widen

A

An int can become an Object, via an Integer

58
Q

You can combine var-args with either

A

widening or boxing

59
Q

Garbage collection

A

Java provides automatic memory management

60
Q

Purpose of GC

A

delete objects which can’t be reached

61
Q

Only the JVM

A

decides when to run GC, you can only suggest it

62
Q

Object must be considered “eligible”

A

before they can be garbage collected

63
Q

An object is eligible for GC

A

when no live thread can reach it.

64
Q

To reach a thread

A

you must have a live, reachable reference to that object.

65
Q

Islands of objects

A

can be GCed, even though they refer to each other

66
Q

Class Object

A

has a finalize() method

67
Q

The finalize() method

A

is guaranteed to run once and only once before the GC deletes an object

68
Q

GC makes no guarantees

A

finalize() may never run

69
Q

You can uneligibilize an object for th GC

A

within finalize()

70
Q

Primitive Data Types

A

Be careful bears shouldn’t ingest large furry dogs.
Boolean, char, byte, short, int, long, float double
8,16,32,64,32,64

71
Q

Roses are red,, violets are blue

A

extend only one, but implement two

72
Q

Instance variables are assigned a default value

A

even if you don’t explicitly assign one. The default values are 0/0.0/false for primitives, and null for references.

73
Q

Roses are red, violets are blue

A

Your parents come first, way before you… The superclass parts of an object must be fully formed before the new subclass object can exist.

74
Q

A final

A
variable means you can't change it's value,
method means you can't override the method,
class means you can't extend the class