java interview questions - reprint

forward from

 

1, the difference in java "==" and equals and the hashCode

  Operator "==":

  Designed to compare the values of two variables are equal, i.e. for comparing the variables corresponding to the values stored in the memory are the same. Comparative example if two basic types of data equal or two reference variables point to the same object, etc., require the use of "==" .

 

  equals method:

  Java is a class hierarchy root class Object methods defined, which means that each class inherits this method is generally used to compare whether two objects of the same non-null, returns true or false.

  Whenever you override this method, usually you must override hashCode methods to maintain the general contract of hashCode method - the method declaration equal objects must have the same hashcode.

 

  hashcode method:  

  Returns the hashcode value of the object. This method supports hash tables, for example HashMap hash table provided.

  hashcode General Statute:

  • During the execution of a Java application, regardless of the same Object hashcode () method has been called many times, as long as the information used in equals comparisons need not be modified, hashcode () method must always return the same integer. Of course, in various implementations, the integer need not remain consistent.
  • If two objects is determined according to the method equals are equal, then the call will be for each object hashcode () method to get the same integers.
  • Does not equal two objects is determined according to the equals method does not require two objects hashcode () method must return a different integer. However, the programmer should be noted, produce distinct integer results may improve the performance of the hash table is unequal objects.

  Indeed, hashcode Object defined () method returns indeed different integer values ​​for different objects. This is done by the internal address of the object into an integer to achieve, but this is not the Java language conversion technology requirements.

  (http://www.cnblogs.com/bigbigbigo/p/8553320.html)

 

  2, the number of how many bytes int, char, long each accounted for?

  Integer: byte: 1B short: 2B int: 4B long: 8B

  Float: float: 4B double: 8B

  Character: char: 2B

  关于boolean:The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

  

  3, the difference between int and Integer

  Java provides a wrapper class for each of the basic data types, Integer int is the wrapper class.

  There wrapper class attributes and methods, and more powerful than the basic data types.

  Java also provides a mechanism to automatically convert data between primitive types and their wrapper classes.

 

  4, talk about the understanding of Java polymorphism

  Defined polymorphism: the particular type of reference variables defined in the program pointed to is not determined at the time of programming, but it is determined during program execution.

  Exhibit polymorphism: the same message such that different objects respond differently.

  Polymorphic type: compile-time polymorphism, run-time polymorphism

  Polymorphism ways: inheritance (inheritance, rewriting method, upcast) and an interface (interface method invocation different objects through the interface variables).

  

  5、String、StringBuffer、StringBuilder区别

  String:

  String objects are immutable objects, that is, when you modify the contents of a String object, JVM will not change the original object, but creates a new String object;

  String objects are stored in the String Pool in;

  Immutable objects in Java are thread-safe, so the String object is thread-safe, meaning String object can not be used simultaneously two threads.

 

  StringBuffer:

  StringBuffer object is mutable object, i.e., its value can be modified. When StringBuffer string processing is performed, it does not generate a new object.

  StringBuffer object is stored in the heap;

  StringBuffer and StringBuilder method in the approach is the same, but the methods are the StringBuffer is synchronous, that is thread-safe. Therefore, StringBuffer objects does not allow two threads simultaneously access the same method.

  Because ensures thread-safe, StringBuffer performance degradation.

  () Method is converted into String toString Classes; StringBuffer object can be constructed by a String object.

  Internal implementations and String StringBuffer different;

 

  StringBuilder:

  And StringBuffer are basically the same;

  The difference is that, in the method of the StringBuilder does not guarantee the synchronization and thread-safe, so the speed will be faster than StringBuffer number.

  

 

  6. What is the inner class? The role of internal class

  The definition of a class declaration and placed inside another class. Internal class corresponding to the outer class class members, external access class member variables, methods and other internal classes.

  Inner class action:

  • Class logical grouping. Class A Class B if only role, may be used as an internal class A class B type.
  • Increase the degree of encapsulation. Inner class can be declared as private, you can access private members of the outer class. At this time, only the outer class can access the internal class.
  • The code can be made more readable and easier to maintain. The small class defined inside a large class, it may be closer to where it is used, and thus more readable.
  • Inner class object is created the moment does not rely create an external class object, no inner class (and outside of class) confusing "is a" relationship, that is a separate entity.
  • Each inner class can independently inherit a class or implement an interface, so no matter whether the outer class inherits a class or implements an interface, no effect on the inner class.

  (https://www.geeksforgeeks.org/nested-classes-java/)

 

  7, the difference between abstract classes and interfaces

  The performance of different interfaces and abstract classes in the following three aspects:

  (1) different levels of abstraction

    An abstract class is an abstract class for the entire whole, including the properties and behavior;

    Interface only abstract behavior. 

  Different (2) modeling the relationship

    An abstract class for modeling classes have similar characteristics, reflected in more abstract class that is an inheritance;

    The interface can be modeled class irrelevant.

  (3) different levels of design

    An abstract class is a bottom-up design;

    Interface only need to define a rule, only interested in the behavior of requirements specification;

  

  8, meaning an abstract class

  An abstract class can have properties and methods, but can not create an instance, it is used to be inherited. However, the abstract class can have constructor!

  Specific abstract methods abstract class implemented by subclasses responsible for their own complete, derived from the abstract superclass subclass to be able to implement all of the abstract methods.

 

  9, application scenario abstract classes and interfaces

  A class can implement multiple interfaces; a class can only inherit an abstract class;

  Select the abstract class:

  • You want to share code among several closely related classes.
  • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
  • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

  Select Interface:

  • You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
  • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
  • You want to take advantage of multiple inheritance of type.

 

  10, if you can not have an abstract class methods and properties?

  can!

  Interface may also not methods and properties.

 

  11, meaning the interface

  Interface implementation class must be done to provide a code of conduct;

  An interface can inherit multiple interfaces;

  A class can implement multiple interfaces, a single inheritance between classes make up the defects;

 

  12, the difference between generic and super extends the

  Providing upper bound limit for the generic type (can also use the upper bound class) and a lower bound;

 

  13, static methods of the parent class subclasses can override

  Yes, but overridden method must also be declared as static.

 

  14 difference, process and thread

 

 

  15 difference, final, finally, finalize the

  final:

  final modification classes, subclasses to avoid. If a class is declared as final, it's all method defaults to final (will not be rewritten anyway), but its properties do not like this;

  final modification methods, means that the method can not be overridden by subclasses;

  final modification of attributes or variables can not be modified after the variables are initialized. Under normal circumstances we can initialize a variable when you declare it final, however, you can also declare an empty final variable, then initialize (two ways to experience the difference in class) in the constructor. Empty final variable used to create immutable objects. ;

  (https://www.geeksforgeeks.org/blank-final-in-java/)

 

  finally:

  In common exception handling mechanism which ensures finally block is always executed, to recover resources.

  inally performed after block try and catch blocks, performed before control is transferred back to its original position.

  

  finalize:

  protected void finalize defined in the class Object () throws Throwable;

  If an object can be recycled garbage (there is no point to this object reference variables), then the garbage collector before the destruction of the object, it will automatically call the finalize () method for resource recovery. After the end of the method, the object is immediately destroyed.

  (https://www.geeksforgeeks.org/g-fact-24-finalfinally-and-finalize-in-java/)

  

   

 

  16, serialized manner

  If Java object belongs to a class or any of its parent class implements the interface java.io.Serializable or its subclasses java.io.Externalizable, then we say that the object is serialized.

  (https://www.geeksforgeeks.org/object-serialization-inheritance-java/)

  (https://www.geeksforgeeks.org/serialization-in-java/)

 

  18, whether static properties and static methods can be inherited? It can be rewritten? And why?

  It can be inherited;

  Static methods can be overridden, but overridden method must be static;

  There is only a hidden attribute that does not override a say.

 

  19, static inner classes of design intent

  Non-static inner classes after the completion of the translation will save a reference implicitly, the references to external class that created it. However, static inner classes do not have such a reference, it means the object is to create a static inner classes do not depend on external objects class is created , you can not access any non-static member outside of class.

  Reference :

  One common use of a static member class is as a public helper class, useful only in conjunction with its outer class.

  One common use of a nonstatic member class is to define an Adapter that allows an instance of the outer class to be viewed as an instance of some unrelated class. 

  Use a non-static nested class (or inner class) if you require access to an enclosing instance's non-public fields and methods.

  Use a static nested class if you don't require this access.

 

  20, members understand the inner classes, static inner classes, local inner classes and anonymous inner classes, as well as project application

 

-----------------------------------------

1, objects under what circumstances will be disposed of garbage collection?

  When an object is no longer in use, it should reclaim the memory occupied by objects, thereby enhancing memory utilization. Java in memory of recycling handled automatically by the garbage collector (Garbage Collection, GC), GC is only responsible for memory-related clean-up, other resources must be manually released.

  Java's automatic garbage collection mechanism: JVM heap memory view, to distinguish objects and unused objects being used, then delete unused objects.

  It refers object is a pointer to the program object reference; unused objects refers to the referenced object is not held by any part of the program.

Mark spam algorithm 
algorithm in Java garbage mark There are two main reference counting and reachability analysis algorithm. We first introduce a reference counting. 

Reference counting 
  Reference counting is to add a reference to the object counter in place whenever a reference to it, the counter is incremented 1 ; when referring to failure, the counter is decremented by 1 ; 
  any time the counter is 0 the object is no longer being used and it can be used as garbage collection. This method is simple to implement and the advantages and disadvantages are obvious. The advantages of high efficiency, program execution less affected shortcomings can not be detected that the circular reference, a memory leak reachability analysis algorithm basic idea of this algorithm is a series called the "GC Roots" objects as a starting point, from these nodes start searching down, the node is called a reference path traversed chain, when an object is not connected to any reference GC Roots chain, then prove this object is unavailable.

 

 

 

 

 

  2, talk about common coding?

  Common are ASCII, ISO-8859-1, GB2312, GBK, UTF-8, UTF-16, etc.

  Reference: in-depth analysis of Chinese coding problem in Java

 

  3, utf-8 encoded in the Chinese accounted for a few bytes; int type a few bytes?

  UTF-8 uses variable length bytes to store Unicode characters, for example, 1-byte ASCII characters continue to be used to store, accented characters, Greek or Cyrillic alphabet uses 2 bytes to store the like, and commonly used kanji characters necessary to use 3 section. Auxiliary plane 4 byte character is used.

  Java is an int type 4 bytes of storage

 

  4, the difference between static and dynamic proxy agent, what the scene to use?

   Static Agent: Create or specific tool automatically generates source code, compile it and then, before the program is run, the proxy class .class file already exists by the programmer. A proxy class can only serve as an interface.

  Dynamic Agent: dynamic proxy class bytecode dynamically generated by the Java reflection at runtime, without having to write the source code programmer manually. Through a proxy class, complete all agency work, do not need to implement a static proxy requires a proxy interface.

  Reference reading: the Java dynamic proxy mechanism      of static and dynamic agent Proxy

 

  5, Java exception system

     All non-normal divided into two types: Exception (Exception) and error (Error), were inherited from class Throwable.

  Error: refers to the virtual machine-related problems such as system crashes, and other virtual machine errors, such errors can not be restored, the program would not have captured.

  Exception: Abnormal divided into two categories: checked exception (Runtime) abnormal (Checked) and runtime.

  Runtime exception:

  All runtime exception object class and its subclasses are called run-time exception. Abnormal do not need to show that throw the exception, you can use try ... catch block to run if the program needs to capture run-time.   

  Checked exception:

  Exception instance the property of the checked exception is not running, it can be repaired exception . Programs must explicitly deal with checked exception, if not processed at compile time can not pass.

  Must ... catch block catches with a try, repair in the catch. When the method does not know how to deal with, when you define a method declaration throws (throws) an exception, by a higher calling process.

 

  6, talk about your understanding of the analysis and dispatched.

  Depth understanding of JVM. 

 

  7, modify objects A equals method signature, then use the time HashMap to store the object instance, which equals method calls?

  

 

  8, Java What is the mechanism to implement polymorphism is?

  Polymorphism at compile time, runtime polymorphism.

  Ways: inheritance, interfaces.

  Inheritance: inheritance, methods, rewrite, upward transition.

 

  9, how a Java object serialization to a file?

 

 

  10, talk about your understanding of Java reflection (reflecting the use and implementation)

  Reflection base (1) it is because in the operating state, the JVM is possible to know all the properties and methods, and it can call any of a method or access any of its properties. Such reflection can be dynamically loaded program, to view and use completely unknown at compile time classes (objects) at runtime

   (2) reflecting common use:

  • Procedures for checking tool and debugger, you can get the internal structure of the program at run time. Only takes a few dozen lines of code, can traverse the structure of the object's class, including construction method, declared properties and methods defined modifiers and the like.
  • At run time with the notes with dynamically change the behavior of the object, for example, adding logging, access control and other operations for a particular object.

 

  11, talk about your understanding of Java annotations (custom annotation usage scenarios and implementation)

 

  12, talk about your understanding of dependency injection

 

  13, talk about the generic principles and examples

 

  15, String why should be designed to be immutable?

 

  16, equal and hashCode methods of the Object class rewrite, and why?

 

 

  21, to talk about the understanding of Kotlin

   Kotlin design goals: the creation of a Java-compatible language, it is more secure than Java, more concise.

 

  Distinction 22, closure and partial inner class

  Closures are now present in many popular languages, such as C ++, C #. Closure allows us to create a function pointer, and passes them as parameters. That is, functional programming, Java 8 provides Lambda expressions, Lambda achieve a functional programming, it can be used for anonymous inner class alternative to the widely used implement a callback function for event responder.

  (https://www.oschina.net/translate/at-first-sight-with-closures-in-java?lang=chs&page=1#)

 

  23, String converted to methods and principles of Integer

   Integer.parseInt("10");

  String is the essence of char array (String.toCharArray ()), we can according to the ASCII table to convert a numeric character into a numeric. The character '0' ~ ASCII '9' value of 48 to 57.

 char[] chars={'0','1'};
 System.out.println(chars[0]);    //0

 int c=(int)chars[0];
 System.out.println(c);    //48

 

Guess you like

Origin www.cnblogs.com/zwq20134/p/11620213.html