Widening and Boxing Java primitives?

Short answer The java language only supports some level of carelessness Longer answer It is my believe that autoboxing was added to support developer carelessness. Specifically in situations like this: "I need an Integer as a parmeter to the method I want to call, but I have an int. Somehow, new Integer(int) never pops into my head.

Instead, I'll just send an int and the java compiler will do the new Integer() call for me. Thanks java carelessness support group! The folks designing autoboxing were willing to support 1 level of carelessness (int => Integer and back), but were not willing to support auto casting of smaller primitive types to larger primitive types in conjunction with automatic creation and extration from primitive type wrapper classes.

I suspect the descision matrix for this would be somewhat larger than the decision matrix for the current autoboxing scheme.

Short answer The java language only supports some level of carelessness. Longer answer It is my believe that autoboxing was added to support developer carelessness. Specifically in situations like this: "I need an Integer as a parmeter to the method I want to call, but I have an int.

Somehow, new Integer(int) never pops into my head. Instead, I'll just send an int and the java compiler will do the new Integer() call for me. Thanks java carelessness support group!"

The folks designing autoboxing were willing to support 1 level of carelessness (int => Integer and back), but were not willing to support auto casting of smaller primitive types to larger primitive types in conjunction with automatic creation and extration from primitive type wrapper classes. I suspect the descision matrix for this would be somewhat larger than the decision matrix for the current autoboxing scheme.

– ziggy Aug 10 at 17:28 @DwB not carelessness, laziness, which is completely different. It's obvious that you don't think this is a good idea, but I think most developers would disagree with you. And most new (newer than Java) languages take this quite a bit further with inferred typing or dynamic typing.

– Bigwheels Aug 10 at 17:34.

Because boxing / autoboxing is only some compiler sugar and not a new type system. It's badly designed and causes trouble at least as often as it simplifies things. But here are some workarounds for your compile error: goInteger((int) b); // these are equivalent goInteger(((Byte) b).intValue()); goInteger(Byte.

ValueOf(b).intValue()).

Btw, another (ugly) option is to define this method as #goInteger(Object o), and then check & cast (& convert) in method body - Byte wrapper instance will be passed. – Arturs Licis Aug 10 at 16:26 Thanks Sean. I wasnt really after a workaround.

I was more interested in the technical reason as to why it was decided that this will not be allowed/possible. – ziggy Aug 10 at 16:27.

If we allow too many magic conversions, it'll get very confusing. The existing conversion rules are already more than people care to understand. Even the language spec got it wrong!

See this funny example Java casting: is the compiler wrong, or is the language spec wrong, or am I wrong?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions