Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In java, using a zero as the most significant digit, it interpreters the number as an octal number. So <code>01234</code> gets converted to:</p> <pre><code>4 + 3 x 8 + 2 x 8^(2) + 1 x 8^(3) = 668 </code></pre> <p>So you are better off with using one of the convert-to-string methods as in the other answers.</p> <p><strong>Edit:</strong> Here is a version without StringBuffer etc.:</p> <pre><code>public String getReversed(int number) { char[] digitArray = String.valueOf(number).toCharArray(); String strNum = ""; for (int i = digitArray.length - 1; i &gt;= 0; i--) { strNum += digitArray[i]; } return strNum; } </code></pre> <p><strong>2nd edit:</strong> Reversing a <code>char</code> array and then create the <code>String</code> after finished reversing, because of a possible creation of number of <code>O(n^2)</code> operations that could be a problem for the garbage collector.</p> <pre><code>public String getReversed(int number) { char[] digitArray = String.valueOf(number).toCharArray(); char[] reversedDigitArray = new char[digitArray.length]; for (int i = 0; i &lt; digitArray.length; i++) { reversedDigitArray[i] = digitArray[digitArray.length - i - 1]; } return new String(reversedDigitArray); } </code></pre> <p><strong>3rd Edit:</strong> Swaps chars with in-place algorithm.</p> <pre><code>public static String getReversed(int number) { char[] digitArray = String.valueOf(number).toCharArray(); int count = digitArray.length - 1; char tmp; for (int i = (count-1) &gt;&gt; 1; i &gt;= 0; --i) { tmp = digitArray[i]; digitArray[i] = digitArray[count - i]; digitArray[count - i] = tmp; } return new String(digitArray); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload