Java Programmer interview Questions

Source: Internet
Author: User
Tags anonymous assert final finally block

Talk about final, finally, finalize the difference

final-modifier (keyword) If a class is declared final, it means that it cannot derive a new subclass and cannot be inherited as a parent class. Thus a class cannot be declared as abstract and declared final. Declaring a variable or method final can guarantee that they will not be changed in use. A variable declared as final must be given an initial value at the time of declaration, and can only be read and not modified in future references. A method that is declared final is also used only and cannot overload

Finally-provides a finally block to perform any cleanup operations when the exception is processed. If an exception is thrown, the matching catch clause executes, and then the control enters the finally block (if any).

finalize-method name. Java technology allows you to use the Finalize () method to do the necessary cleanup before the garbage collector clears the object out of memory. This method is invoked by the garbage collector on this object when it is determined that the object is not referenced. It is defined in the Object class, so all of the classes inherit it. Subclasses override the Finalize () method to defragment system resources or perform other cleanup work. The Finalize () method is invoked on this object before the garbage collector deletes the object.

Anonymous Inner Class (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)?

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

Static Nested class and Inner class, the more the more the better (the face of the question some very general)

Nested class (generally C + +), Inner class (generally Java). The most important difference between a Java inner class and a C + + nested class is whether there is a reference to an external point.

Note: Static inner class (Inner Class) means that 1 creates an object of a static inner class, does not require an external class object, and 2 cannot access an external class object from an object in a static inner class

The difference between & and &&

& is a bitwise operator. && is a boolean logical operator.

The difference between HashMap and Hashtable

Are classes that are part of the map interface, which enables you to map unique keys to specific values.

HASHMAP classes are not categorized or sorted. It allows a null key and multiple null values.

Hashtable is similar to HASHMAP, but does not allow null keys and null values. It's also slower than HASHMAP, because it's synchronized.

The difference between Collection and collections

Collections is a Java.util class that contains a variety of static methods for collection operations.

Collection is a Java.util interface, which is the parent interface of various collection structures.

When do I use assert

An assertion is a statement that contains a Boolean expression, which is assumed to be true when the statement is executed. If the expression evaluates to False, then the system reports a assertionerror. It is used for debugging purposes:

ASSERT (a > 0); Throws an assertionerror if a <= 0

Assertions can be of two forms:

assert Expression1 ;
assert Expression1 : Expression2 ;

Expression1 should always produce a Boolean value.

Expression2 can be any expression that results in a value. This value is used to generate a String message that displays more debugging information.

The assertion is disabled by default. To enable assertions at compile time, you need to use the source 1.4 tag:

Javac-source 1.4 Test.java

To enable assertions at run time, you can use-enableassertions or-ea tags.

To choose to disable assertions at run time, you can use-da or-disableassertions tags.

To enable assertions in a system class, you can use-esa or-DSA tags. You can also enable or disable assertions on a package basis.

You can place assertions at any location that is expected to not be reached normally. Assertions can be used to validate parameters that are passed to a private method. However, assertions should not be used to validate parameters that are passed to the public method, because the public method must check its arguments, regardless of whether the assertion is enabled. However, you can test the post condition with assertions in a public method or in a Non-public method. In addition, assertions should not change the state of the program in any way.

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.