Java re-learning--random face question

Source: Internet
Author: User

1, final, finally, finalize the difference

Final-are modifiers that can modify variables, methods, and classes.

The final class can no longer derive a new subclass that is not a parent class;

The final variable must be given the initial value at the time of declaration or assigned in the constructor method, and can only be read in subsequent references and cannot be modified.

The final method cannot be overloaded.

The finally-provides a finally block during exception handling to perform the closing operation. If an exception is thrown, the matching catch clause executes, and finally is entered into the finally block (if there is finally) as long as the try code block is executed (either return or not).
Finalize-is a method name defined in the object class that is called on the object before the object is deleted by the garbage collector. Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector to this object when it determines that the object is not referenced.

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

The anonymous inner class is an inner class without a name. Cannot extends (inherit) Other classes also cannot explicitly call implements, but an inner class can implement an interface.

3, the difference between & and &&

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

When used to connect a judgment statement,

& Two conditions to Judge;&& the first condition is false, directly determine the condition false, no longer determine the second condition.

Similarly, | and | | It's the same. | Two conditions to be judged; | As long as the first condition is true, the entire condition is directly judged to be true, and the second condition is no longer determined.

4. The difference between HashMap and Hashtable

Are classes that are part of the map interface and implement a unique key mapping to a specific value.
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 is also slower than HASHMAP, because it is synchronous.

5. The difference between Collection and collections

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

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

6. When to use Assert

An assertion is a statement that contains a Boolean expression that 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:
To enable assertions at run time, add-enableassertions or-ea tags to the VM arguments.
To choose to disable assertions at run time, you can use the-da or-disableassertions tags, or none, because the default is off.

7. What is GC? Why do you have a GC?

GC is a garbage collector. Java programmers don't have to worry about memory management because the garbage collector is automatically managed. To request garbage collection, you can call one of the following methods: System.GC () or Runtime.getruntime (). GC ()

8, String s = new String ("Wyt"), creates several string Object

Two objects, one is "Wyt", and the other is a reference object pointing to "Wyt" s.

9, Math.Round (11.5) equals how much? Math.Round (-11.5) equals how much

Math.Round (11.5) returns (long) 12,math.round (-11.5) return (long)-11;

This "rounding" of the "four homes" are the same, but the "five into" is the big value.

10, short S1 = 1; S1 = s1 + 1;

Short S1 = 1; S1 + = 1;

The first error, S1 is a short type, s1+1 is an int, can not be explicitly converted to short type. Need to be modified to S1 = (short) (S1 + 1).

The second one is correct.

11, the array has no length () This method, string has no length () This method
The array does not have the length () method, which has the length property.
String has the length () method.

12, the difference between overload and override. Whether the overload method can change the type of the return value
Override override and overload Overloade of the method are different manifestations of Java polymorphism. Overrides are a representation of polymorphism between a parent class and a subclass, and overloading is a representation of polymorphism in a class.

If you define a method in a subclass that has the same name and parameters as its parent class, we say that the method is overridden. When an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked". If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, which are called overloads of the method. The overloaded method is to change the type of the return value.

13, the most common to the runtime exception

ArithmeticException, Arraystoreexception, Bufferoverflowexception, Bufferunderflowexception, CannotRedoException, Cannotundoexception, ClassCastException, Cmmexception, Concurrentmodificationexception, DOMException, Emptystackexception, IllegalArgumentException, Illegalmonitorstateexception, Llegalpathstateexception, Illegalstateexception,imagingopexception, Indexoutofboundsexception, MissingResourceException, Negativearraysizeexception, Nosuchelementexception, NullPointerException, Profiledataexception, ProviderException, Rasterformatexception, SecurityException, SystemException, Undeclaredthrowableexception, UnmodifiableSetException, Unsupportedoperationexception

14. Difference between error and exception
Error indicates a serious problem in situations where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such situations.
Exception represents a design or implementation issue. That is, it means that if the program runs normally, it never happens.


15, List, Set, whether the map inherits from collection interface
List,set inherits from collection, and map is not

