Java Engineer - interview

Interviewer: Tell us about your understanding of concurrent?

I: Concurrency refers to the same period in between, are in more than one program has been started or completed, and these programs are running on a physical machine, but at any point in only one program at a time running on the processor.

Interviewer: So initialize an array of default size is how much?

I:? ? ? . . . When installed inside initialization not how much is it?

Interviewer: Java is the default length of the array of type int size,

I: the amount. . . I do not know, do not say ye, ye do not dare to ask.

Interviewer: the basis of your database is not good!

I: As you can see, are rarely the database operations deep in the daily work, CRUD are generally single table, and moreover are automatically generated by the tool.

Interviewer: Tell us about your understanding of the micro-services?

I: micro-services system each function module is to be split into a tiny services, there are benefits of doing so, each module functions between systems independently of each other, in the development of a prototype iterations can be faster, greatly reducing the the degree of coupling between systems in question meet later you can quickly locate the module out of the question, but also between each service is to communicate via HTTP protocol, is now the cloud is developed, some systems may be omitted for the company maintenance expenses

Interviewer: What do you talk about the encounter in the work of the thorny issue

I: BB lot, in fact, a StackOverFlow problem, ha ha ha, put on quite tall

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

Written part:

HashMap and HashTable difference:

HashMap

· HashMap is based on the hash table implementation, each element is a Key-Value of key-value pairs to solve its internal conflicts through a single list, insufficient capacity, the will automatically increase,

· HashMap is not thread-safe, value for concurrentHashMap single-threaded environment, if used in multiple threads, and concurrent use of contract under

· HashMap implements the Serializable interface, so it supports serialization, implement the Cloneable interface, it can be cloned.

Internal HashMap maintains Entry array for storing data, HashMap using the list to resolve the conflict, on each Entry is essentially a singly linked list. When ready to add a key-value pairs, is first calculated by the hash (Key) method hash value, and then find the storage location of the key-value pairs by indexFor (hash, length), is calculated by first using the hash & 0x7FFFFFFF, and then the length modulus, which ensure that each key-value pairs can be stored in a HashMap, when the calculated position of the same, since the position is stored in a linked list, put the key-value pair is inserted into the list head.

· HashMap of key and value are allowed to be null. key is a null value pairs are always placed in the list table [0] is the first node.

· Understand the stored data, the data is read and very easy to understand.

· HashMap of the storage structure, as shown below:

· The drawing, which represents part of the purple hash table, also called a hash array, each element of the array is a single-head node of the linked list, the linked list is used to resolve the conflict, if a different key is mapped to the same array position, it will be placed in a single list.

Entry · within an array of HashMap to store data by default is 16, and if not, when data is stored more than one, inside the Entry list will be very long for Entry expansion mechanism, which lost in the memory meaning HashMap. It has its own internal HasnMap expansion mechanism. Internal HashMap are:

* Variable size, which records the number of the underlying array of HashMap has a groove;

· A variable threshold, which is a threshold value of the HashMap, for determining whether to adjust the capacity of HashMap (threshold = loading capacity factor *)    

· Variable DEFAULT_LOAD_FACTOR = 0.75f, the default load factor of 0.75

· HashMap expansion condition is: When size is greater than the threshold, for expansion of HashMap  

· Expansion Yes Yes built a HashMap of the underlying array, and then call transfer method, all elements will be added to the new HashMap HashMap in (to re-calculate the index position of the element in the new array). Obviously, the expansion is a very time-consuming operation, because it need to recalculate the position of these elements in the new array and copy processing. Therefore, when we use the HashMap, it is best to estimate in advance the number of elements in the next HashMap, which helps improve the performance of the HashMap.

· HashMap There are four constructors. Constructors mentioned two important parameters: initial capacity and load factor. These two parameters are important parameters affecting the performance · HashMap, where the number represents the capacity (i.e., the length of the hash array) of the slot in the hash table, the initial capacity is the capacity to create a hash table (it can be seen from the constructor If not specified, the default is 16), load factor hash table before automatically increase its capacity can reach more than a full scale, when the number of entries in the hash table exceeds the product of the load factor and the current capacity when , will have to perform resize operation (i.e., expansion) to the hash table.

· Below that under load factor, if the load factor greater use of space more fully, but look for efficiency will be reduced (the length of the list gets longer); if the load factor is too small, then the data in the table would be too sparse ( not with a lot of space, the expansion began), causing serious waste of space. If we do not specify the configuration process, the system default load factor is 0.75, which is an ideal value, we are generally without modification.

