Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Java exception handling skills! ! ! !
Java exception handling skills! ! ! !
Simplicity and application of exception handling mechanism in 1 Java language (a computer language, especially for creating websites)

When a Java program violates the semantic rules of Java, the Java virtual machine will represent the error as an exception. Violation of semantic rules includes two situations. One is the built-in semantics of Java class library. For example, if the subscript of an array is out of bounds, an IndexOutOfBoundsException will be thrown. NullPointerException will be thrown when accessing an empty object. Another case is that Java allows programmers to extend this semantic check. Programmers can create their own exceptions and freely choose when to throw exceptions with the throw keyword. All exceptions are subclasses of java.lang.Thowable

Similarities and differences between 2.Java interface and C++ virtual class.

Because Java does not support multiple inheritance, and it is possible that a class or object will use methods or properties in several classes or objects, the existing single inheritance mechanism can not meet the requirements. Compared with inheritance, the interface is more flexible because there is no implementation code in the interface. When a class implements an interface, it should implement all the methods and properties in the interface. By default, the properties in the interface are public static, and by default, all methods are public. A class can implement multiple interfaces.

3. Advantages and principles of garbage recycling. And consider two recycling mechanisms.

A notable feature of Java language is the introduction of garbage collection mechanism, which solves the most headache memory problem for c++ programmers, so that Java programmers no longer need to consider memory management when writing programs. Because of the garbage collection mechanism, objects in Java no longer have the concept of "scope", and only references to objects have "scope". Garbage collection can effectively prevent memory leakage and effectively use available memory. The garbage collector usually runs as a single low-level thread, which cleans up and recycles objects that have died or been unused for a long time in the memory heap under unpredictable circumstances. Programmers cannot call the garbage collector in real time to collect garbage from one object or all objects. Recycling mechanisms include generational copy garbage collection, marked garbage collection and incremental garbage collection.

4. The method of thread synchronization.

Wait (): Let a thread wait and release the lock of the held object.

Sleep (): This is a static method to put the running thread to sleep. Call this method to catch InterruptedException.

Notify (): Wake up a waiting thread. Note that when calling this method, you can't wake up a waiting thread accurately, but the JVM decides which thread to wake up, not according to the priority.

Allnotity (): Wake up all threads that are waiting. Note that instead of giving all awakened threads a lock on an object, let them compete.

5. What's the difference between an error and an exception?

Errors mean system-level errors and exceptions that programs don't have to handle.

An exception is an exception that a program needs to catch or handle.

6. In java, a class is declared as the final type. What does this mean?

Indicates that the class cannot be inherited and is a top-level class.

7. What's the difference between heap and stack?

Stack is a linear set, and adding and deleting elements should be done in the same paragraph. Stacks are processed in a last-in, first-out manner. Heap is an integral part of the stack.

8. Talk about the difference between final, finally and finalize.

Final-Modifier (keyword) If a class is declared as Final, it means that it can no longer derive new subclasses or inherit as a parent class. Therefore, a class cannot be declared abstract and final at the same time. Declaring variables or methods as final ensures that they will not be changed in use. Variables declared as final must be given an initial value at the time of declaration, and can only be read and cannot be modified in future references. Methods declared as final can only be used and cannot be overloaded.

Finally— Provides a finally block to perform any cleanup operations during exception handling. If an exception is thrown, the matching catch clause will be executed, and then control will enter the finally block (if any). Done-Method name. Java technology allows you to use the finalize () method to clean up the objects before the garbage collector clears them from memory. The garbage collector calls this method when it is determined that the object is not referenced. It is defined in the Object class, so all classes inherit it. Subclasses override the finalize () method to organize system resources or perform other cleanup tasks. The finalize () method is called on the object before it is deleted by the garbage collector.

9. Anonymous inner class (Anonymous inner class)

Can you extend other classes and implement interfaces?

An anonymous inner class is an inner class without a name. You cannot extend other classes, but an inner class can be implemented as an interface by another inner class.

10. The difference between static nested classes and inner classes

Nested classes (usually C++), inner classes (usually JAVA). The biggest difference between Java inner classes and C++ nested classes is whether there are external references.

Note: Static inner class means that 1 creates an object of static inner class without external class objects, and 2 external class objects cannot be accessed from objects of static inner class.

This article comes from www.wangluosky.cn, Online Sky Tutorial Network. Original source: /show.asp? id= 1237