Passing Arguments
Home ] Up ] [ Passing Arguments ] Returning Values ] Autoboxing ]

 

 

If you wish to pass an argument of a primitive type to a method that expects a reference to an object, then you wrap the value in its corresponding wrapper class:

package example;

public class PassIntTest
{
  public static void handleValue(Object value)
  {
    // Do something with value...
  }

  public static void main(String[] args)
  {
    int intValue = 56;
    handleValue( new Integer(intValue) );
  }
}

The above code will compile, and the instance of class Integer is passed in as the object instance value.

Note: All the wrapper classes live in the java.lang package.

 

This page was last modified on 02 October, 2007