Java Basic Question Summary

Source: Internet
Author: User
Tags switch case thread class singleton class in java

1. Wait () differs from sleep () in Java :
A: Both are paused threads that are currently running. Sleep () is a method of the thread class, and wait () is a method of the object class. Call Sleep (), the thread does not release the object lock, the other thread still cannot access the object, call Wait (), the thread frees the object lock, and other threads can access the object.

2. Which bigdecimal or double is the data type that represents the price in Java?
A: If you need to calculate the results accurately, use the BigDecimal class, but the comparison consumes memory and performance, otherwise you can use float or double directly.

3. A = the difference between A + B and a + = b?
A: In-memory analysis: If the result of the addition operation is greater than the maximum value of a, then A+b will have a compile error, but A + = B is no problem, + = implicitly converts the result type of the plus operation to the type holding the result.

4. Can we use String in Switch?
A: Starting with Java 7, we can use strings in the switch case, but this is just a syntactic sugar. The internal implementation uses the hash code of the string in switch. Starting with Java 5, enum type Enum was introduced in Java.

5. What is the difference between weakreference (weak reference) and softreference (soft reference) in Java?
A: Both WeakReference and softreference are useful for improving GC and memory efficiency. WeakReference, once the last strong reference is lost, it will be recycled by GC, and soft references can be deferred until the JVM is out of memory and collected by GC.

6. What is the difference between heap and stack in Java?
A: The heap and stack in the JVM are different memory areas. Stacks are used to hold variables of some basic types of methods and reference variables of objects, and when the scope of the variable is exceeded, Java automatically frees up the allocated memory space for the variable, and heap memory is used to hold the object created by new, which is managed by the JVM's GC.

7. What is the difference between "a==b" and "A.equals (b)"?
A: If both A and B are objects, a==b is a reference to the comparison of two objects, and only if A and B point to the same object in the heap will return true, whereas A.equals (b) is more logically consistent and two objects must have the same hashcode to return true, it is often necessary to re- Write the Equals () method to provide a comparison of logical consistency.

8. What is the difference between ArrayList and LinkedList?
A: All implementations of the list interface.
1.ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on linked list.
2. For random access get and set,arraylist feel better than LinkedList, because linkedlist to move the pointer.
3. Add and Remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.

9. What is the difference between string and StringBuilder, StringBuffer?
A: String is a read-only string, which means that string content referenced by string cannot be changed.
The string object represented by the Stringbuffer/stringbuilder class can be modified directly. StringBuilder is introduced in Java 5, and it is exactly the same as the StringBuffer method, except that it is used in a single-threaded environment because all aspects of it are not synchronized decorated. Therefore, it is also more efficient than stringbuffer.
On-thread security, StringBuilder threads are unsafe, and stringbuffer is thread-safe.


10. What are the similarities and differences between abstract classes and interfaces (interface)?
A: Abstract classes and interfaces cannot be instantiated, but you can define references to abstract classes and interface types. If a class inherits an abstract class or implements an interface that requires all of its abstract methods to be implemented, the class still needs to be declared as an abstract class. Interfaces are more abstract than abstract classes, because constructors can be defined in abstract classes, can have abstract methods and concrete methods, and constructors are not defined in an interface, and the methods are all abstract methods. Members in an abstract class can be private, default, protected, public, and members of the interface are all public. Member variables can be defined in an abstract class, whereas member variables defined in an interface are actually constants. Classes with abstract methods must be declared as abstract classes, and abstract classes may not necessarily have abstract methods.

11. Write a singleton class in Java.
For:
-A Hungry man type single case

 Public class Singleton {  private  Singleton () {}  privatestaticnew single    ton ();     publicstatic  Singleton getinstance () {  return  instance; }}

-Lazy Type Single case

 Public classSingleton {Private StaticSingleton instance =NULL; PrivateSingleton () {} Public Static synchronizedSingleton getinstance () {if(Instance = =NULL) Instance =NewSingleton (); returninstance; }}

Java Basic Question Summary

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.