Articles on Java

Things To Remember - Java integer division

  • access_time 9 years ago
  • date_range 19/04/2013
  • comment 0
  • share 0
  • label_outlineJava

Another one which is counterintuitive to people programming in untyped languages while doing Java integer division, is that the result will be integer if both dividend and divisor were integers, regardless of the type of the quotient. See the following test case:

public void shouldShowHowDivisionWorks() {
    int a = 3;
    int b = 5;
    float c = a / b;
    assertEquals(0.0, c, 1e-3); // this is true, but may be unexpected
    c = a / (float) b;
    assertEquals(0.6, c, 1e-3); // this also is true
    c = (float) a / b;
    assertEquals(0.6, c, 1e-3); // this is true too

Things To Remember - NullPointerException on if with null boolean value

  • access_time 9 years ago
  • date_range 18/04/2013
  • comment 0
  • share 0
  • label_outlineJava

Update 12/2017: Could just smile at this now.

This one occurred to me today and it's caused by laziness I got used to while coding in untyped languages :) The condition in if statement should evaluate to boolean. If it's null, then NullPointerException is thrown as in the following test case.

@Test(expected = NullPointerException.class)
public void shouldThrowNullPointerException() throws Exception {
    Boolean someFlag = null;
    if (someFlag) {
        // unexpected
    } else {
        // could be expected

Few Recommendations on Logging

Maybe it's basic, but still, sometimes some suboptimal usage of logger can be seen. There also is/was a notion that Java applications tend to overuse logging. That may or not be true, but using the logger properly should have minimal performance implications on the application. This one is to give you few recommendations for logging things in Java, based on my experience. Feel free to disagree in comments.

Spring Boot Security Application

Update 12/2017: It will need an update/rewrite since Spring Security 5 is coming.

Spring Security had some opinions of being complicated to use. Well, of course it's quite complicated when you look at it, as its scope covers a lot of use-cases. Thing is that, truly in a Spring spirit, you don't have to use every feature there is at once for the use-case you are having. In fact, when you start cherry-picking and back it with Spring Boot, it doesn't appear so complicated anymore.

Application Metrics With Spring Boot Actuator

Update 12/2017: It will need an update/rewrite since Spring Boot 2.0 is coming.

Having metrics collected is vital for ...just anything, besides relationships maybe :) My favourite quote by Deming goes like this: "You can't manage what you can't measure". Without it either your experience, prediction and planning skills are so awesome that everything works as expected, or you're just deluding yourself. It's hard data that gives you feedback to confront your actions with reality. It's not only important on company-wide level, or in project management, processes, or when counting conversions in Google Analytics. There are metrics you can collect down on the application level, so can have insight on how it is performing, being used and that it works at all. A nice tool exists for Spring Boot apps to do that, and this is Spring Actuator I'm going to write about today.