What is Autoboxing?

Autoboxing is a new feature offered in the Tiger (1.5) release of Java SDK. In short auto boxing is a capability to convert or cast between object wrappers (Integer, Long, etc) and their primitive types.

Previously when placing primitive data into one of the Java Collection Framework object we have to wrap it to an object because the collection cannot work with primitive data. Also, when calling a method that requires an instance of object rather than an int or long, we have to convert it too.

Now, starting from version Java 1.5 we have a new feature in the Java Language which automate this process, its call the Autoboxing. When we place an int value into a collection, such as List, it will be converted into an Integer object behind the scene. When we read it back, it will automatically convert to the primitive type. In most way this simplifies the way we code, no need to do an explicit object casting.

Here an example how it will look like using the Autoboxing feature:

package org.kodejava.basic;

import java.util.HashMap;
import java.util.Map;

public class Autoboxing {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();

        // Here we put an int into the Map, and it accepted
        // as it will be autoboxed or converted into the wrapper
        // of this type, in this case the Integer object.
        map.put("Age", 25);

        // Here we can just get the value from the map, no need
        // to cast it from Integer to int.
        int age = map.get("Age");

        // Here we simply do the math on the primitive type
        // and got the result as an Integer.
        Integer newAge = age + 10;
    }
}

How do I write Hello World program in Java?

The Hello World program is a classic example to start with when we begin to learn a new programming language. The main purpose is to know the basic syntax of the language. Below is the Java version of a Hello World program, it is simple enough to start.

package org.kodejava.basic;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

The code contains one class called HelloWorld, we declare a class using the class keyword. A main(String[] args) method with a public static void signature and an argument of string array is a special method in Java class. This method is the execution entry point of every Java application. When a class have this method it will be executable. And finally in the body of the method we have a single line of code that write a Hello World string to the console.

The HelloWorld class must be saved in a file named HelloWorld.java, the class file name is case-sensitive. In the example we also define a package name for the class. In this case the HelloWorld.java must be placed in a org\kodejava\basic directory.

To run the application we need to compile it first. I assume that you have your Java in your path. To compile it type:

javac -cp . org.kodejava.basic.HelloWorld.java

The compilation process will result a file called HelloWorld.class, this is the binary version of our program. As you can see that the file ends with .class extension because Java is everything about class. To run it type the command bellow, class name is written without extension.

java -cp . org.kodejava.basic.HelloWorld

And you will see a string Hello World! is printed out in the console as the output of your first Java program, the HelloWorld program.

To install Java Development Kit (JDK) and configure Java environment you can see the following tutorial: