Java basics-001, java-001
I. differentiate final, finally, and finalize
1. Keyword final
The Java keyword final can be used for variables, classes, or methods, but its meaning may be different.
-Used for variables: Once initialized, the variable value cannot be modified.
-Method used: This method cannot be overwritten by the quilt class (overrride)
-Used for class: This class cannot be derived from a subclass.
2. Keyword finally
If the keyword finally is used together with the try/catch statement, even if an exception is thrown, a code segment can be executed. The finally statement block is executed after the try/catch statement block and before the control is handed back.
3. finalize method
Before the object is actually destroyed, the automatic garbage collector calls the finalize () method. Therefore, a class can override the finalize () method of the Object class to define specific behavior during garbage collection.
Protected void finalize () throws Throwable {/* close the file to open, release resources, and other operations */}
Ii. differentiate between heavy load and rewriting
1. overloading means that the two methods have the same name but different parameter types or numbers.
1 1 public double computeArea(Circle c ){...........}2 3 2 public double computeArea(Square s){..........}
2. overriding means that a method has the same name and function signature as the method of the parent class.
Iii. Common collection frameworks
1. ArrayList: ArrayList is an array that can be dynamically adjusted. With the insertion of elements, the array will be scaled in due time.
1 ArrayList <String> myArray = new ArrayList <String> (); 2 myArray. add ("one"); 3 myArray. add ("two"); 4 System. out. println (myArray. get (0); // print "one"
2. Vector: the vector is very similar to the ArrayList, except that the former (Vector) is synchronous (synchronized ). The syntax is almost the same.
1 Vector<String> myVector = new Vector<String>();2 myVector.add("one");3 myVector.add("two");4 System.out.println(myVector.get(0));
3. Generator list: Java built-in generator list class. Some iterator syntax will be introduced during use.
1 LinkedList<String> myLinkedList = new LinkedList<String>();2 myLinkedList.add("two");3 myLinkedList.add("one");4 Iterator<String> iter = myLinkedList.iterator();5 while(iter.hasNext()){6 System.out.println( iter.next()); 7 }
4. HashMap: The HashMap set is widely used in various scenarios.
1 HashMap<String,String> map = new HashMap<String,String>();2 map.put("one","one");3 map.put("two","two");4 System.out.println(map.get("one"));