<p>How do I multiply 10 to an <code>Integer</code> object and get back the <code>Integer</code> object?</p>

<p>I am looking for the neatest way of doing this.</p>

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

<p>Code will be something like ... </p>

<pre><code>integerObj = new Integer(integerObj.intValue() * 10);
</code></pre>

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

<p>The code is something like this:</p>

<pre><code>String s = integerObj + "0";
integerObj = Integer.parseInt(s);
</code></pre>

<p>Is there any merit in doing it either way?</p>

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

[{'answer_id': 45973, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 3, 'selected': False, 'text': "<p>The string approach is amusing, but almost certainly a bad way to do it.</p>\n\n<p>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.</p>\n\n<p>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).</p>\n"}, {'answer_id': 45975, 'author': 'toolkit', 'author_id': 3295, 'author_profile': 'https://Stackoverflow.com/users/3295', 'pm_score': 6, 'selected': True, 'text': '<p>With Java 5\'s <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html" rel="nofollow noreferrer">autoboxing</a>, you can simply do:</p>\n\n<pre><code>Integer a = new Integer(2); // or even just Integer a = 2;\na *= 10;\nSystem.out.println(a);\n</code></pre>\n'}, {'answer_id': 45980, 'author': 'Galbrezu', 'author_id': 1419, 'author_profile': 'https://Stackoverflow.com/users/1419', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 45981, 'author': 'Clokey', 'author_id': 2438, 'author_profile': 'https://Stackoverflow.com/users/2438', 'pm_score': 1, 'selected': False, 'text': '<p>The solution using the String method is not so good for a variety of reasons. Some are aesthetic reasons others are practical. </p>\n\n<p>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).</p>\n\n<p>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.</p>\n'}, {'answer_id': 46424, 'author': 'ckpwong', 'author_id': 2551, 'author_profile': 'https://Stackoverflow.com/users/2551', 'pm_score': 2, 'selected': False, 'text': '<p>The problem with the second way is the way Strings are handled in Java:</p>\n\n<ul>\n<li><code>"0"</code> is converted into a constant String object at compile time.</li>\n<li><p>Each time this code is called, <code>s</code> is constructed as a new String object, and <code>javac</code> converts that code to <code>String s = new StringBuilder().append(integerObj.toString()).append("0").toString()</code> (StringBuffer for older versions).  Even if you use the same <code>integerObj</code>, i.e.,</p>\n\n<p><code>String s1 = integerObj + "0";\n String s2 = integerObj + "0";</code></p>\n\n<p><code>(s1 == s2)</code> would be <code>false</code>, while <code>s1.equals(s2)</code> would be <code>true</code>.</p></li>\n<li><p><code>Integer.parseInt</code> internally calls <code>new Integer()</code> anyway, because <code>Integer</code> is immutable.</p></li>\n</ul>\n\n<p>BTW, autoboxing/unboxing is internally the same as the first method.</p>\n'}, {'answer_id': 24281194, 'author': 'Chris', 'author_id': 1460848, 'author_profile': 'https://Stackoverflow.com/users/1460848', 'pm_score': 0, 'selected': False, 'text': '<p>toolkit\'s answer above is correct and the best way, but it doesn\'t give a full explanation of what is happening.\nAssuming Java 5 or later:</p>\n\n<pre><code>Integer a = new Integer(2); // or even just Integer a = 2;\na *= 10;\nSystem.out.println(a); // will output 20\n</code></pre>\n\n<p>What you need to know is that this is the exact same as doing:</p>\n\n<pre><code>Integer a = new Integer(2); // or even just Integer a = 2;\na = a.intValue() * 10;\nSystem.out.println(a.intValue()); // will output 20\n</code></pre>\n\n<p>By performing the operation (in this case *=) on the object \'a\', you are not changing the int value inside the \'a\' object, but actually assigning a new object to \'a\'.\nThis is because \'a\' gets auto-unboxed in order to perform the multiplication, and then the result of the multiplication gets auto-boxed and assigned to \'a\'.</p>\n\n<p>Integer is an immutable object. (All wrapper classes are immutable.)</p>\n\n<p>Take for example this piece of code:</p>\n\n<pre><code>static void test() {\n    Integer i = new Integer(10);\n    System.out.println("StartingMemory: " + System.identityHashCode(i));\n    changeInteger(i);\n    System.out.println("Step1: " + i);\n    changeInteger(++i);\n    System.out.println("Step2: " + i.intValue());\n    System.out.println("MiddleMemory: " + System.identityHashCode(i));\n}\n\nstatic void changeInteger(Integer i) {\n    System.out.println("ChangeStartMemory: " + System.identityHashCode(i));\n    System.out.println("ChangeStartValue: " + i);\n    i++;\n    System.out.println("ChangeEnd: " + i);\n    System.out.println("ChangeEndMemory: " + System.identityHashCode(i));\n}\n</code></pre>\n\n<p>The output will be:</p>\n\n<pre><code>StartingMemory: 1373539035\nChangeStartMemory: 1373539035\nChangeStartValue: 10\nChangeEnd: 11\nChangeEndMemory: 190331520\nStep1: 10\nChangeStartMemory: 190331520\nChangeStartValue: 11\nChangeEnd: 12\nChangeEndMemory: 1298706257\nStep2: 11\nMiddleMemory: 190331520\n</code></pre>\n\n<p>You can see the memory address for \'i\' is changing (your memory addresses will be different).</p>\n\n<p>Now lets do a little test with reflection, add this onto the end of the test() method:</p>\n\n<pre><code>System.out.println("MiddleMemory: " + System.identityHashCode(i));\ntry {\n    final Field f = i.getClass().getDeclaredField("value");\n    f.setAccessible(true);\n    f.setInt(i, 15);\n    System.out.println("Step3: " + i.intValue());\n    System.out.println("EndingMemory: " + System.identityHashCode(i));\n} catch (final Exception e) {\n    e.printStackTrace();\n}\n</code></pre>\n\n<p>The additional output will be:</p>\n\n<pre><code>MiddleMemory: 190331520\nStep2: 15\nMiddleMemory: 190331520\n</code></pre>\n\n<p>You can see that the memory address for \'i\' did not change, even though we changed its value using reflection.<br>\n(DO NOT USE REFLECTION THIS WAY IN REAL LIFE!!)</p>\n'}]
['https://Stackoverflow.com/questions/45964', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4406/']