Java interview questions -java base 01

  1. Object-oriented have to understand what features and a pair of these features

(1)  Inheritance: continue to pass, that is the previous generation of things left to the next generation. In java in a class of their own it is something (such as a property method, etc.) to another class. The class offers something called the parent class, get what is called a subclass of class. Inherited advantages: reusability scalability.

(2)  Package: things sealed package, open to the public do not want to add a lock sealed, only to have the appropriate key to unlock the lock to see the hidden things. In java in the data is hidden, only outside provides a simple programming interface for access.

(3)  polymorphism: a variety of forms, as if to answer the same question can have many different answers. In java in the same object that calls the same method to make different things. Compiled into polymorphic polymorphic (overloaded method) polymorphism and operation (rewrite process). Multi-state running requires two conditions: temporarily overridden methods, two o'clock subclass object reference to point to the parent class.

(4) Abstract: be extracted, the common feature of a class of objects is extracted to form a new class, including behavioral abstraction and data abstraction, only concerned with the properties and behavior of the object, behavior does not care about the details.

  1. Access modifiers difference between public, protected, default, private of

Modifiers

The current class

The same package

Subclass

Different packages

public

protected

×

default

×

×

private

×

×

×

 

  1. Process new process of an object and clone the object of a difference

(1)  In can create objects in java clone () with the new.

(2) clone () does not call the constructor; new calls the constructor.

(3) clone () can quickly create a copy of an existing object, that object is created and the attribute values ​​of all existing clones object; only apply a new new empty memory region in the JVM, the attribute value of the object to be constructed by methods assignment.

note:

① use clone () must implement the interface java.lang.Cloneable and override clone () method of the Object class, if not implement Cloneable () Interface will throw an exception CloneNotSupportedException. (Such interfaces implemented java.lang.Cloneable, indicating the Object.clone () method can legally be copied by the instance of the class field.)

② default Object.clone () method is a shallow copy, a copy of the object is created and then point to refer to the same through the "assignment" copy content, if the class member variable contains a reference type, then the original object reference type member variables and objects cloning content.

  1. What is shallow copy? What is a deep copy?

(1) "shallow copy": the default Object.clone () method, citing a copy of the original object to the new object, the new object with the original object point to the same address value, and did not open up new heap memory space.

(2) "deep copy": override clone () method, while a copy of the original object, together with a reference copy of the original object, the original object and the new object reference to point to a different address value. Deep copy to re-open up new space in the heap memory.

 

Note: If you want to deep copy an object, the object must be to achieve Cloneable () interface, clone method. And within the clone method, put the object references other objects have a clone, which requires that the referenced object must also implement the Cloneable interface and implement the clone method.

  1. & & & Distinction of

(1) && has a function of short-circuit, short-circuit does not have the functions &.

(2)  When the time both sides of the result of the expression & operator are true, until the entire computation result is true. && operator while the first expression is false, the result is false, the second expression is not evaluated.

(3) & && and can be used as the logical AND operator, and the logical expression (and), when the result of the expression operators on both sides are true, the result was the entire operation is true, otherwise, as long as one party false, the result is false.

(4) && also has a function of short circuit, i.e. if the first expression is false, the second expression no longer calculated, for example, for if (str! = Null &&! Str.equals ( "")) expression style, when str is null, the latter expression does not occur, it will not appear NullPointerException. If && change & will throw NullpointerException exception. In if (x == 33 & ++ y> 0) in the y grow; in if (x = 33 && ++ y> 0) in y does not grow

(5) can also be used as & bitwise operators, when the expression & operator on both sides of the brush is not a boolean, & represents a bitwise AND operation, we usually use

  1. Two objects of equal value ( x.equals (the y-) == to true), but there are different hashCode, this sentence right?

Right, if two objects meet x.equals (y) == true, then they should be the same hash code hashCode

  1. You can inherit String?

String is designed as a final class can not be inherited. String class reuse of the best way relationship (has-a) and dependencies (use-a) instead of inheritance (is-a)

  1. Overloading and rewrite the difference

(1) both multi-state manner, overload is achieved polymorphic compile time, rewriting is realized when a multi-state operation.

(2) overload occurs in the same class, the rewriting occurs in the parent class and subclass of two classes.

Rule (3) is overloaded same method name, a list of different parameters, regardless of the return value.

(4) rewrite rules: the name of the same method, the same argument list; subclass return type is not greater than the parent class, subclass thrown exception class method declaration is not greater than parent; subclasses access method is not less than the parent class.

  1. Why can not function to distinguish overloaded by return type?

Because you can not specify the type of information when you call, the compiler does not know which function you want to call.

  1. char type variable can store a Chinese characters, and why?

char type may store Chinese characters a, is used because the encoding Unicode java, a char type 2 bytes (16 bits), a Chinese character can be stored.

  1. The difference between abstract classes and interfaces? When to use abstract classes and interfaces?

(1) abstract class:

① can have constructors, member variables, static methods, abstract methods and specific methods

② the members of the class can be private, default, protected, public

③ a class can only inherit an abstract class

④ abstract class contains methods must be an abstract class, an abstract class and can not abstract methods

