Java Trivia points

Source: Internet
Author: User
Tags thread class

First of all, both classes implement the list interface, and the list interface has three implementation classes, namely ArrayList, Vector, and LinkedList. The list is used to hold multiple elements, to maintain the order of elements, and to allow the repetition of elements. The related differences between the 3 specific implementation classes are as follows:

    1. ArrayList is the most commonly used list implementation class, implemented internally by an array, which allows for fast random access to elements. The disadvantage of an array is that there can be no interval between each element, and when the array size does not meet the need to increase storage capacity, it is necessary to copy the data of the array into the new storage space. When inserting or deleting elements from the middle of a ArrayList, it is necessary to copy, move, and cost the array. Therefore, it is suitable for random lookups and traversal, and is not suitable for insertions and deletions.
    2. Vectors, like ArrayList, are also implemented through arrays, except that it supports thread synchronization, where only one thread can write vectors at a time, avoiding inconsistencies caused by simultaneous writing of multiple threads, but achieving synchronization requires a high cost, so Accessing it is slower than accessing ArrayList.
    3. LinkedList is used to store data in a linked list structure, which is very suitable for the dynamic insertion and deletion of data, and the random access and traversal speed is relatively slow. In addition, he provides methods that are not defined in the list interface, specifically for manipulating the header and footer elements, and can be used as stacks, queues, and bidirectional queues.
    4. Look at the Java source code, and find that when the size of the array is not enough, you need to re-establish the array, and then copy the elements into the new array, the size of the ArrayList and vector extended array is different.
    5. The differences between ArrayList and vectors are as follows:

      1. ArrayList default is 50% + 1 when memory is insufficient, vector is 1 time times the default extension.
      2. The vector provides the indexof (obj, start) interface, ArrayList not.
      3. Vectors are thread-safe, but in most cases do not use vectors, because thread safety requires greater overhead.
2. Exception issues
    1. Java runtime Exceptions are exceptions that might be thrown when a Java virtual machine is working correctly.

      Java provides two kinds of exception mechanisms. One is a run-time exception (runtimeexepction) and one is an inspection exception (checked execption).

      Check exception: The IO exception that we often encounter and the SQL exception are check-in exceptions. For this exception, the Java compiler requires that we have to catch these exceptions, so we can only write a bunch of catch to catch these exceptions, whether we like it or not.

      Run-time exception: we can not handle it. When such an exception occurs, it is always taken over by the virtual machine. For example: No one has ever dealt with a nullpointerexception exception, which is a run-time exception, and this exception is one of the most common exceptions.

      Runtimeexecption under the Java.lang bag.

    2. It is very easy to find out what our common 5 run-time exceptions are.

      Example: ClassCastException (class conversion exception)

      Indexoutofboundsexception (array out of bounds)

      NullPointerException (null pointer)

      Arraystoreexception (data store exception, type inconsistency when manipulating arrays)

      There are also bufferoverflowexception exceptions for IO operations

3. The following is a run-time exception provided by the Java Virtual machine

Annotationtypemismatchexception,
ArithmeticException,
Arraystoreexception,
Bufferoverflowexception,
Bufferunderflowexception,
Cannotredoexception,
Cannotundoexception,
ClassCastException,
Cmmexception,
Concurrentmodificationexception,
Domexception,
Emptystackexception,
Enumconstantnotpresentexception,
Eventexception,
IllegalArgumentException,
Illegalmonitorstateexception,
Illegalpathstateexception,
IllegalStateException,
Imagingopexception,
Incompleteannotationexception,
Indexoutofboundsexception,
Jmruntimeexception,
Lsexception,
Malformedparameterizedtypeexception,
Mirroredtypeexception,
Mirroredtypesexception,
MissingResourceException,
Negativearraysizeexception,
Nosuchelementexception,
Nosuchmechanismexception,
NullPointerException,
Profiledataexception,
ProviderException,
Rasterformatexception,
Rejectedexecutionexception,
SecurityException,
SystemException,
Typeconstraintexception,
Typenotpresentexception,
Undeclaredthrowableexception,
Unknownannotationvalueexception,
Unknownelementexception,
Unknowntypeexception,
Unmodifiablesetexception,
Unsupportedoperationexception,
Webserviceexception

