tags:

views:

3640

answers:

10

How do I multiply 10 to an Integer object and get back the Integer object?

I am looking for the neatest way of doing this.

I would probably do it this way: Get int from Integer object, multiply it with the other int and create another Integer object with this int value.

Code will be something like ...

integerObj = new Integer(integerObj.intValue() * 10);

But, I saw a code where the author is doing it this way: Get the String from the Integer object, concatenate "0" at the end and then get Integer object back by using Integer.parseInt

code is something like ...

String s = integerObj + "0";
integerObj = Integer.parseInt(s);

Is there any merit in doing it either way?

And what would be the most efficient/neatest way in general and in this case?

+5  A: 

The string approach is amusing, but almost certainly a bad way to do it.

Getting the int value of an Integer, and creating a new one will be very fast, where as parseInt would be fairly expensive to call.

Overall, I'd agree with your original approach (which, as others have pointed out, can be done without so much clutter if you have autoboxing as introduced in Java 5).

Matt Sheppard
+20  A: 

With Java 5's autoboxing, you can simply do:

    Integer a = new Integer(2); // or even just Integer a = 2;
    a *= 10;
    System.out.println(a);
toolkit
Using the Integer constructor is not recommended. Use autoboxing or valueOf() instead...
Steven Schlansker
+1  A: 

Keep away from the second approach, best bet would be the autoboxing if you're using java 1.5, anything earlier your first example would be best.

Galbrezu
A: 

The solution using the String method is not so good for a variety of reasons. Some are aesthetic reasons others are practical.

On a practical front more objects get created by the String version than the more normal form (as you have expressed in your first example).

On an aesthetic note, I think that the second version obscures the intent of the code and that is nearly as important as getting it to produce the result you want.

Clokey
A: 

I agree with @Matt Sheppard and @Jonathan Holland -- you should not do math on strings.

It does remind me of shifting bits to cheaply multiply/divide by powers of 2, though.

pkaeding
+1  A: 

I believe that should count as one of the worst examples (or coding horrors, as Steve/Jeff would have it) .

Thanks all.

I will accept toolkit's answer as it added something more and would vote up for Matt.

Jagmal
+1  A: 

I'd also advise you to check if using Integer is REALLY needed. Joshua pointed that using primitives is MANY MANY times faster in Effective Java. So, if you can, stick with int.

Marcio Aguiar
A: 

@marcio: Using Integer is actually a design constraint. The idea is I get an Integer object and I have to update that. So, can't do away with that :)

Jagmal
+2  A: 

The problem with the second way is the way Strings are handled in Java:

  • "0" is converted into a constant String object at compile time.
  • Each time this code is called, s is constructed as a new String object, and javac converts that code to String s = new StringBuilder().append(integerObj.toString()).append("0").toString() (StringBuffer for older versions). Even if you use the same integerObj, i.e.,

    String s1 = integerObj + "0"; String s2 = integerObj + "0";

    (s1 == s2) would be false, while s1.equals(s2) would be true.

  • Integer.parseInt internally calls new Integer() anyway, because Integer is immutable.

BTW, autoboxing/unboxing is internally the same as the first method.

sundae1888
A: 

Programmers are a creative bunch, and we love to find new ways of doing things. The author of the second (String) solution probably thought "hey, a completely new way of multiplying - it's just like bitshift for multiplying by two". He (and I'll bet he was a "he") then showed it to his friends, who also thought "that's cool".

So far that's a great little story. Had it ended there it would have been harmless and, in a geeky way, mentally improving. Finding new ways of doing things helps us think outside the box. Next time he might think of something really useful. But for this case, reality should have taken hold before coding happened. For all the reasons mentioned elsewhere, the String solution is less effective than multiplying Integer. It's slower, uses more memory, and is harder to understand. So while kudos to the author for innovation, it should never have been allowed into the outside world.

DJClayworth