(2) Interface:

① constructor, static methods can not be defined

② methods are all abstract methods

③ members are all public, member variables are actually constants

④ A class can implement multiple interfaces

  1. The difference between static variables and instance variables

(1)  Static variables: the static modifications, called class variables, belong to the class by class name called, is common to all objects, if an object to change its value, then the other objects to is that after they result. No matter how many objects of a class is created, static variables and only one copy in memory. In the static storage area

(2) instance variables: to be dependent on an instance, you need to create an object before you can access to it through an object. Objects belonging to private, an object will change its value does not affect other objects. Stored on the heap

  1. == and equals the difference

(1) equals: is a method, compare the contents of two objects are equal. Can not be used to compare the basic data types, if its not rewritten, the address comparator is a reference type variable points of the object.

(2) ==: is an operator for comparing two values ​​of the basic data types are equal, comparison reference data for two types of address values ​​are equal

  1. Break and continue the difference

(1) Break completely finished for one cycle

(2) Continue to jump out of the current cycle, the next cycle into the

  1. String s = "Hello"; s = s + "world"; after two lines of code execution, the contents of the original String object is not changed in the end?

No, String is a final class, it's all objects are immutable objects, perform String s = "Hello" one, will open up a space for memory "Hello" in the heap , S = S + "world" will be executed in a two storage spaces are opened up the heap "world" and "the Hello World" , the original pointed to by s "Hello" is never changed

  1. What is the mechanism implemented in Java polymorphism is?

Inheritance, pointing to an object reference to an instance of a subclass of the parent class, rewrite

 

  1. Java exception into what kinds?

According to the timing of the abnormal need to handle exceptions (also known as mandatory abnormal) divided when compiled, also known as abnormal (also known as non-mandatory abnormal) CheckeException and runtime also called RuntimeException

  1. Error and exception of difference

(1) Error class and the parent class are throwable exception class

(2) Error type errors are alone program itself can not be recovered and prevention, usually refers to the problems associated with the virtual machine, such as Ben collapse system, virtual machine error, insufficient memory, etc.

(3) Exception class indicating abnormality can handle may be captured and may resume. But also abnormal and abnormal CheckedException RuntimeException when divided by the run-time checking

  1. java exception handling mechanism

Java exception is the root class Throwable, derived two sub-classes below its error and exception. Error Indicates a serious problem to overcome and the program itself can not be restored; exception indicates a potential problem to overcome and recovery, exception can be divided into general system abnormalities and abnormalities, abnormalities are caused by defects in the software itself, the user can not overcome and recover, but in this problem can continue to let the software run directly or dead. The exceptions are common problems caused by changes in the operating environment, users can overcome and recover. Java is a general system abnormalities and anomalies provide different solutions, compiler enforces the general exception must deal with a try ... catch or throw with throws, so common abnormalities called checked exceptions; and the system can handle an exception can not handle, but also become unchecked exceptions

  1. Please write your most common five RuntimeException

(1) NullPointerException null pointer exception

(2) ClassNotFoundException not find the specified class

(3) IndexOutOfBoundsException array subscript bounds exception

(4) NumberFormatException abnormal string converted to digital

(5) IllegalArgumentWxception method to pass parameters exception

  1. Throw and throws the difference?

(1) Throw:

For use in vivo, an exception thrown by a method of processing statements in vivo, represents an exception is thrown example, performing throw must throw some abnormality

(2) Throws:

In a statement after the method used, the caller by the method throws an exception to deal with, represent some type of anomaly that may be thrown, throws some kind of representation may throw an exception, this exception may not occur

  1. The difference between final, finally, finalize the

(1) final: for declaring attributes, methods and classes when each represent immutable properties, the method can not be overwritten, modified class can not be inherited

(2) finally: exception handling part of a statement structure representing always performed

(3) finalize: a method of the Object class, when the garbage collector calls this method to perform recovery targets for resource recovery. It is a passive method. This method is called indicates that the object of death

  1. Array has no length () method? String has no length () method?

Arrays are not length () method, but the length property. String has a length () method. JavaScript, the length of the string can be acquired by the length property.

  1. String, StringBuilder, Stringbuffer difference? What were applied to the scene?

(1) String: Read-only string with the contents of String references can not be changed, the underlying character array constants, thread safety, efficiency slowest.

(2) StringBuilder Stringbuffer string object and represented by may be directly modified.

(3) StringBuffer: multi-threading, thread-safe, low efficiency

(4) StringBuilder: single-threaded, thread-safe, highest efficiency

(5)  be used: a small amount of data operation String; single-threaded operating string buffer large amounts of data with the StringBuilder; StringBuffer operation with large amounts of data string buffer multithreaded operations.

  1. Java basic data types are there? Each accounted for a few bytes

(1) four categories: integer, floating point, character, Boolean

(2)  eight kinds: Integer: byte. 1 byte, short 2 bytes, int 4 bytes, long 8 bytes

(3)  Float: a float. 4 bytes, double 8 bytes

(4)  Character: char 2 bytes

(5)  Boolean: Boolean. 1 byte

  1. What is the difference int and Integer

