Java basics-001, java-001

Source: Internet
Author: User

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"));

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.