I was recently checking out some code that was posted on Oracle tutorials regarding Swing#JTable. There were a few warning messages that were returned by IntelIJ regarding the boxing of primitive variables inside the multidimensional array holding the Table data.
Here is the array found which is taken from docs.oracle.com:
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
All the wrapped variables were receiving this message:
"Unnecessary boxing 'new Integer(5)' Reports "boxing", e.g. wrapping of primitive values in objects. Boxing is unnecessary under Java 5 and newer, and can be safely removed. This inspection only reports if the project or module is configured to use a language level of 5.0 or higher."
I know the concept of boxing and unboxing in Java, my question would be as to why it's 'irrelevant' in newer version of Java as I've seen many developers discuss it or use it recently.
Also, since boxing is not required what should 'new Integer(5)' be replaced with?
Thanks to autoboxing in Java 5 and newer, you don't have to call the Integer and Boolean constructors to manually "box" the primitive values. The IDE seems to recommend you write the code as:
Object[][] data = {
{"Kathy", "Smith", "Snowboarding", 5, false},
{"John", "Doe", "Rowing", 3, true},
{"Sue", "Black", "Knitting", 2, false},
{"Jane", "White", "Speed reading", 20, true},
{"Joe", "Brown", "Pool", 10, false}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With