· In addition, no matter how much we specified capacity, the constructor will set the actual capacity of a number of not less than the specified capacity power of 2, and the maximum value can not exceed 30 power of 2

HashTable

`Hashtable also based on a hash table to achieve the same each element is a key-value pair that internal conflict is solved through a single list, insufficient capacity (above the threshold), it will also automatically increase.

`Hashtable also introduced in JDK1.0 class is thread-safe, can be used in a multithreaded environment.

`Hashtable likewise implements Serializable interface, which supports serialization achieved Cloneable interface, be cloned.

the difference:

  1, different inheritance parent

    Dictionary class inherited from Hashtable, and HashMap class inherits from AbstractMap. But we have achieved Map interface.

  2, different thread safety

    javadoc concerning hashmap period described as follows: This implementation is not synchronized. If multiple threads access a hash map, and wherein the at least one thread modifies the map structurally, it must be synchronized externally

  3, whether to provide a method contains

     HashMap Hashtable contains methods removed, and replaced containsValue and containsKey, because it contains could easily lead to misunderstanding.

          Hashtable contains is retained, and containsValue containsKey three methods, which contains the same functions and containsValue.

  4, key value, and whether to allow null value

    And wherein the key is an object value, and can not contain duplicate key, but can contain duplicate value.

          The Hashtable, key and value are null values ​​are not permitted. But if there is a similar put (null, null) in the Hashtable operations, the compiler can also be passed because key and value are of type Object, but throws NullPointerException runtime exceptions, this is the specification of the JDK.

    The HashMap, as null key, only one such bond; can have one or more keys corresponding to the value of null. When the get () method returns a null value, this may not be a HashMap key, also possible for the key corresponding to the value of null. Thus, in a HashMap can not get () method to determine whether there is a key HashMap rather should containsKey () method to determine.

  5, the internal implementation of the two different traversal

        Hashtable, HashMap use the Iterator. And for historical reasons, Hashtable also used the Enumeration way.

  6, hash values ​​are different

    哈希值的使用不同,HashTable直接使用对象的hashCode。而HashMap重新计算hash值。

    hashCode是jdk根据对象的地址或者字符串或者数字算出来的int类型的数值。

    Hashtable计算hash值,直接用key的hashCode(),而HashMap重新计算了key的hash值,Hashtable在求hash值对应的位置索引时,用取模运算,而HashMap在求位置索引时,则用与运算,且这里一般先用hash&0x7FFFFFFF后,再对length取模,&0x7FFFFFFF的目的是为了将负的hash值转化为正值,因为hash值有可能为负数,而&0x7FFFFFFF后,只有符号外改变,而后面的位都不变。

  7、内部实现使用的数组初始化和扩容方式不同

        HashTable在不指定容量的情况下的默认容量为11,而HashMap为16,Hashtable不要求底层数组的容量一定要为2的整数次幂,而HashMap则要求一定为2的整数次幂。
        Hashtable扩容时,将容量变为原来的2倍加1,而HashMap扩容时,将容量变为原来的2倍。

          Hashtable和HashMap它们两个内部实现方式的数组的初始大小和扩容的方式。HashTable中hash数组默认大小是11,增加的方式是 old*2+1。

 

整理来自:https://www.cnblogs.com/williamjie/p/9099141.html

 

finnal和finnally的区别

  final关键字可以用来声明:不可变变量,静态常量,不可变参数,不可重写方法,不可继承类(String),final类的变量方法自动变成了final的,不可变引用变量可以改变其指向的对象的内部状态,但是不能改变他指向的对象。

  finally: 在异常处理时经常遇到,被包含在try,try/catch中,用来保证无论try语句中是否抛出异常,finally中的都能保证代码被执行,一般可以把流关闭语句,数据库连接关闭语句,网络连接关闭语句等放在finally语句块中,保证系统资源被释放,值得一提的是finally语句在try子句里面的return语句之前,catch字句块之后被执行。

整理来自:https://www.iteye.com/blog/yixiaohuamax-753035

 

抽象类和接口的区别

  抽象类可以有成员的实现,接口没有,

  抽象类的成员可以被子类部分实现,接口必须全部实现。

  一个类可以只能继承一个抽象类,但可以实现多个接口。

 

Guess you like

Origin www.cnblogs.com/wangyuanjun/p/11957106.html