Friday Sep 06

Java Integer

PDF Print E-mail
Wednesday, 15 June 2011 10:17
AddThis Social Bookmark Button

Java Integer Tutorial/examples

Java Integer

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int .In addition, this class provides several methods for converting an int to a String and a String to an int , as well as other constants and methods useful when dealing with an int .

Memory size of a java.lang.Integer

A java.lang.Integer costs 16 bytes. An int value can fit into just 4 bytes. Using an Integer costs 300 percent memory overhead compared to a primitive type.

Passing integer by reference

By default Java parameters are always passed by values. An easy way to pass by reference is to use a single element array.

Let's define a Junit test case to illustrate an example :

Passing a Java integer by reference

    public void passingIntegerByReference(){
        int[] currentCounter = new int[1];
        currentCounter[0] = 1;
        assertEquals ("Counter should have been incremented",2,currentCounter[0]);
    void incrementCounter(int[] reference) {
        reference[0] += reference[0];

Tags: java , type , memory , string , reference , integer , void , methods , costs , java.lang.integer , bytes , primitive

Add comment

Security code

Java Tutorial on Facebook