Java review-basic1
阅读原文时间:2023年07月09日阅读:3

1. Dependency Injection Answer:

Any application is composed of many objects that collaborate with each other to perform some useful stuff. Traditionally each object is responsible for obtaining its own references to the dependent objects (dependencies) it collaborate with. When using Dependency Injection, objects are given their dependencies at run time rather than compile time (car manufacturing time). The Dependency (Wheel) can be injected into Car at run time. Inversion of Control (IoC) is a general concept, and it can be expressed in many different ways and Dependency Injection is merely one concrete example of Inversion of Control. This concept says that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container is then responsible for hooking it all up.

个人理解:

An application is composed by many objects and they collaborate with each others. Tranditionally, each object colaborate with others need to obtain reference from dependent objects. For Dependency Injection, objects are given thier dependency at run time rather than complie time.  IOC inverson of control is an example of Dependency Injection. it doesn't need create reference object by ourself instead of descire how to create them. components and services, we don't need create connection between components and services in code but also descibe who to create connection between componetns and services in a configuration file.

三句话:

应用是对象与对象组成的,传统的对象之间需要自己建立引用并且管理他们。

在依赖注入中,对象在运行时获得他们的依赖而不是在编译时

IOC是DI的一种实现方式,不需要在代码中实现依赖,而是描述如何建立依赖,不要自己亲自建立组件与服务之间的关系,只需要在配置文件中描述如何建立。

2. How does Java HashMap or LinkedHashMap handles collisions?

Prior to Java 8, HashMap and all other hash table based Map implementation classes in Java handle collision by chaining, i.e. they use linked list to store map entries which ended in the same bucket due to a collision. If a key end up in same bucket location where an entry is already stored then this entry is just added at the head of the linked list there. In order to address this issue in the case of frequent HashMap collisions, Java8 has started using a balanced tree instead of linked list for storing collided entries

两句话,使用链表加上数组,当有相同元素因为碰撞放入了同一buket,就加在链表的前面,桶排列在数组中。

3. Reflection API

The name reflection is used to describe code which is able to inspect other code in the same system (or itself). For example, say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to.So, to give you a code example of this in Java

(imagine the object in question is foo) :

Method method = foo.getClass().getMethod("doSomething", null);

method.invoke(foo, null);

反射:如果我们不知道一个对象的类型,想确定调用一个方法,可以使用反射来调用这个方法并且使用。

4. Java 8 features - Lambda Expressions, Default Methods.

Lambda Expressions: parameter -> expression body Following are the important characteristics of a lambda expression − Optional type declaration − No need to declare the type of a parameter.

Default Methods: Java 8 introduces a new concept of default method implementation in interfaces. This capability is added for backward compatibility so that old interfaces can be used to leverage the lambda expression capability of Java 8.

5. Java Keywords - static, final, volatile, synchronized, transient, this, super etc.

Static: static members belong to the class instead of a specific instance. It means that only one instance of a static field exists even if you create a million instances of the class or you don't create any. It will be shared by all instances. Since static methods also do not belong to a specific instance, they can't refer to instance. static members can only refer to static members. Instance members can, of course access static members.

Volatile: the volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory. So if you want to share any variable in which read and write operation is atomic by implementation e.g. read and write in an int or a boolean variable then you can declare them as volatile variable. The Java volatile keyword cannot be used with method or class and it can only be used with a variable.

transient: is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred; they are lost intentionally.

6. What is immutable, Implement an immutable class (e.g. myDateTime)?

As said earlier, Immutable classes are those class, whose object can not be modified once created, it means any modification on immutable object will result in another immutable object. Implements: best example to understand immutable and mutable objects are, String and StringBuffer. Since String is immutable class, any change on existing string object will result in another string e.g. replacing a character into String, creating substring from String, all result in a new objects. While in case of mutable object like StringBuffer, any modification is done on object itself and no new objects are created. Some times this immutability of String can also cause security hole, and that the reason why password should be stored on char array instead of String.

7. Atomic: is a toolkit of variable java.util.concurrent.atomic package classes, which assist in writing lock and wait-free algorithms with the Java language. An algorithm requiring only partial threads for constant progress is lock-free. Lock and wait-free algorithms are also known as nonblocking algorithms. Nonblocking algorithms are used for process and thread scheduling at the operating system and Java virtual machine levels.