MIDTERM 2 Cheat Sheet
MIDTERM 2 Cheat Sheet
-import java.util.*;
⇒ Easy, you should know all, notice we have something call “Object[] objectArray” because
⇒ Create a new object in memory, and return a reference, reference store in a variable.
⇒ Copy Arrays:
//we have array1 and its values
1) Int[] array2 = Arrays.copyOf(array1, array1.length)
2) Create a new array, then use the for loop to make indexes in new array =
arrayWantToCopy
Ex: array1[i] = array[i].
++Don’t use array1 = array2 (copy the memory address (reference), not the actual values)
WRONG!!!++
⇒ Answer above.
⇒By Reference:
–Objects, Arrays pass reference to objects in memory.
–Change in method will change the actual objects.
–Avoid this by making a new array and copy
Mutable vs immutable objects
⇒Wrapper is a reference type, meaning the variable of a wrapper object points to its object in
memory. When set to null, means no arrow pointing in memory.
–int on the other hand can’t do this.
BigInteger class
Public vs private
⇒ public:
–Accessible from anywhere in the program, including outside the class.
–Can be used by other classes and objects
⇒ private:
–Accessible only from within the class where they are declared.
–Other classes and objects cannot directly access them.
Garbage collection:
⇒ Clean up memories, with objects only, so primitives like int, and double won’t work.
⇒ Only clean objects that are not reachable, (there is no way to access an object in memory)
Ex:
Obj obj = new Obj
Obj = null (NEED GC)
⇒ String:
–Immutable object. (The content of the String object in that memory address can’t be modify)
–Do any methods or operations on String, a new String object is created outside of the String
pool (adding Strings, substring etc,..)
–No security risks
String b = “Hello World” → Check, that there is Hello World in the String pool, make b
many more as long as it's the same thing with different names.
⇒ String builder:
–Mutable objects
–Allow modification directly without creating new objects.
–Use to only create one object when doing stuff like adding, substring, String manipulation,
etc…
–Security risks.
–Unlike String, is memory efficient when having a large amount of text.
⇒ Search Google.
What are the common methods for String and StringBuilder classes (charAt,
getChars,indexOf, lastIndexOf, length, substring)
⇒ Google
StringBuilder methods such as delete, append, insert, etc (Not in String)
⇒ 7 methods:
+append: add a string to the end of the current StringBuilder object.
+insert(int offset, String str)
+delete(int start, int end)
+replace(int start, int end, String str)
+substring(int start)
+substring(int start, int end)
+reverse()
⇒ Encapsulation:
–Constructors are part of the encapsulation mechanism in Java, where the internal state of an
object is hidden from external code.
–Encapsulation means attributes are private and only accessible through methods.
== vs equals method
⇒ Comparing object Reference vs comparing actual Values or Contents, this is asked for String
toString method