The strictfp modifier in Java

java-card.png

I just learned there are some (not very known) modifiers in the Java language, one in particular is called strictfp.

Natively, java uses the IEE754 for the float and double values , but it can extend the the exponent range where available resulting in more precise results.

But, when we use the keyword strictfp in a class or a method, we are forcing the results of floating-point calculations to be IEEE754, ensuring an identical result on every platform.

This keyword can be used on classes or methods just like the next code shows:

public strictfp class SomeClass {

    ... 
}

 

public  class SomeClass {

    public strictfp Double someMethod(){
      ...  
    }
}

 

Depending where is used it will be the scope of the IEEE754 FP calculations and using the strictfp you may not be able to use some JAVA capabilities for floating point.

SOLID as your design principles

900px-template_method_uml_class_diagram-svg.png

One of the most important concepts that I was asked for in my last job interview was “what is SOLID?, are you familiar with the SOLID principles?”  .

And this is important because SOLID stands for five principles for object designing, you known, before start coding you should design your solution.. right? ….. right? ok.

So what SOLID stands for?

S : Single responsibility – Your class must have one single responsibility, if you are creating a class for “sending a mail” that is the only task that should do, not validating, not connecting, not applying a template, only to send the mail, the other tasks should be done by other classes.

O : Open/Close – When you are designing an application that probably will be need extended in the future, you should be sure that your design is open to extend the functionality but closed to modify it. If you need to modify a core functionality in order to update your application then your design is probably incorrect.

L : Liskov Substitution – After te “O” principle is when SOLID starts to be interesting. Liskov Substitution states that  subclasses  must be suitable for the parent classes,  in other words, if you are inheriting a parent class and you HAVE to override a method with a “return null” or throwing a “NotImplementedException”or you completely changed the functionality of the parent method then you are violating the Substitution Liskov .

I : Interface segregation- This is very similar to Single responsibility but applied to interfaces, interfaces are for the clients not for the application. So you want to segregate your interfaces in particular tasks rather than create a giant interface that makes everything and your client has to create dummy implementations.

D: Dependency Injection – This principle is the most important, DI allows to build loose coupled code, because other class doesn’t know the details of how create an instance another method to start working with it. Instead calling a constructor  a DI manager will inject the instance to the class through the constructor or a setter method.

What is the difference between Struts 2 and Spring MVC?

 

When you look for a MVC framework you will find there is two main candidates: Struts 2 and Spring Framework.

I will not argue which is better, because both are a fit for different situations , not only technical situations.

TL;DR , the main difference between Spring MVC and Struts 2 is the way they handle the requests, Spring MVC IS a servlet while struts 2 uses a filter to execute the action, BUT the learning curve of Spring 2 is way shorter than Spring MVC, if you have a very very complex requirement for a short time you can build an elegant and robust solution with Struts 2, if you have experience with spring and your backend already uses spring framework your best choice is Spring MVC.

Spring MVC

Spring MVC is part of one of the biggest frameworks available for Java :Spring Framework, relies on IoC (Inversion of Control) and complements the OOP with Aspect Oriented Programming by using the dependency injection.

So this means, when you are using Spring MVC you probably have a backend made with Spring so you can access to the application context and work with the beans that are loaded by injecting them to your MVC Controller.

Finally, Spring MVC is designed around a dispatcher servlet that catch all the incoming requests and dispatches to the handlers, so it’s a servlet.

Struts 2

Struts 2 is a stand alone MVC framework which means  you are not tied to a technology in particular, you can integrate it easily with Hibernate, JSP, Spring, etc. In the “old days” you were tied to JSP for the view layer, but now thanks to the plugins you can create a modern singlepage site in integrate it with Struts 2.

Struts 2 uses the Filter from javax to intercept the requests and choose the action required to process it.

The filter (since struts 2.3) is the PrepareAndExecuteFilter that handles the incoming request, for its preparation and execution. So when a new requests is passed by the filter it will search for the required action, then the request is passed to interceptors that will execute tasks like validations, then is passed to the action class to process and generate a result.

The result is passed back by interceptors too and finally the PrepareAndExecuteFilter will deliver the result to the client.