The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Weblog:   Defending Autoboxing (or Save Us From the Preprocessor)
Subject:  Shortcut to ...
Date:  2003-09-19 08:53:23
From:  ronaldtm


I think the autoboxing feature comes only to implement the new 'printf' feature (almost-C-like variable parameter list):

C's printf (String s, ...) == J's printf (String s, Object[] params)

then:

int age = 20;
printf ("%s is %d years old", { "Jack", 20 } ); //new shortcut syntax to new Object[]

instead of

printf ("%s is %d years old", { "Jack", new Integer(20) } );

Autoboxing sux, and brings even more danger of hidding performance bottlenecks than Strings.

Moreover, only autoboxing will be supported, not autounboxing, causing the code to handle the same value in different ways. Imagine:

int a = 2;
ArrayList list = new ArrayList();
list.add(a); //add as int
a = list.get(0); //error
a = (int) list.get(0); //error
a = (Integer) list.get(0); //error
a = ((Integer) list.get(0)).intValue(); //get as Integer, cast and get int value

or, with generics,
int a = 2;
ArrayList list = new ArrayList<Integer>();
list.add(a);
a = list.get(0); //error
a = list.get(0).intValue(); //get as Integer, and then get int value

This is the only code to change:
list.add(a); -> list.add(new Integer(a));

 Feed java.net RSS Feeds