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
Local Variables
Live on the stack
Objects and instance Variables
Live on the Heap
Integer literals
can be decimal, octal, or hexadecimal
Float literals end in
F or f
Double literals end in
D or d
Boolean literals
are true and false
scope
refers to the lifetime of a variable
4 basic scopes
- Static variables live basically as long as their class lives
- Instance variables live as long as their object lives
- Local variables live as long as their method
- Block variables live until the block completes
Literal integers
are implicitly ints
integer expressions
always end in a int-sized result, never smaller
floating point numbers
are implicitly doubles (64 bits)
Narrowing primatives
truncates the high order bits
Compound assignments (eg +=)
perform an automatic cast
A reference variable
holds bits that are used to refer to an object
Reference variables
can refer to subclasses of the declared type but not to the superclasses
When creating an object (new keyword), 3 things happen
- ) Make a reference variable
- ) Create a new Object
- ) Assign object to the reference variable
Array objects that are uninitialized
when instantiated, objects within the array are not initialized automatically, but all reference get the default value of null.
Array primatives
when instantiated receive their default values
Instance variables
always initialized with their default value
local/automatic/method variables
never given a default value, if you attempt to use one before initializing you will get a compiler error
Methods
can take primitive and/or object references as arguments
Method arguments
are always copies
Method arguments
are never actual objects (they can be references to objects)
A primitive argument
is unattached copy of the original primative
A reference argument
is another copy of a reference to the original object
Shadowing
occurs when two variables with different scopes share the same name
Arrays
can hold primitives or objects, but the array itself is always an Object
When you declare an array
the brackets can be left or right of the name
Array size
it is never legal to declare the size of an array in the declaration
You must include the size of an array when you construct it
(using new) unless you are creating an anonymous array.
Elements in an array of objects
are not automatically created, although primitive array elements are given their default values
You will get a NullPointerException
if you try to use an array element in an object array if that element does not refer to a real object
arrays
are indexed beginning with 0
An ArrayIndexOutOfBoundsException
occurs if you use a bad index value
Arrays have a length variable
whose value is the number of array elements
Last index in an array
is always one less than the length of the array
Multidimensional Arrays
are just arrays of arrays
The dimensions in a multidimensional array
can have different lengths
Array primitives
can accept any value that can be promoted implicitly to the array’s declared data type.
An array of objects
can hold any object that passes an IS-A test for the declared type of array.
If you assign an array to a previously assigned array reference
the array you are assigning must have the same dimension as the reference your assigning it to
You can assign an array of one type
to a previously declared array of one of its supertypes.
Static initialization blocks
run once when the class is first loaded
Instance initialization blocks
run every time a new instance is created. They run after ALL super-constructors, and before the constructor’s code has run
If multiple init blocks exist in a class
they follow the previous 2 rules on initialization AND they run in the order in which they appear in the source file.
The wrapper class
correlate to the primitive types
Wrappers have 2 main fuctions
- to wrap primitives so they can be handled like objects.
2. to provide utility methods for primitives (usually conversions)
The 3 most important method families are
- xxxValue(); takes no args, returns primitive
- parseXXX();takes a string, returns primative, throws NFE
- valueOf; takes a String, returns a wrapped object, throws NFE
Wrapper constructors
can take a string or a Primitive, except for Character, which can take a char.
Radix refers to
bases other than 10; octal is radix = 8, hex = 16
Boxing
as of Java 5, allows you to convert primitives to wrappers or to convert wrappers to primitives automatically.
Using == with wrappers created through boxing
those with the same small values, typically lower than 127 will be ==; larger values will NOT be ==
Primitive widening
uses the “smallest” method argument possible
used individually, boxing and var-args
are compatible with overloading
You CANNOT widen from one wrapper type to another
IS-A fails
You CANNOT widen and then box
An int can’t become a long
You can box then widen
An int can become an Object, via an Integer
You can combine var-args with either
widening or boxing
Garbage collection
Java provides automatic memory management
Purpose of GC
delete objects which can’t be reached
Only the JVM
decides when to run GC, you can only suggest it
Object must be considered “eligible”
before they can be garbage collected
An object is eligible for GC
when no live thread can reach it.
To reach a thread
you must have a live, reachable reference to that object.
Islands of objects
can be GCed, even though they refer to each other
Class Object
has a finalize() method
The finalize() method
is guaranteed to run once and only once before the GC deletes an object
GC makes no guarantees
finalize() may never run
You can uneligibilize an object for th GC
within finalize()
Primitive Data Types
Be careful bears shouldn’t ingest large furry dogs.
Boolean, char, byte, short, int, long, float double
8,16,32,64,32,64
Roses are red,, violets are blue
extend only one, but implement two
Instance variables are assigned a default value
even if you don’t explicitly assign one. The default values are 0/0.0/false for primitives, and null for references.
Roses are red, violets are blue
Your parents come first, way before you… The superclass parts of an object must be fully formed before the new subclass object can exist.
A final
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