Category Archives: Java

Object Initialization Order in Java

An object is a chunk of memory bundled with the code that manipulates memory. In the memory, the object maintains its state (the values of its instance variables), which can change and evolve throughout its lifetime. To get a newly-created object off to a good start, its newly-allocated memory must be initialized to a proper initial state. Here we take an in-depth look at the mechanisms Java uses to manage object initialization. Java's Initialization Mechanisms At the beginning of an object's life, the Java virtual machine (JVM) allocates enough memory on the heap to accommodate the object's instance variables. When that memory is first allocated, however, the data it contains is unpredictable. If the memory were used as is, the…

Continue Reading

HashMap Internal Implementation Analysis in Java

Most of you will agree that HashMap is a most favorite topic for discussion in interviews nowadays. Now, I am continuing this discussion with you all. I am assuming that if you are interested in the internal working of HashMap, you already know the basics of HashMap, so I'm skipping that part. But if you are new to the concept, follow official java docs. Single Statement Answer If anybody asks me to describe "How HashMap works?", I simply answer: "On the principle of Hashing". As simple as it is. Now before answering it, one must be very sure to know at least the basics of Hashing. Right? What is Hashing Hashing in its simplest form is a way to assign a…

Continue Reading

Integrate Spring MVC with Log4j

It’s quite straightforward to integrate Log4j into the Spring MVC application. First, include Log4j.jar library into your project dependency, then create a file to define the Log4j’s appender and put this file into the project classpath, Done. In the tutorial, we show you how to integrate the Log4j 1.x logging framework into the Spring MVC application. Add Log4j Library to Your Project Download the Log4j library from the official website, or via Maven : Create Create a Log4j properties file (, put it into the project class path. see figure : File : – Define how log4j handles the logged message, in this example, it will redirect all the logged messages into a text. Use Log4j in…

Continue Reading

Unit Testing of Spring MVC Controllers

After all the services, DAO, and support classes are tested then it’s time for the controller. Generally, this is hard to test and most developers (based on observation) would rather just test it via Selenium or worse, by hand. That can work but it makes testing logic branches difficult and not to mention it’s time-consuming. Plus no active developer would be willing to wait for browser tests to run before checking in code. Luckily the Spring MVC Test project can do full controller testing via unit tests, it was such a success that it’s now in Spring MVC core as of version 4.0.5. Getting Ready We can get the required testing dependencies by adding the following dependency declarations to the…

Continue Reading

Spring 4 MVC Hello World Tutorial Using Maven

In this tutorial, you will learn how to develop a Spring 4 MVC Hello world example. We hope this tutorial will give you a quick start with Spring MVC development using the latest Spring 4 Release. Technologies used: Spring 4.0.4.RELEASE JDK 1.8 Tomcat 7.0.53 Maven 3.2.1 Eclipse Java EE IDE ( Eclipse Kepler) Maven Project Setup In Eclipse Let us start with the creation of a Maven web project in Eclipse. A maven web project archetype will create all the necessary folder structures required for a web project. We assume that you have installed the Maven plugins for Eclipse. File -> New -> Other -> Maven -> Maven Project Click Next and Click Next again ( If you wish to…

Continue Reading

How To Convert Byte[] Array To String in Java

In some cases, we have to convert the String variable into a Byte array format, for example, JCE encryption. However, how do we convert a Byte[] array to a String afterward?Simple toString() function like the following code is not a working property. It will not display the original text but the byte value. In order to convert the Byte array into String format correctly, we have to explicitly create a String object and assign the Byte array to it. And here's a sample code: Output

Continue Reading
Contact Us
  • Nanyang Technological University, Singapore
  • root [at] haozhexie [dot] com