Android-------Java frequently asked basic questions

Source: Internet
Author: User

1.What is the difference between "= =" and Equals method?

The = = operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator.

The Equals method is used to compare the contents of two separate objects, as compared to two people whose looks are the same, compared to the two objects that are independent of each other.

2, access modifier public,private,protected, and do not write (default) when the difference?

The difference is as follows:

Scope current similar Bun class other

Public√√√√

Protected√√√x

Default√√xx

Private√xxx

Defaults to default when members of a class do not write access adornments. The default is equivalent to exposing (public) to other classes in the same package, and for other classes that are not in the same package are equivalent to private (private). A protected (protected) subclass is equivalent to exposing a class that is not a parent-child relationship in the same package as private.

3. Is String the most basic data type?

No. There are only 8 basic data types in Java: Byte, short, int, long, float, double, char, Boolean, except for the base type (primitive type) and the enumeration type (enumeration type). All that is left is the reference type (reference type).

4. Explain the usage of the in-memory stack (stack), heap, and static storage.

Usually we define a variable of a basic data type, a reference to an object, and a field save for a function call to use the in-memory stack space, while the object created by the new keyword and the constructor is placed in the heap space, and the literal in the program (literal) is written as 100, "Hello" And constants are placed in a static storage area. Stack space operation is the fastest but also very small, usually a large number of objects are placed in the heap space, the entire memory, including the hard disk of virtual memory can be used as heap space.

String str = new string ("Hello");

In the above statement, STR is placed on the stack, the string object created with new is placed on the heap, and the literal "hello" is placed in the static storage area.

Supplement: A technique called escape analysis is used in newer versions of Java to put some local objects on the stack to improve the operational performance of the object.

5, briefly describe the following keywords use try, catch, throw, throws, finally

Try: Protect the code, and the code in try no longer executes if there is an exception to a line of code in the try. b) Catch: Catch Exception, when an exception occurs in try, the catch is responsible for catching the exception and processing.
Throw: Throws an exception.
Throws: Declares an exception.
Finally: Finally, no matter if there is an exception in the try.

6, LinkedList and ArrayList difference?

Both ArrayList and LinkedList implement the list interface, but they are somewhat different.

(1) ArrayList is an index-based data structure supported by array, so it provides random access to elements with an O (1) complexity, but LinkedList stores a series of node data, each connected to the previous and next node. So, although there is a way to get an element using an index, the internal implementation is traversing from the starting point, traversing the node to the index and returning the element, with a time complexity of O (n), which is slower than ArrayList.

(2) inserting, adding, and deleting an element in LinkedList is faster than ArrayList, because when an element is inserted in the middle, it does not involve changing the size of the array or updating the index.

(3) LinkedList consumes more memory than ArrayList because each node in the LinkedList stores references to the front and back nodes.

7. What are the aspects of object-oriented features?

The object-oriented features are mainly in the following aspects:

1) Abstraction: Abstraction is the process of constructing classes by summarizing the common features of a class of objects, including both data abstraction and behavioral abstraction. Abstractions focus only on what properties and behaviors the object has, and do not care what the details of those behaviors are.

2) Inheritance: Inheritance is the process of creating a new class from an existing class to get inherited information. The class that provides the inheritance information is called the parent class (the superclass, the base class), and the class that gets the inherited information is called the subclass (derived class). Inheritance allows for a certain degree of continuity in a changing software system, and inheritance is also an important means of encapsulating the variables in the program (if not understood, read Dr. Shanhong's "Java and Patterns" or "Design pattern Refinement" section on Bridge mode).

3) Encapsulation: Encapsulation is generally considered to be the binding of data and the method of manipulating data, and access to data can only be achieved through defined interfaces. The essence of object-oriented is to portray the real world as a series of completely autonomous and closed objects. The method we write in the class is a encapsulation of the implementation details, and we write a class that encapsulates the data and data operations. It can be said that encapsulation is to hide everything can be hidden, only to the outside world to provide the simplest programming interface (can think of the difference between ordinary washing machine and automatic washing machine, it is obvious that the automatic washing machine package is better, so it is easier to operate; The smart phones we use are also well-packaged, Because of a few buttons to get everything done).

4) Polymorphism: Polymorphism means that objects of different subtypes are allowed to respond differently to the same message. The simple thing is to invoke the same method with the same object reference but do something different. Polymorphism is divided into compile-time polymorphism and run-time polymorphism. If the method of an object is treated as a service to the outside world, then the runtime polymorphism can be interpreted as: when a system accesses the services provided by B system, B system has a variety of ways to provide services, but everything is transparent to a system (like an electric shaver is a system, its power supply system is B, b system can use battery power or AC power, and possibly even solar energy, a system only through the Class B object call power supply method, but do not know what the bottom of the power supply system to achieve what is the way to get the momentum. Method overloads (overload) implement compile-time polymorphism (also known as pre-binding), while the method override (override) implements runtime polymorphism (also known as post-bind). Runtime polymorphism is the most essence of object-oriented things, to achieve polymorphism need to do two things: 1. Method overrides (subclasses inherit the parent class and override existing or abstract methods in the parent class); 2. Object styling (referencing a subtype object with a parent type reference, so that the same reference calls the same method and behaves differently depending on the subclass object).

8, the difference between String and StringBuilder, StringBuffer?

Common denominator: String StringBuffer StringBuilder are string-related functions, StringBuffer StringBuilder are subclasses of Abstractstringbuilder
Different points:
String content is not variable, stringbuffer StringBuilder content variable
String with StringBuffer thread safety, StringBuilder non-thread safe
If the program is not multithreaded, then using StringBuilder is more efficient than stringbuffer

9. The difference between overloading (overload) and overriding (override). Can overloaded methods be differentiated according to the return type?

The overloads and overrides of a method are implemented in a polymorphic way, except that the former implements the polymorphism at compile time, while the latter implements the runtime polymorphism. Overloads occur in a class, and methods with the same name are considered overloaded if they have different parameter lists (different parameter types, different number of arguments, or both); Overrides occur between subclasses and parent classes, and overrides require subclasses to be overridden by methods that have the same return type as the parent class, which is better accessed than the overridden method of the parent class. You cannot declare more exceptions than the parent class is overridden by the method (the Richter substitution principle). Overloads do not have special requirements for return types.

What are the similarities and differences between abstract classes and interfaces (interface)?

Abstract classes and interfaces cannot be instantiated, but they 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.

one, What's the difference between HashMap and Hashtable?

HashMap quickly find its content through hashcode, and all elements in TreeMap remain in a fixed order, and if you need to get an ordered result you should use TREEMAP (the order of the elements in HashMap is not fixed).

The collection framework provides two general map implementations: HashMap and TreeMap (TreeMap implements the SortedMap interface).


HashMap is the best choice for inserting, deleting, and locating elements in a map. But if you want to traverse the key in natural order or in a custom order, TreeMap is better. The implementation of Hashcode () and Equals () is clearly defined by the key class that is required to be added using HashMap.

Whatis the difference between the & operator and the && operator?

When an & expression is evaluated, the two operands are evaluated,&& more like a shortcut to an operator. When an && expression evaluates, the first operand is evaluated first, and if it returns true, the second operand is evaluated. If the first operand is a value of Fale, the second operand is not evaluated.

Android-------Java frequently asked basic questions

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.