Interview Backend-Java-Developer: questions and where to look for answers. Part 1



I used to do a series of interviews for a Backend Java developer and jot down questions for myself for the future, so I could go over and refresh my memory later. I thought that this collection will probably be useful not only for me, so I blew the dust off it, jotted down the answers and share it with the community. I do not pretend to be original and exclusive: similar articles have already been on HabrΓ©, and in many other places - at the end (in the second part) I will give a list of links so that the cheat sheet is as complete as possible.



β€” . - - middle, . . , .



, . Java Spring, β€” .



TL;DR

GitHub- , .





Java



1. Equals, hashcode HashMap.

. HashMap. ? ? , , . β€” ? , , ? . . . .



equals hashcode:



  1. .
  2. equals, .
  3. , equals ().
  4. , .


, - .



HashMap ( , Java 8, - ). , IDE. :)



2. : , , , , .

ArrayList vs LinkedList. , , β€” - , ArrayList , LinkedList , Β« Β» LinkedList. . , , ArrayList System.arraycopy, , .



3. `Object`.

HashMap, Java.



- ( :D), JavaDoc:



  • clone
  • equals
  • finalize (Deprecated)
  • getClass
  • hashCode
  • toString
  • notify
  • notifyAll
  • wait


, Object .



4. `wait`, `notify`, `notifyAll` `synchronized`.

, Baeldung . , , wait, notify, notifyAll synchronized . Oracle Concurrency Java.

, β€” . Java Language Specification, 17.1 17.2.



5. JMM. volatile. .

, JMM β€” , , . , , β€” , .



, JMM.



, JMM. , . ". Java- ": 1 2.



Java Language Specification, 17.4.



itsobes.ru.

JVM- How β€˜volatile’ works on JVM level? Medium.



6. . ? ? JVM? , , - ?

Java Stack Heap.



Stack β€” , LIFO. frame β€” . , , , Heap. , Escape Analysis Java 6, , , . Escape Analysis ( ) , .



Frame Stack . Frame , , , . Stack . JVM Specification.



Heap . , telegram- Senior's Blog. :



Heap :



  1. Young Generation

    1. Eden
    2. Survivor 0 Survivor 1


  2. Old Generation

    1. Tenured






Young : Eden, Survivor 0 Survivor 1. Eden . Survivor . Eden , Eden Survivor Survivor, Eden Survivor . . , Survivor, Tenured.



, Tenured, , . , . Tenured ( ), , , . Mark-Sweep-Compact ( , , ).



-, , Eden, Survivor’ . Tenured.



, , , . Survivor , Eden .

GC:





, .

Java ", !" alygin β€” , , .

74 Podlodka . .



:





gc JVM (, ):



  • medium
  • β€” OpenJDK
  • Java Garbage Collection Handbook reachability algorithm
  • Tracing garbage collection
  • Simone Bordet ZGC Shenandoah
  • JVM Anatomy Quarks β€” JVM. , , , -- .


7. Executor ExecutorService, Thread pool ?

β€” . N (Thread pool) . . .



Executor (void execute​(Runnable command) β€” ) ExecutorService ( , Callable ) β€” , . . Executors. - - , , .



:



  • C
  • Baeldung:
  • Oracle


8. Java ? ? heap-dump?

. . heap-dump, jmap, memory profiler ( VisualVM)



:





9. ? ?

parallel stream ForkJoinPool.commonPool Runtime.getRuntime().availableProcessors() β€” 1. Common pool ForkJoinPool System::exit ( shutdown() shutdownNow()). common pool, pool . Common pool . stream ForkJoinPool β€” stream Callable submit ForkJoinPool. fork() ForkJoinPool ( ).



ForkJoinPool ExecutorService, ForkJoinTask (RecursiveAction RecursiveTask). pool . ForkJoinPool work stealing β€” , , . .



:





10. ? ?

2 Java Stream:



  • (Intermediate) β€” filter, map, sorted, peek .. Stream.
  • (Terminal) β€” collect, forEach, count, reduce, findFirst, anyMatch .. .