Seeing so many anomalies, it is very easy to find out our common 5 run-time exceptions.

3. What is the difference between sleep () and wait () sleep and wait

1. Sleep refers to when the thread is called, the CPU does not work, the image is described as "occupy the CPU Sleep", at this time, the system's CPU part of the resources are occupied, other threads can not enter, will increase the time limit.
Wait means that the thread is in the waiting state and is visually described as "waiting for CPU", at which point the thread does not occupy any resources and does not increase the time limit.
So
Sleep (100L) means: Consumes CPU, thread sleeps 100 milliseconds
Wait (100L) means: CPU not occupied, thread waits 100 milliseconds

2. For the Sleep () method, we first need to know that the method belongs to the thread class. The wait () method is part of the object class.

The sleep () method causes the program to pause execution for the specified time, giving up the CPU to that other thread, but his monitoring state remains, and when the specified time is up, it will automatically resume its running state.

During the call to the sleep () method, the thread does not release the object lock.

When the Wait () method is called, the thread discards the object lock, enters the waiting lock pool waiting for the object, and the thread only enters the object lock pool when the Notify () method is called for this object.

Gets the object lock into the running state.

What do you mean?

To give a few examples:

 1/** 2 * 3 */4 package com.b510.test; 5 6/** 7 * Differences between sleep () and wait () in Java 8 * @author hongten 9 * @date 2013-12-1010 */11 public class TestD {pub Lic static void Main (string[] args) {new Thread1 ()). Start (); try {THREAD.SL EEP (Exception e) {e.printstacktrace ();}20 new Thread (New Threa  D2 ()). Start ();}22 the private static class Thread1 implements RUNNABLE{24 @Override25 public    void Run () {synchronized (Testd.class) {System.out.println ("Enter thread1 ..."); System.out.println ("Thread1 is waiting ..."); try {30//Call the Wait () method, the thread discards the object lock and enters Wait for this object to lock the pool to TestD.class.wait (); (Exception e) {E.printstacktrac E ();}35 System.out.println ("Thread1 is going on ..."); System.out.priNtln ("THREAD1 is over!!!");         PNS}38}39}40 private static class Thread2 implements RUNNABLE{42 @Override43 public void Run () {synchronized (Testd.class) {System.out.println ("Enter Thread2. ); System.out.println ("Thread2 is sleep ..."); 47//Only after calling the Notify () method for this object does this thread enter the object lock pool preparation Gets the object lock into the running state. TestD.class.notify (); 49//==================50//Difference 51/ /If we put the code: TestD.class.notify () and commented out, that is, Testd.class calls the Wait () method, but does not call the Notify () 52//method, the thread is always in the suspended state. The try {//sleep () method causes the program to pause execution for the specified time, yielding the CPU to that other thread, 55//But his monitoring status remains , the run state is automatically restored when the specified time is up. 56//During the call to the sleep () method, the thread does not release the object lock. Thread.Sleep (Exception e) {$ e.printstacktra CE ();}61 SystEm.out.println ("Thread2 is going on ..."); System.out.println ("THREAD2 is over!!!"); 63}64}65}66}

Operating effect:

Enter Thread1...thread1 is Waiting...enter thread2....thread2 are sleep....thread2 is going on....thread2 are over!!! Thread1 is going on .... THREAD1 IS-over!!!

If the code is commented out:

1 TestD.class.notify ();

Operating effect:

Enter Thread1...thread1 is Waiting...enter thread2....thread2 are sleep....thread2 is going on....thread2 are over!!!

And the program is always in a pending state.

the difference between equals and = = in 4.Java

1. "= =" compares the memory address of the object (heap) stored in the value "variable (stack) memory"
equal is used to compare whether the values of two objects are the same "not more than the address"

"Special note" The Equals method in the object class is the same as the "= =", and the String class, the integer class, and so on, are overridden by the Equals method to make the equals and "= =" different, so when you create the class yourself, The Equals method of object is automatically inherited, and you must override the Equals method if you want to implement a different equals comparison.

"= =" runs faster than "equal" because "= =" is only a comparison reference.

Java Trivia points

Related Article

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.