Quiz 2 Flashcards
(57 cards)
8 Primitive wrapper class
Byte Short Integer Long Float Character Double Boolean
Java provides an object-based wrapper class for each primitive data type and it will…
“box” or “unbox” primitives to/from wrapper-based objects as needed without an explicit request
StringBuffer key methods
- apprehend
- insert
- delete
- replace
Objects
the bundles of (related):
- data (“state”) –> instance variables
- operations (“behavior”) –> methods
- invoking operations can change state (values stored in instance variables)
Program
collection of interacting objects
Class
- Blueprint/”Recipe” for objects
- include:
- instance variables(including types, etc.) to include in objects
- implementations of methods to include in objects
- seen in static methods, public/private methods
Objects are created by…
in java: using new example: Scanner sc = new Scanner(System.in);
invoking new:
- creates an object in a memory area called “heap”
- space is created for instance variables
- returns the address/reference where the object lives
- if you lose the reference you cannot reach the object
Reference type
Objects always stored in heap (including all data)
Reference to objects are another type, and hold one memory address (typically one word)
Heap holds allocated memory (i.e., with “new”)
e.g.
Scanner sc = new Scanner (System.in);
is String name = "Batman"; & String name = new String ("Batman"); the same?
Yes, Java provides it!
String is special because it is used often and Java automatically “fills in” new.
Garbage Collection
- heap/memory management
- Java invokes garbage collector to reclaim unused, but still-allocated heap space
- Garbage collector reclaims memory in allocated heap and returns it to free heap
e.g. String s; s = new String("cat"); s = new String("dog"); s = new String("cow");
“cat” and “dog” are garbage collected
-In C++, you need to take care of reclaiming memory, otherwise you will have memory leaks
Memory Leak
type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released.
Are these Strings equal?
String a = new String (“abc”);
String b = new String (“abc”);
if (a == b) { println("Equal"); } else { println("Not Equal"); }
–> not equal. The variables are pointing at the different objects,although the content is the same
Are these Strings equal?
String a = new String (“abc”);
String b = a;
if (a == b) { println("Equal") } else { println("Not Equal"); } --> equal. This is called ALIASING --> Two variables refer to the same object
Aliasing
two variables refer to the same object
equals
- (==) checks if two reference variables refer to the same object
- methods like str.equals() check if two different objects have the same “content”
- other classes will have an equals methods also
Passing Values to Methods
- Values can be passed to methods through a parameter list.
- Actual values provided –> argument list
- Process that takes when a method is called:
i. Arguments are used to initialize the parameters
a. Matching is one to one
ii. After the values have been assigned, we transfer control to the first statement in the method
iii. After the method is done, we return to the point after the method call. - The process is to pass values is called pass-by-value
i. The parameter is a “photocopy” of the argument - We pass copies to the parameter list
- Notice that parameters are like local variables
i. Created when method is called
ii. Destroyed when method is over - Notice that local variables cannot be seen from other methods
Returning Values from Methods
- Methods that return values must specify the type of the value to be returned.
- The bodies of these methods use “return” to indicate when a value is to be returned.
- The value being returned must have the same type as the return type
- Notice that return can be used anywhere in the method
- Return always ENDS a method returning to the call point
- You can have multiple return statements in a method
- For a method with no return type “return;” will end the method.
If you don’t specify any modifier, then it is considered…
package
default value
Boolean –> false
Number –> 0
Reference –> null
Object Creation
-Once a class (Person) is defined, objects based on that class can be created using new: new Person()
-We are “creating an instance of class Person”
-To assign an object to a variable, the VARIABLE’S TYPE MUST BE THE CLASS OF THE OBJECT
Person p = new Person();
-Each object has its own copies of all the instance variables in the class
-Instance variables and methods in an object can be accessed using “.” or using setter(mutator) methods
p. age = 12;
p. setAge(12);
Static Method
-can be called by using the name of the class, followed by a period
ClassName.staticMethodName()
- in a class, a static method can call another static method without having to specify the class name
- static can only call static methods
Non-Static methods
- methods that need an object in order for them to be called
- designed to use data associated with an object
- cannot call non-static method via class name, need object reference
objectRef.nonStaticMethodName()
-nonstatic can only call nonstatic methods
When to define a method as static?
When it makes no reference to object data (instance data!)