(. 1) is a basic data type int, 4 bytes; int Integer wrapper class is provided a string conversion, generic, automatic features such as boxing and unboxing

(2) Ingeter variables must be instantiated before use, and do not need int variables

Default (3) int is 0, the default value is a null Integer

(4)  when the frame is preferably used with Ingeter

  1. What is the automatic boxing and unboxing? Why use automatic boxing and unboxing?

(1) Packing: The basic data types with their corresponding reference type packaged

(2) unboxing: converting the basic data type package type.

(3) Java using automatic boxing and unboxing mechanism, saving memory overhead common values, improve efficiency, the compiler is done, the compiler will compile grammar decide whether to crossing the finish line and unboxing action based.

  1. Convert between data types

(1)  string transfected how the basic data types: basic data call types corresponding method of packaging parseXXX (String) or valueOf (String)

(2) how the basic string data type conversion: One method is to ( "" basic data types and the empty string ) is connected; Another method is to call the String class valueOf () method returns a string corresponding

  1. Java There are several types of flow?

(1) in the direction of flow into the input and output streams;

(2) function implemented by the node and the process flow stream;

(3) per unit of processing data character stream into a byte stream and

  1. How byte stream into a character stream?

(1)  byte input character input flow stream through InputStreamReader achieved;

(2)  bytes by the output stream of characters output stream OutputStreamWriter achieved.

  1. How will a java object serialization to file

In the java class can be serialized must implement Serializable.

  1. The difference between a byte stream and a character stream

(1)  in bytes can handle all types of data stream, for processing one byte in succession InputStream, OutputStream;

(2)  the character stream can process character data, the processing unit is two-byte unicode character, inherited Reader, Writer

(3) as long as the plain text data, character stream priority, are used in addition to the byte stream.

  1. How to achieve the object cloning

(1)  One way to achieve Cloneable interface and override the class Object clone () method

(2)  Another way is to achieve Serializable interface, cloned by deep serialization and deserialization of the objects

  1. What is java serialization? How to achieve java serialization?

(1) is a serialization mechanism for an object a process stream, for solving the problems of the stream of objects to read and write operations initiated.

(2)  to achieve serializable: the need to be serialized must implement serializeable Interface

  1. Will the ArrayList, HashSet, HashMap is thread safe? If not, then I want a set of thread-safe how to do

Threads are unsafe, the Vector, but added the HashTable thread-safe thread-safe on first to add synchronized keyword in the core of the collection method

  1. What internal ArrayList is implemented

ArrayList internal array is implemented, a default length is 10. Thread-safe, high efficiency, fast queries, additions and deletions slow.

  1. It features three subclasses of List

(1) ArrayList bottom is an array, query fast, slow additions and deletions

(2) LinkedList double bottom is a circular linked list, add or delete fast, slow query

(3) Vector is the underlying array, thread-safe, additions and deletions slow, slow queries

  1. List and the Map , the difference between the set

(1) List is set and stored in a separate data set, map is a set of key-value pairs stored in the double row data.

Data (2) List stored ordered repeatable, map stored in the non-sequential, the key can not be repeated, the value may be repeated; data set storage disorder is not duplicated, but the position is fixed.

  1. What is the difference HashMap and HashTable

(1) HashMap is thread safe, but the key does not allow repeated key blank and null values, with high efficiency.

(2) HashTable thread-safe, does not allow the key is null

 

  1. Arrays and linked lists are applicable to any situation, and why?

(1) An array is stored contiguously, fast query, deletions slower, less suitable for the data, the operation is often done by serial access elements;

(2) the list is stored in a discrete, slow query, deletions faster for the linear length or size of the table is difficult to estimate, make frequent additions and deletions operation;

  1. Map of the key and value can be null do

key (1) HashMap objects, value values ​​that can be null;

(2) HashTable object key, value can not be transported to the value null;

(3)  the two key values are not repeated if the same key value pairs added, the latter value will automatically overwrite the previous value

  1. To talk about your understanding of java reflection

The first is the Java reflection can be obtained to be reflected java bytecode class byte code acquisition methods are: 1, Class.forName (className) 2, class name .class 3, this.getClass (). The method is then the bytecode, variables, and other constructors are mapped to corresponding Method, Filed, Constructor other classes that provide a rich so we can use the method.

  1. A class will be initialized at what time

 

(1)  create an instance of the class, which is new when an object is. .

(2) access the static variable of a class or interface, or assignment of the static variables. .

(3) static method call class. .

(4) reflective. .

(5) a subclass of class initialization. .

(6) JVM startup class indicated start time, the file name and class name of that same class. .

Only these six kinds of circumstances will lead to initialize class!

Class initialization steps:

1) If the class is not caught in the link and, before loading and linking

2) If the class there is a direct parent class has not initialized, then initialize the direct parent

3)  If the class initialization statement is present (e.g., static variables and static blocks), it first performs the initialization time statements.

  1. A ".java" source files may include a plurality of whether the class (not an internal class)? What are the limits?

There can be multiple classes, but only one public class, and the public class name must match the file name

Guess you like

Origin www.cnblogs.com/jpxjx/p/12152670.html