Java Basic Knowledge Chapter

Source: Internet
Author: User

1, the difference between & and &&?

Both:& and && can be used as a logical AND operator, representing logic and (and);

&& also has a short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated;

& is a bitwise operator that represents a bitwise AND operation.

2, whether the Swtich can function on a byte, whether it can function on a long, whether it can function on a string?

Answer: in switch (EXPR1), expr1 is an integer expression. An integer expression can be an int primitive type or an integer wrapper type.

Because, Byte,short,char can be implicitly converted to int, these types and their wrapper types can. Long,string cannot be implicitly converted to an int type, so it cannot be applied to Swtich statement types.

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

Answer: 2 << 3

4, = = and equals method difference?

String a=New string ("abc"); String b=new string ("abc");

These two lines of code create two objects, which are two different objects whose first address is different.

Therefore, the expression A==b returns False, and the contents of the two objects are the same, and A.equals (b) returns True.

5,. The difference between collection and collections?

A: Collection is the parent interface of the collection class, and its interface is primarily set and list

Where list must hold elements in a specific order, and set cannot contain duplicate elements.

The collections class is a helper class for a collection class. It provides a series of static methods for searching, sorting, and threading security of various collections.

6. What is the difference between overload and override? Can the overloaded method change the type of the return value?

A: The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism.

Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class.

If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding).

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 is called a method overload (overloading).

The overloaded method is to change the type of the return value

7. Can I inherit the String class?

A: The string class is the final class and cannot be inherited

8. 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?

A: Will execute and execute before return

9, 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, then this is the value of the pass or reference pass?

A: The value is passed. The Java programming language has only value-passing parameters.

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.

10, the difference between ArrayList and Vector, HashMap and Hashtable?

A: ArrayList and vectors are mainly from two aspects.

I. Synchronization: vector is thread-safe, that is, synchronous, and ArrayList is a thread program is not secure, not synchronous

Two. Data growth: When growth is needed, vectors grow by default to the original, while ArrayList is half the original

HashMap and Hashtable are mainly from three aspects.

I. Historical reasons: Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2

Two. Synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is a thread program is not secure, not synchronous

Three. Value: Only HashMap can let you use NULL as the key or value of an entry for a table

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

A: GC is garbage collection meaning (Gabage Collection), memory processing is a problem for programmers, forget or wrong memory recycling can cause the program or system instability or even crash, The GC functionality provided by Java can automatically monitor whether an object exceeds the scope to achieve the purpose of automatically reclaiming memory, and the Java language does not provide a way to release the displayed operation of the allocated memory.

12. Introduce the collection FrameWork in Java (including how to write your own data structure)?

Answer: The Collection framework is as follows:

Collection

├list

│├linkedlist

│├arraylist

│└vector

│└stack

└set

Map

├hashtable

├hashmap

└weakhashmap

Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements)

Map provides a key-to-value mapping.

1.3. What is the difference between string and StringBuffer?

Answer: The length of the string is immutable and the length of the StringBuffer is variable.

If you are frequently manipulating the contents of a string, especially if the content is to be modified, use StringBuffer, and if you need a string at the end, use the ToString () method of StringBuffer

14, talk about final, finally, finalize the difference

A: final-modifier (keyword) If a class is declared final, it means that it can no longer derive a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared abstract and declared final. Declaring variables or methods as final ensures that they are not changed in use. A variable declared as final must be given an initial value at the time of declaration, and can only be read in subsequent references and cannot be modified. A method that is declared final can also be used only and cannot be overloaded

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

The finalize-method name. 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. 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 work. The Finalize () method is called on the object before the garbage collector deletes the object.

15. What are the similarities and differences between abnormal operation and general anomaly?

A: An exception indicates an unhealthy state that may occur during a program run, and a run-time exception that represents an exception that might be encountered in a virtual machine's usual operation is a common run error.

The Java compiler requires the method to declare a non-runtime exception that might occur, but does not require that a runtime exception that is not caught to be thrown must be declared.

16, say Arraylist,vector, LinkedList storage performance and characteristics

A: Both ArrayList and vectors use arrays to store data, which is larger than the actual stored data in order to add and insert elements, both of which allow the element to be indexed directly by ordinal, but the insertion element involves memory operations such as array element movement, so the index data is fast and the data is inserted slowly.

Vector due to the use of the Synchronized method (thread-safe), usually performance is worse than ArrayList.

LinkedList uses two-way linked lists for storage, index data by ordinal requires forward or backward traversal, but when inserting data, you only need to record the item's front and rear items, so the insertion speed is faster.

17, the difference between HashMap and Hashtable?

A: HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface, the main difference is that the HASHMAP allows null (NULL) key value (key), because of non-thread security, the efficiency may be higher than Hashtable.

HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed.

HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding.

The biggest difference is that the Hashtable method is synchronize, and HashMap is not, when multiple threads access Hashtable, they do not need to synchronize their methods, and HashMap must provide external synchronization.

18. Are there several types of streams in Java? The JDK provides some abstract classes for inheritance for each type of stream, tell me what classes they are.

A: byte stream, character stream. The byte stream inherits from the InputStream OutputStream, and the character stream inherits from the InputStreamReader OutputStreamWriter. There are many other streams in the java.io package, mainly to improve performance and ease of use.

19, List, MAP, set three interfaces, access to elements, what are the characteristics of each?

A: List holds elements in a particular order and can have repeating elements. Set cannot have duplicate elements, internal ordering. Map holds the Key-value value, and value can be multivalued.

Java Basic Knowledge Chapter

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.