Java Interview Fifth day

Source: Internet
Author: User
Tags wrapper

Modifier Abstract: Abstract, definition framework does not implement, can modify classes and methods

Abstract Modifier class:

Makes this class an abstract class that will not generate an object instance, but can be used as the type declared by the object variable, which is the compile-time type

Abstract class is equivalent to a class of semi-finished, need to inherit and overwrite the subclass of the abstract method, then the child class to create an instance of the ability, if the subclass does not implement the parent class abstract method, then the subclass is also an abstract class.

Abstract Modification Method:

Will make this method an abstract method, that is, only the declaration is not implemented, the implementation part of the ";" Instead, subclasses are required to inherit the implementation.

Abstract methods represent a standard, define standards, define functions, implement functions in subclasses (subclasses inherit the parent class and need to give an implementation of an abstract method inherited from the parent class).

The method can be defined as abstract when it is unexpected how it is implemented, or if it is intended to be implemented by subclasses to define a standard.

Attention:

A class with an abstract method must be an abstract class. But abstract classes are not necessarily abstract methods, but they can all be concrete methods.

Interface (interface):

Interface definition: An interface is essentially a special kind of abstract class.

Keyword interface.

In the interface, all methods are public, abstract methods: Publicly abstract

In an interface, all the properties are public, static constants: publicly static final

There can be multiple inheritance between interfaces and interfaces, with extends, separated by commas.

There is no construction method in the interface, you cannot instantiate an interface with the new interface name, but you can declare an interface.

Implementation of the interface:

Keyword implements

A class implements an interface that must implement all the methods in the interface, otherwise it is an abstract class, and the methods in the implementation class are added public (cannot be omitted).

Default modifier in the class: defaults.

The default modifier in the interface: public.

A class can also implement multiple interfaces (separated by commas), in addition to inheriting another class, which can inherit only one of the classes.

The role of the interface:

Indirect implementation of multiple inheritance: using interfaces to achieve multiple inheritance does not increase the complexity of class relationships. Because the interface is not a class, and the class is not on one level, it is re-abstracted on the basis of the class.

Interfaces can abstract secondary types, distinguish between primary and secondary relationship types, and conform to the general method of seeing the world.

Interface isolation, which is related to encapsulation. An object has multiple facets that can show only a few of them, and others are hidden. Therefore, it can be seen as a "higher-level package", a large interface into a number of small interfaces.

Establish standards through interfaces (the most important role)----the role of interfaces, the formulation of protocols.

Interface: Set standards.

Caller of interface: Use standard.

Implementation classes for interfaces: implementing standards.

Decoupling: the use of standards and implementation standards are separated, so that the standards of the creator and the implementation of the decoupling relationship, with a strong portability

Example: Sun provides a set of interfaces (standards) for accessing the database, which is programmed for the database interface when the Java programmer accesses the database. The interface is implemented by each database vendor.

Principles of Interface programming

Try to program the interface as much as possible (interface can be used as much as possible)

Interface isolation principle (replaces a large interface with a few small interfaces)

Attention:

There is no constructor in the interface, and there is no Main method

Package class:

Java provides a wrapper class for each simple data type.

In addition to int and char, the remaining type is capitalized as a wrapper class.

int Integer

Char Character

Two most commonly used encapsulation classes integer and double

jdk1.4 before the basic type and encapsulation class conversion is required to transform the constructor, to the jdk1.5 is automatic conversion

conversions between int, Integer, and string (most commonly used)

int i=1;

Integer in = new Integer (i); INT---Integer

int i = In.intvalue (); Integer-to-int

String str = string.valueof (i); Int--String

int II = integer.parseint (str); String--INT

String s = in.tostring (); Integer--String

Integer inte = integer.valueof (str); String--Integer

Object class

Hashcode ():

Returns the hash code value of the object

The general agreement of Hashcode is:

During Java application execution, when the Hashcode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used in the Equals comparison on the object has not been modified.

If two objects are equal according to the Equals (object) method, calling the Hashcode method on each object in two objects must produce the same integer result, which is not necessarily true, for example, for Hashmap,hashtable, is not the same as ArrayList.

ToString ():

Returns the string representation of the object.

Typically, the ToString method returns a string that represents this object as text. The result should be a concise but easy-to-read understanding. It is recommended that all subclasses override this method.

Equals ()

Indicates whether a different object is "equal" to this object.

The Equals method implements an equality relationship on a non-null object reference:

Reflexivity: X,x.equals (x) should return true for any non-null reference value.

Symmetry: x.equals (y) should return true for any non-null reference value x and Y, if and only if Y.equals (x) returns True.

Transitivity: For any non-null reference value x, Y, and Z, if X.equals (y) returns true and Y.equals (z) returns True, then X.equals (z) should return true.

Consistency: For any non-null reference value x and Y, multiple calls to X.equals (Y) always return TRUE or always return false, provided that the information used in the Equals comparison on the object has not been modified. X,x.equals (NULL) should return FALSE for any non-null reference value.

Attention:

When this method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the Hashcode method, which declares that the equality object must have an equal hash code.

String, StringBuffer, and Stringbulder

String: non-changing sequence of Unicode characters

Pooling ideas, put the data that needs to be shared in a pool, and use a storage area to store some common resources to reduce the overhead of storage space.

In the string class, when created in literal value, it is found in the string pool of Java method space, and if not, a string object is created in the string and the address assigned to the object variable is returned, if any, the address of the string in the pool is returned, and the address is assigned to the object variable.

If it is new, the object of the string class is created in the heap space and there is no such procedure

Such as:

String S1 = "abc"; Newly created, string constant pool does not have this string, then a string "ABC" is created in the pool

String s2 = "abc"; "ABC" already exists in the string pool, then S2 will point to "ABC" instead of creating a new

String s3 = new String ("abc"); Directly in the heap to open up a new space, instead of going to the pool to find

View the following API documentation in the specific method in the

Calling a method in any string does not change the string itself unless the value is re-assigned.

StringBuffer: A sequence of Unicode characters that can be changed

Allows concurrent operations, which are thread-safe

The string class is inefficient in connection with strings because the properties of the object it produces cannot be modified, and only new objects can be created when the string is concatenated.

For many string connections, you should use the StringBuffer class, which optimizes efficiency by using objects of this class to concatenate strings without unnecessary intermediate object generation.

Example: Strings connected to string str = "A" + "B" + "C" + "D";

Production: "AB", "ABC", "ABCD"

The "AB" and "ABC" produced in the pool are obviously superfluous objects and waste space.

Solution:

String s = null;

StringBuffer sb = new StringBuffer ("A");

Sb.append ("B");

Sb.append ("C");

Sb.append ("D");

s = sb.tostring ();

Stringbulder: A sequence of Unicode characters that can be changed

Operations with StringBuffer, except that concurrent operations are not supported, non-thread-safe

Java Interview Fifth day

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.