0% found this document useful (0 votes)
20 views8 pages

MIDTERM 2 Cheat Sheet

Djnd

Uploaded by

2cdg5bbkvm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

MIDTERM 2 Cheat Sheet

Djnd

Uploaded by

2cdg5bbkvm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

REMEMBER:

-import java.util.*;

Arrays of different data types:

⇒ Easy, you should know all, notice we have something call “Object[] objectArray” because

Object is the root class of all JAVA objects.

The keyword new. What does it mean in memory diagram

⇒ Create a new object in memory, and return a reference, reference store in a variable.

How to display, access, traverse and copy arrays

⇒ 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!!!++

Why println(array1) does not work?

⇒ Give address of array1 not its contents


Why array2 = array1 does not work? You need to use copyOf

⇒ Answer above.

Why comparing arrays using == does not work?


⇒ Again, above.

Command line arguments

⇒ public static void main(String[] args)


–Values passed to JAVA program from the command window.
–args[i] to access elements.
–args.length;
–Values are always String, need to covert if want something else that are not Strings.
–Example command line:
java FileName args1 args2 args3

Pass by reference vs pass by value


⇒By values:
–int, double, char, boolean is passed to methods by values.
–When passed to method is passing copy of values
–Change inside method does not affect primitives outside.

⇒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

⇒Mutable: Can be modified after creation. (array, StringBuilder)

⇒Immutable: Can’t be modified after creation.(String, Wrapper classes)

Wrapper classes: null keyword

⇒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

⇒BigInteger is an object, stores super large numbers


–IMMUTABLE!!!
–Use BigInteger methods to do calculations.
–Convert to String by Instance.toString (prefer)
–Covert to int Instance.intValue(); (very big number like 30 zeros behind can cause lost of
precision)

Mutable objects: Point, Rectangle (import java.awt)

⇒ Points: new Point(x, y); //x and y cor.


–Mutable.
⇒Rectangle:
–Rectangle(int x, int y, int width, int height);
–Rectangle(int width, int height);
–Rectangle(Rectangle r); //Rectangle that is copy of another Rectangle
–Mutable.
Class Diagram: UML diagram, attributes, methods

⇒ ‘+’ Public, ‘-’ Private,


–Underline: static.

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.

Getters/setters methods. Accessors/mutators

⇒ Use to access, and set private attributes in a class.

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)

Obj obj = new Obj


Obj = new Obj (a reference to a new obj so GC need)
String (immutable) vs StringBuilder(mutable)

⇒ 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 Interning (String pool)


⇒ String a = “Hello World” → Create hello World in memory, add to the String pool

String b = “Hello World” → Check, that there is Hello World in the String pool, make b

reference (arrow pointing) to Hello World in memory


String c=.... String d=....
→ In conclusion, there will only be one String object (Hello World) created, no matter how

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.

String methods vs StringBuilder methods

⇒ 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()

Class, instance, instantiation, instance variables/attributes/data fields

⇒ Search Google, you should know how to program these

Setters (mutators), getters (accessors) methods

⇒public void setter(type value){


This.value = value;
}

Public valueType valueGetter(){


Return this.value;
}

If static has to do className.value;


Instance variable vs class variable:

⇒Instance: each object has different instance variables.

⇒Class: share among all classes, have to be static

Information hiding (Data hiding): constructor.

⇒ 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.

Overloaded constructors: value constructor vs default constructor

⇒ public name(){ //this is default



}
Public name(String name){ //this is value

}

Public name(String name, int age){ //this is value



}

The name class has overloaded constructors because of 3 constructors.

== vs equals method

⇒ Comparing object Reference vs comparing actual Values or Contents, this is asked for String
toString method

⇒ Arrays.toString(array) // work perfectly if used with primitives, String


How it looks like: [element, element, element]
Objects in array need a custom method

⇒For objects that need a custom method.


–”@override” to override any toString methods already exited in JAVA before, no need to but
good practice.

You might also like