Posted In: Apache, Commons, Security

How to encrypt Password/String using Apache Codec

Apache codec provide multiple methods to encrypt a string Maven commons-codec

Read More

by , on August 24th, 2017

Posted In: Java Core

How to determine the size of an object in Java

JDK 5 provides Instrumentation interface which can be used find Object Size Instrumentation set up requires entry to manifest file Premain-Class: ObjectAgent and use public static void premain(String agentArgs, Istrumentation instrumentation). There is little easy way to do it. Use memory-measurer Run class with javaagent VM argument  

Read More

by , on August 24th, 2017

Posted In: Java Core, String

String vs StringBuilder vs StringBuffer Simple Benchmark test

Since String is immutable in java any changes to it creates a new object. StringBuffer and StringBuilder are mutable objects. They provide methods like append(), insert(), delete() and substring() to manipulate date.   This test clearly shows that always use StringBuilder. It will perform much better than String concatenation. Result

Read More

by , on August 24th, 2017

Posted In: Java Core, String

When to use StringBuilder vs String concatenation

Most of the discussions say that for few fields String concatenation is fine. If code is looping then do use StringBuilder. Use StringBuilder for following kind of code There is slight different issue with this may be approach. Applications which go through various static code analysis tools should always use StringBuilder append than String concatenation. […]

Read More

by , on August 24th, 2017

Posted In: Collections, Java Core

Randomly change order of elements in ArrayList

Shuffle array to change order To pick random object from ArrayList java.util.Collections shuffle code

Read More

by , on August 23rd, 2017

Posted In: IO, Java Core

How to create a file and write text to it

Techniques used 1. Before JDK 7 using BufferedWriter 2. Using JDK 7 Files 3. Apache Commons IOUtils 4. Guava library  

Read More

by , on August 23rd, 2017

Posted In: Date, Java Core

Calculate number of days between two Java date instances

Calculate difference between two Java date instances Techniques used 1. Using java.util.Date Before JDK 8 2. Using java.time.LocalDate JDK 8 3. Using JODA time org.joda.time.Days 4. Using JODA time org.joda.time.Interval

Read More

by , on August 23rd, 2017

Posted In: Caching, Java Core

Do not forget to add serialVersionUID in your POJO

What is a serialVersionUID and why should developer use it in POJO? Why developer should always add serialVersionUID in POJO. In an enterprise application sooner than later you will start using some caching mechanism. Any changes in POJO even if very small can change UID. In such cases all your cached objects will not work. […]

Read More

by , on August 23rd, 2017

Posted In: Date, Java Core

Parsing ISO 8601-compliant String to java.util.Date

Best way to parse an XML dateTime in Java Techniques used 1. JODA Time 2. JAXB  

Read More

by , on August 23rd, 2017

  • Categories