- Basics in Computer Memory
- ordered sequence of bytes
- unique address
- key difference between high & low level programming languages
- whether programmer has to deal with memory addressing directly
- Numbers
- Binary
- Octal (0,1,2,3,4,5,6,7)
- Hexadecimal (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F)
- Memory allocation
- structure:
- Q1 : Why is it designed like this?
- A1: The memory used by local variables will be automatically released without additional memory management operations in the stack area.
- see the structure in the other direction
- When a new object is created, Java allocates space from heap.
- When a method is called, Java allocates a new block of memory called a stack frame to hold its local variables.
- When a method returns, its stack frame is erased. Stack frames come from stack
- About Stack vs Heap
- It is estimated that there will be other essays talking about each of them
- Q1 : Why is it designed like this?
- structure:
- Garbage Collection
- When memory is running short, Java does garbage collection
- Mark the objects referenced by variables on stack or in static storage.
- Sweep all objects in the heap, reclaim unmarked objects (garbage)
- When memory is running short, Java does garbage collection
- Arrays
- Features:
- Ordered and fixed length
- Homogeneous: Each value in the array is of the same type
- An array is characterized by
- ◦ Element type
- ◦ Length
- Default values in initialization
- numerics 0
- boolean false
- objects null
- Two-dimensional arrays:
- Each element of an array is an array (of the same dimension)
- Typical operations on an array
- } Traversal: processing each element
- } Search: finding an element by using the given index or key
- } Insertion: adding new elements
- } Deletion: removing elements
- } Sorting: arranging elements with a type of order [DS:3.3] Sorting with Arrays
- } Merging: combining two arrays into a single array
- Features:
- Implementation
- Use named constant to declare the length of an array.
- private static final int N_JUDGES = 5; double[] scores = new double[N_JUDGES];
- Identifying an element by an integer number or an expression
- array[index], array[(a+b)/2]
- Cycling through array elements
- Easier code attention
- Two typical starting numbers of indexing
- ◦ 0: Java, Python, C/C++, …
- ◦ 1: Matlab, Julia, R, …
- Passing Arrays as Parameters?
- [DS:3.1]Passing objects (references) versus primitive type (values) as parameters.
- [DS:3.1]Passing objects (references) versus primitive type (values) as parameters.
- Pass-by-Value
- ◦ copied to another location of the memory
- ◦ accesses only the copy, no effect on the original value
- change(i){ i=5;}
- ◦ copied to another location of the memory
- Pass-by-Reference
- memory address is passed to that function, so the function changes the actual variable
- example: stringBuffer.append
- For primitive types
- pass-by-value, which means a copy of the value is passed, and the original value is not changed.
- For objects,
- although the value passed is also a value (the memory address of the object, i.e., a reference), any modifications made to the object within the method will affect the original object because this value can be used to access and modify the object's state. This behavior is similar to pass-by-reference [DS:3.2]Understanding Java's Memory Management and Array Return Mechanism
- but strictly speaking, Java does not have true pass-by-reference(the value of address instead of the referrence itself).
- although the value passed is also a value (the memory address of the object, i.e., a reference), any modifications made to the object within the method will affect the original object because this value can be used to access and modify the object's state. This behavior is similar to pass-by-reference [DS:3.2]Understanding Java's Memory Management and Array Return Mechanism
- In Java, whether something remains unchanged or not depends on
- whether the first one has been assigned an address
- whether the second one has either been allocated a new address or is being pointed to the address of the first one.
- The key factor is whether the addresses of the two are the same or not.
- Use named constant to declare the length of an array.
- Examples
- String Array:
- String array is an array holding a fixed number of strings or string values
- ◦ One structure commonly used in Java
- ◦ Even the arguments of the Java ‘main’ function is a String Array public static void main(String args[ ]){…}
- Each string is an object!
- codes
- String array is an array holding a fixed number of strings or string values
- Jagged Arrays
- } Jagged arrays have rows of unequal length
- ◦ each row has a different number of columns, or entries
- } Jagged arrays are allowed in Java
- } Jagged arrays have rows of unequal length
- String Array:
- Sorting Strategies [DS:3.3]Important! Sorting with arrays
[DS: 3]Addressing & Array &Sorting
最新推荐文章于 2024-05-19 23:31:32 发布