[ANSWERS] The Most Common Java Software Engineer Questions – Part 1

Last modified date

Comment: 1

Core Java

1. What are annotations in Java and how do they work?

Annotations are decorators that provide metadata in a Java project.

Metadata is data about data.

Annotations in Java have been around for a long time and the most well known annotations are those such as @Override and @Depracated.

Annotations do not contain any business logic. Annotations provide information about the attribute e.g the class, method, package, field.

Since annotations don’t contains any business logic, then something else must be performing operations based on those annotations – it is the consumer of the annotation that provides the logic.

Annotation processors uses the Reflection API to analyse the attribute that the Annotation has been applied to and then run some business logic such as running checks, modifying them or generating code (e.g. lombok annotations).

In standard annotations like @Override, the consumer is the JVM. However if you are writing your own annotation then you will have to write your own consumer.

There are also different types of annotations and this is defined by the the @Retention policy of the annotation. For example compile time annotations such as @Override are used by the compiler to check that the method is overridden and there is a corresponding method in the superclass, if not it throws an error. Then the annotation is discarded before runtime.

Other annotations such as Springs @Component annotation are available at runtime.

2. What is the difference between a primitive and an object (non-primitive)?

Primitives are independent data types, they are not objects created from a class and they are not part of any hierarchy. The 8 primitive data types are boolean, byte, short, int, long, float, double and char.

All objects created in Java are inherited from the Object superclass.

Primitives are pass by value meaning that when a primitive variable is passed to another method or object in the java program, the value of that primitive is passed and not the primitive itself

Objects on the other hand are pass by reference meaning that when an object is passed to a method or another object, it is a reference that is passed. This means that if there are two references to the object, then performing any operations on the object will affect the other references.

3. Where are primitives and objects stored in memory?

There are two types of memory used in Java, stack memory and heap memory.

Stack memory is where primitives and object addresses are stored.

Heap memory is where objects are stored.

Spring

4. Does Spring Boot spawn a new thread for every incoming request?

Yes.

For each new incoming request into a Spring Boot REST Controller, Spring will spawn a new thread to handle the request for the duration of that request.

If the Spring Boot application has 300 threads available to it, and 500 requests come in simultaneously, then 200 threads will be queued until a new thread is available in the pool.

New threads will be spawned up until the configured maximum max-threads attribute. This can be set in the application.properties file.

5. What happens when there are no available threads to handle incoming requests to a Spring Boot Controller?

When there are no more threads available to handle incoming requests, then the Apache Tomcat Web Server used by Spring Boot will start to stack up these requests inside the server socket created by the Connector.

These new requests will keep getting stacked until the configured maximum is reached, defined by the acceptCount attribute.

If the configured maximum has been reached but more requests are still coming in, then these further requests will receive a “connection refused” error until there are more resources available to process these requests.

Maven

6. What is the difference between plugins, dependencies and repositories in Maven?

Plugins and dependencies are both Jar files.

Plugins are artifacts that provide goals to Maven, they can be viewed as extensions to your project. They are Jar files that perform some function within your maven project. All work in a Maven project is done by plugins.

For example the Compiler plugin compiles your code.

The core plugins provided by Maven can be found here.

Dependencies are Jar files that are added to the classpath of your project when executing the tasks defined by the plugins. Dependencies allow you to easily add frameworks and libraries into your project by adding the dependency to the POM.

Maven downloads the dependencies on compilation and all of the dependencies of the dependencies. The dependencies of dependencies of the dependencies are called transitive dependencies.

For example the JUnit dependency adds the annotations and classes needed to write unit tests in your java project.

Repositories in Maven are directories in where all of the project jars, libraries and plugins and other project specific artifacts are stored and cached so that they can be easily used by Maven.

Repositories hold build artifacts and dependencies of varying types. There are two types of repositories: local and remote.

Local repositories are directories on the machine where Maven is run. It caches remote downloads and contains temporary build artifacts.

Remote repositories are any other type of repository accessed by protocols such as https://.

JakTech

1 Response

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.