16. The difference between abstract class and interface
A class that declares the existence of a method and does not implement it is called an abstract class, which is used to create a class that embodies some basic behavior, declares a method for that class, but does not implement the class in that class. An instance of the abstract class cannot be created. However, you can create a variable whose type is an abstract class that points to an instance of a specific subclass. Cannot have abstract constructors or abstract static methods. The subclasses of the abstract class provide implementations for all abstract methods in their parent class, otherwise they are also abstract classes. Instead, implement the method in the subclass. Other classes that know their behavior can implement these methods in the class.
An interface (interface) is a variant of an abstract class. In an interface, all methods are abstract. Multiple inheritance can be obtained by implementing such an interface. All the methods in the interface are abstract, without a program body. An interface can only define static final member variables. The implementation of an interface is similar to a subclass, except that the implementation class cannot inherit the behavior from the interface definition. When a class implements a special interface, it defines the method (which is given by the program body) to all such interfaces. It can then invoke the interface's method on any object that implements the interface's class. Because of an abstract class, it allows you to use the interface name as the type of the reference variable. The usual dynamic binder will take effect. A reference can be converted to an interface type or converted from an interface type, and the instanceof operator can be used to determine whether the class of an object implements an interface.

17, the abstract method can be static at the same time, whether it can be native at the same time, whether it can be synchronized
Are not

18, whether the interface can inherit interface, whether abstract class can implement (implements) interface, abstract class can inherit entity class (concrete Class)
Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, and the premise that an abstract class can inherit an entity class is that the entity class must have a definite constructor.

19. Start a thread with run () or start ()
Starting a thread is calling the start () method so that the virtual processor represented by the thread is in a running state, which means it can be dispatched and executed by the JVM. This does not mean that the thread will run immediately. The run () method can produce a flag that must be exited to stop a thread.

20. Whether the constructor constructor can be override
The constructor constructor cannot be inherited and therefore cannot override override, but can be overloaded with overload.

21. Whether the string class can be inherited
The string class is the final class and cannot be inherited.

22. There is a return statement in try {}, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return
Executes before the return.


23. Use the most efficient method to calculate 2 times 8
2 << 3

24, two object values are the same (x.equals (y) = = true), but can have different hash code, this sentence is right
Right. Because this depends on whether the class of the object overrides equals, it is possible that the same hashcode can be equal if overridden. However, if there is no Override, the equals return true must have the same hash code.

25, when an object is passed as a parameter to a method, this method can change the properties of the object, and can return the changed results, whether this is a value or a reference to pass
is a value pass. The Java programming language only passes parameters by value. When an object instance is passed as a parameter to a method, the value of the parameter is a reference to the object. The contents of the object can be changed in the called method, but the object's reference is never changed.


26, whether the Swtich can function on a byte, whether it can function on a long, whether it can function on a string
In switch (exp), exp is an integer expression. So the arguments passed to the switch and case statements should be int, short, char, or byte, as well as the enumeration type. Long,string can not act on Swtich.

27, write a singleton

The main purpose of the singleton mode is to ensure that only one instance of a class is present in a Java application.
The constructor for a class of the singleton pattern is private, and it has a static private class variable that gets an instance reference to it through a public getinstance method, and then calls the method in it.
The first form of:

Class Singleton {    private static Singleton instance = new Singleton ();    public static Singleton getinstance () {        return instance;    }    Private Singleton () {    }}    

The second form of:

Class Singleton {    private static Singleton instance = null;    public static Singleton getinstance () {        if (instance = = null) {            instance = new Singleton (); 
        
         } return
          instance;} private Singleton () {}}  
            

The third form of:

Class Singleton {    private static Singleton instance = null;    public static Singleton getinstance () {        if (instance = = null) {            synchronized (Singleton.class) {                if (instance = = null) {instance = new Singleton ()}}} return instance;} private Singleton () { }}

Other forms:

Define a class whose constructor is private and all methods are static, which of course is a bit like a tool class. But the first form is generally considered to be more secure.

Java re-learning--random face question

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.