Syntactic Sugar Is Not Always Good

This write-up is partly inspired by a recent post by Vlad Mihalcea on LinkedIn about the recently introduced text blocks in Java. More about them can be read here. Now, that’s a nice feature. I’ve used it in Scala several years ago, and other languages also have it, so it seems like a no-brainer to introduce it in Java. But, syntactic sugar (please don’t argue whether that’s precisely syntactic sugar or not) can be problematic and lead to “syntactic diabetes”. It has two possible issues. The less important one is consistency – if you can do one thing in multiple, equally valid ways, that introduces inconsistency in the code and pointless arguments of “the right way to do things”. In this context – for 2-line strings do you use a text block or not? Should you do multi-line formatting for simple strings or not? Should you configure checkstyle rules to reject one or the other option and in what circumstances? The second, and bigger problem, is code readability. I know it sounds counter-intuitive, but bear with me. The example that Vlad gave illustrates that – do you want to have a 20-line SQL query in your Java code? I’d say no – you’d better extract that to a separate file, and using some form of templating, populate the right values. This is certainly readable when you browse the code: String query = QueryUtils.loadQuery("age-query.sql", timestamp, diff, other); // or String query = QueryUtils.loadQuery("age-query.sql", Arrays.asList("param1Name", "param2Name"), Arrays.asList(param1Value, param2Value); Queries can be placed in /src/main/resources and loaded as templates (and cached) by the QueryUtils. And because of the previous lack of text...

A Disk-Backed ArrayList

It sometimes happens that your list can become too big to fit in memory and you have to do something in order to avoid running out of memory. The proper way to do that is streaming – instead of fitting everything in memory, you should stream data from the source and discard the entries that are already processed. However, there are cases when code that’s outside of your control requires a List and you can’t use streaming. These cases are rather rare but in case you hit them, you have to find a workaround. One is to re-implement the code to work with streaming, but depending on the way the library is written, it may not be possible. So the other option is to use a disk-backed list – one that works as a list, but underneath stores and loads elements from disk. Searching for existing solutions results in several 3+ years old repos like this one and this one and this one. And then there’s MapDB, which is great and supported. It’s mostly about maps, but it does support a List as well, as shown here. And finally, you have the option to implement something simpler yourself, in case you need just iteration and almost nothing else. I’ve done it here – DiskBackedArrayList.java. It doesn’t support many things (not all methods are overridden to throw an exception, but they should). But most importantly, it doesn’t support random adding and random getting, and also toArray(). It’s purely “fill the collection” and then “iterate the collection”. It relies on ObjectOutputStream which is not terribly efficient, but is simple to use....

Becoming a Junior Java Developer 101

Hello everyone,in order to become a Java developer you don’t need much ;), however if you start learning everything you probably will be overwhelmed and will focus on the wrong bits and basically will probably give up.However I have to say DON’T give up. Java is extremely easy language, maybe the easiest to start with, it is the most easy language to understand and to read (in case someone else has written the code) so this is the primary reason why Java is the most commonly used language.The issue with JavaScript is ones you learn the language and bits of the SDK (Standard Development Kit) you will be overwhelmed by a HUGE number of Libraries and Frameworks, you will not know what to learn and how to combine and how to connect different frameworks in order to have a finished product.So I hope this guides will give you some direction and by reading and understanding EVERYTHING in this guide you should be more than ready to join some company as a Junior Java Developer. All materials are completely FREE and are far better then any academy or university course and etc.Why you will choose an Academy or University or Udemy course ? The only reason is if you are lazy. You see in order to understand and start applying something you need to first learn it and then to use it to write something. Most people just read without the writing bit, or try to write before they have learned anything (which is better case then the first in my opinion) but in both cases this creates a huge,...

Use Multiple JVM versions on Mac OS and Linux

Linux Download multiple Java versions and put them into /opt/ If you already have some JDK from ubuntu repo or etc not a big deal, just fix the paths bellow Register them as alternatives sudo update-alternatives –install /usr/bin/java java /opt/java-8-oracle/bin/java 1081sudo update-alternatives –install /usr/bin/java java /opt/sap-machine-jdk-11.0.3/bin/java 1080 Edit your ~/.bashrc file alias java11=’sudo update-alternatives –set java /opt/sapmachine-jdk-11.0.3/bin/java;export JAVA_HOME=/opt/sapmachine-jdk-11.0.3/’alias java8=’sudo update-alternatives –set java /opt/java-8-oracle/bin/java;export JAVA_HOME=/usr/lib/java-8-oracle/’ SAVE and start a new bash terminal execute java8 to use java8java11 to use java11 the latest version you have set stays as system wide, but the JAVA_HOME is not πŸ™ you can put java8 or java11 as a last line in the bashrc but since it is sudo it will always require password when start and is not great ;( Mac Install homebrew, since it rox ! Install Oracle Java 8 or OpenJDK 8. I recommend adoptopenjdk brew tap adoptopenjdk/openjdkbrew search adoptopenjdkbrew cask install adoptopenjdk8brew cask install adoptopenjdk11 On mac since it RULZ you have a java_home executable (that changes and fixes both your path and your JAVA_HOME) , so the .bashrc changes are easy ! Edit your ~/.bashrc file export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)export JAVA_11_HOME=$(/usr/libexec/java_home -v11)alias java8=’export JAVA_HOME=$JAVA_8_HOME’alias java11=’export JAVA_HOME=$JAVA_11_HOME’java8 Note: the latest execution of java8 is to make it system wide by default SAVE and start a new bash terminal execute java8 to use java8 java11 to use java11 Windows Use a normal OS or suffer...

Use Multiple JVM versions on Mac OS and Linux

Linux Download multiple Java versions and put them into /opt/ If you already have some JDK from ubuntu repo or etc not a big deal, just fix the paths bellow Register them as alternatives sudo update-alternatives –install /usr/bin/java java /opt/java-8-oracle/bin/java 1081sudo update-alternatives –install /usr/bin/java java /opt/sap-machine-jdk-11.0.3/bin/java 1080 Edit your ~/.bashrc file alias java11=’sudo update-alternatives –set java /opt/sapmachine-jdk-11.0.3/bin/java;export JAVA_HOME=/opt/sapmachine-jdk-11.0.3/’alias java8=’sudo update-alternatives –set java /opt/java-8-oracle/bin/java;export JAVA_HOME=/usr/lib/java-8-oracle/’ SAVE and start a new bash terminal execute java8 to use java8java11 to use java11 the latest version you have set stays as system wide, but the JAVA_HOME is not πŸ™ you can put java8 or java11 as a last line in the bashrc but since it is sudo it will always require password when start and is not great ;( Mac Install homebrew, since it rox ! Install Oracle Java 8 or OpenJDK 8. I recommend adoptopenjdk brew tap adoptopenjdk/openjdkbrew search adoptopenjdkbrew cask install adoptopenjdk8brew cask install adoptopenjdk11 On mac since it RULZ you have a java_home executable (that changes and fixes both your path and your JAVA_HOME) , so the .bashrc changes are easy ! Edit your ~/.bashrc file export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)export JAVA_11_HOME=$(/usr/libexec/java_home -v11)alias java8=’export JAVA_HOME=$JAVA_8_HOME’alias java11=’export JAVA_HOME=$JAVA_11_HOME’java8 Note: the latest execution of java8 is to make it system wide by default SAVE and start a new bash terminal execute java8 to use java8 java11 to use java11 Windows Use a normal OS or suffer...