, java.util.stream Collectors.



- , . , , - :





Java Doc, :





:





11. List<? extends Number>, List<? super Number>? , , ?

PECS β€” Producer extends, Consumer super (Joshua Bloch, Effective Java). β€” (, , ).



(covariance) β€” .

List<? extends T> , T , . List<? extends T> ( null) β€” , , . , T T, T.

, List<? extends Number> ArrayList<Number> ArrayList<Integer>, ArrayList<Object>. get Number, Integer Number.

.

, Java 5, .



List<?> List<? extends Object> .



(contravariance) β€” .

List<? super T> , T , . List<? super T> T , T . Object, .

, List<? super Number> ArrayList<Number>, ArrayList<Object>, Number(.. ArrayList<Integer>). Integer Double ( Number, ), β€” Object. get Object β€” .



β€” .

List<T> , T. T . T, .

, List<Number> ArrayList<Number>, ArrayList<Integer> ArrayList<Object>. Integer Double ( Number, ), β€” Object. get Number, Integer Number.



:





12. ConcurrentHashMap?

ConcurrentHashMap β€” (, , , ""), .

:



  • (Node<K,V>) val () next( ), (Node<K,V>[] table) volatile
  • CAS β€” , (insert, delete, replace)
  • volatile/atomic , CAS, intrinsics- (jdk.internal.misc.Unsafe)
  • Concurrent resizing
  • LongAdder


:



  • . , non-null , get(key) happens-before
  • ConcurrentHashMap β€” ConcurrentModificationException,
  • (size, isEmpty, containsValue),
  • null,
  • , ( ) , β€” forEach, search, reduce (bulk operations). , - ( forEach). parallelismThreshold β€” , parallelismThreshold. Long.MAX_VALUE . 1 ForkJoinPool.commonPool(),


β€” java 8 . Segment<K,V> , . concurrencyLevel initialCapacity - :



if (initialCapacity < concurrencyLevel)   // Use at least as many bins
    initialCapacity = concurrencyLevel;   // as estimated threads


ConcurrentMap. ConcurrentMap Baeldung.



13. Xmx Xms, Xss?

JVM Xms heap Xmx.

Xss .

:



java -Xmx<>< >


, (k), (m) (g).

:



java -jar my.jar -Xms256m -Xmx2048m


:



  • Mkyong
  • Tuning JVM Orcale
  • Factors Affecting Garbage Collection Performance Oracle
  • X CLI


14. ?

β€” , , .

β€” , , .. lock-free thread-safe . compare-and-swap (CAS) , . CAS.



. volatile value, compareAndSet(current, new), β€” current. CAS value , (.. current), compareAndSet(current, new). value , . , compareAndSet false. compareAndSet(current, new) current value.



:



  • compare-and-set β€” current CAS
  • set-and-get β€” current CAS ,


value VarHandle, Unsafe, . VarHandle β€” , , , . , /, volotile / compare-and-swap.



java.util.concurrent.atomic :





, :



public class NonReentrantSpinLock {

    private AtomicReference<Thread> owner = new AtomicReference<>();

    public void lock() {
      Thread currentThread = Thread.currentThread();

      while (!owner.compareAndSet(null, currentThread)) {}
    }

    public void unlock() {
      Thread currentThread = Thread.currentThread();
      owner.compareAndSet(currentThread, null);
    }
}


:





15. TreeSet/TreeMap? - ?

TreeMap β€” NavigableMap, - . Comparator, , . containsKey, get, put remove.

TreeSet β€” NavigableSet, TreeMap. Comparator, , . add, contains remove.



synchronized ConcurrentModificationException.



null, NullPointerException. null . 7- Java null TreeMap TreeSet .



- , , , . - :



  • ,
  • β€”


:





16. Java 8 Java <CURRENT_VERSION>?

Java . :



  • legacy- Java 8
  • Java 8,
  • Java 9+ ( 11 LTS, )


8 9 , , Java- . , , , , , , Java 8 , Java.



, :





API, , release notes API Java-.



, , Java:





GraalVM β€” JDK Java, , :



  • Java
  • Java
  • , , -
  • JIT AOT-
  • ..


:





:





:





17. Java? String? String?

Java 9 UTF-16 (2 ) char.

Java 9 Compact String. Latin-1 ( ), 1 , char . char byte, Latin-1 . byte coder, Latin-1 UTF-16.



String hashcode.



, (final class). , . StringBuilder append. ! Java 9 JEP 280: Indify String Concatenation, . StringBuilder bytecode StringConcatFactory invokedynamic, +.



String pool β€” heap , . .



, [String.intern()](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#intern()) , . . β€” JVM Anatomy Quark #10: String.intern().



, equals ( indexOf) JIT .



: java.lang.String The Lord of the Strings: Two Scours.



:





18. ThreadLocal ?

ThreadLocal β€” , .



ThreadLocal- , Thread. Thread ThreadLocal.ThreadLocalMap threadLocals, ThreadLocal. ThreadLocal.ThreadLocalMap HashMap, WeakReference<ThreadLocal<?>>, ref field . ThreadLocal, β€” Object. null, (stale) .

, ThreadLocal , . , .

ThreadLocal- ( get), , threadLocals, , . ThreadLocal.

, ThreadLocal- , .



:





19. ? ?

:



  • byte β€” 1
  • short β€” 2
  • int β€” 4
  • long β€” 8
  • char β€” 2
  • float β€” 4
  • double β€” 8


boolean . β€” , . JVM. .



β€” JVM - . Java Objects Inside Out.



:





jvm ( - C++), openjdk. , , :





20. Java?

Java:





, get(). , null. null.



, PhantomReference , ReferenceQueue β€” , . SoftReference WeakReference , PhantomReference . .



:





Spring



21. scope Spring? ? singleton prototype? scope ? ' prototype singleton?'

Spring scope:



  • singleton ( )
  • prototype
  • request
  • session
  • application
  • websocket


scope , Bealdung. , , Spring- . 2.



prototype singleton :



  • @Lookup
  • prototype-
  • ProxyMod = ScopedProxyMode.TARGET_CLASS

    Bealdung Spring- . 2.


22. Spring. ? , ?

, β€” BeanCurrentlyInCreationException ( ).



:



  • , β€”
  • @Lazy
  • setter-,


Bealdung



23. , , , DI

Spring- 1 2. β€” .



.



24. @Transactional. ? ? @Transactional ? @Transactional @Transactional - ?

, Proxy , .



, MyServiceImpl 2 , @Transactional β€” method1 method2( Propagation.REQUIRES_NEW). method1 method2.



, Spring AOP, method1() . method1() MyServiceImpl. method1() method2(), , , , .

, .



? Spring- β€” 1 2. Proxy .



25. ( Boot) Spring- main-?

Spring- ( ), main-. war-. war- , , . Spring Boot war .



:





26. Spring Boot ?

-, spring-boot-starter-parent, spring-boot-dependencies, β€” , dependencyManagement pom. BOM.



Spring Boot β€” , properties-.



Spring Boot main- @SpringBootApplication run SpringApplication, ApplicationContext.



@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}


@SpringBootApplication @EnableAutoConfiguration, @ComponentScan @Configuration.



SpringBootApplication WebApplicationContext ( classpath Servlet ConfigurableWebApplicationContext), GenericApplicationContext.



META-INF/spring.factories β€” org.springframework.boot.autoconfigure.EnableAutoConfiguration - ( , @ComponentScan, @Import ..) .



@EnableAutoConfiguration AutoConfigurationImportSelector, . getCandidateConfigurations SpringFactoriesLoader loadFactoryNames, classpath META-INF/spring.factories - , .



Spring boot spring-boot-autoconfigure META-INF/spring.factories. - , @Conditional ( ) - .



Spring boot jar spring-boot-maven-plugin. jar META-INF/MANIFEST.MF Main-Class β€” org.springframework.boot.loader.JarLauncher, Start-Class main- . JarLauncher class path ( org.springframework.boot), BOOT-INF( lib class ), Start-Class.



:





:





27. http- Spring?

Spring -:



  • Spring MVC
  • Spring WebFlux


Spring MVC DispatcherServlet, Servlet' Front Controller: Http- . DispatcherServlet WebApplicationContext. DispatcherServlet " " :



  1. HTTP- DispatcherServlet ( ) HandlerMapping, , Controller . HandlerMapping, : BeanNameUrlHandlerMapping RequestMappingHandlerMapping ( RequestMappingInfo @RequestMapping @Controller). HandlerMapping HttpServletRequest β€” handler- (, HandlerMethod). HandlerMapping HandlerInterceptor β€” - . HandlerInterceptor' handler- HandlerExecutionChain, DispatcherServlet.
  2. HandlerAdapter . HttpRequestHandlerAdapter ( , HttpRequestHandler), SimpleControllerHandlerAdapter ( , Controller) RequestMappingHandlerAdapter ( @RequestMapping).
  3. applyPreHandle HandlerExecutionChain. true, HandlerInterceptor . false , HandlerInterceptor .
  4. HandlerAdapter HandlerExecutionChain handle , - .
  5. - Controller ( handle) DispatcherServlet ModelAndView. ViewResolver DispatcherServlet , View .

    REST-Controller RESTful- , ModelAndView DispatcherServlet Controller null , , ViewResolver β€” HttpServletResponse handle. RESTful-, @ResponseBody @Controller @RestController, RESTful.
  6. HandlerExecutionChain applyPostHandle HandlerInterceptor.
  7. , HandlerExceptionResolver. ExceptionHandlerExceptionResolver ( , @ExceptionHandler), ResponseStatusExceptionResolver ( @ResponseStatus HTTP-) DefaultHandlerExceptionResolver ( Spring MVC HTTP-).
  8. Controller , View , DispatcherServlet View, HttpServletResponse. REST-Controller , HttpServletResponse.


HTTP Accept, Spring MVC HttpMessageConverter , , POJO Accept. HttpMessageConverter : Java , Java HTTP .

, Spring Boot HttpMessageConverter, , HttpMessageConverter .



, , Spring MVC javax.servlet.Filter , . Sring MVC .



Spring Security, . .



:





:





Spring WebFlux β€” -, Spring Framework 5.0. Servlet API ( Servlet 3.1+containers, Netty ( Spring Boot) Undertow), , Reactive Streams Reactor.

Spring WebFlux Spring MVC (RestController, RequestMapping ) . , HandlerFunction.



Spring WebFlux :



  • HttpHandler β€” HTTP- I/O, Reactive Streams back pressure Reactor Netty, Undertow ..
  • WebHandler β€” , API HTTP- .


HttpHandler HTTP-, WebExceptionHandler, WebFilter WebHandler. WebHttpHandlerBuilder ApplicationContext.



Spring WebFlux DispatcherHandler, WebHandler Front Controller: Http- . DispatcherHandler β€” Spring bean, ApplicationContextAware , . DispatcherHandler - webHandler WebHttpHandlerBuilder WebHandler.



DispatcherHandler http- " ", , . :



    @Override
    public Mono<Void> handle(ServerWebExchange exchange) {
        if (this.handlerMappings == null) {
            return createNotFoundError();
        }
        return Flux.fromIterable(this.handlerMappings)
                .concatMap(mapping -> mapping.getHandler(exchange))
                .next()
                .switchIfEmpty(createNotFoundError())
                .flatMap(handler -> invokeHandler(exchange, handler))
                .flatMap(result -> handleResult(exchange, result));
    }


  1. HandlerMapping (- , - ). (handler). HandlerMapping :

  2. , ( invokeHandler) HandlerAdapter, handle . HandlerResult, DispatcherHandler. HandlerAdapter β€” - DispatcherHandler. HandlerAdapter :

  3. HandlerResult ( handleResult) HandlerResultHandler. . HandlerResultHandler:



:







In the second part, we 'll talk about Hibernate, databases, development patterns and practices, one popular library, support and maintenance of our applications, and also look at alternative cheat sheets and summarize.




All Articles