<p>I want to convert a primitive to a string, and I tried:</p>

<pre><code>myInt.toString();
</code></pre>

<p>This fails with the error:</p>

<pre><code>int cannot be dereferenced
</code></pre>

<p>Now, I get that primitives are not reference types (ie, not an Object) and so cannot have methods.  However, Java 5 introduced autoboxing and unboxing (a la C#... which I never liked in C#, but that's beside the point).  So with autoboxing, I would expect the above to convert myInt to an Integer and then call toString() on that.</p>

<p>Furthermore, I believe C# allows such a call, unless I remember incorrectly.  Is this just an unfortunate shortcoming of Java's autoboxing/unboxing specification, or is there a good reason for this?</p>

[{'answer_id': 4247, 'author': 'Justin Standard', 'author_id': 92, 'author_profile': 'https://Stackoverflow.com/users/92', 'pm_score': 7, 'selected': True, 'text': '<p>Java autoboxing/unboxing doesn\'t go to the extent to allow you to dereference a primitive, so your compiler prevents it. Your compiler still knows <code>myInt</code> as a primitive.  There\'s a paper about this issue at <a href="http://jcp.org/aboutJava/communityprocess/jsr/tiger/autoboxing.html" rel="noreferrer">jcp.org</a>.</p>\n\n<p>Autoboxing is mainly useful during assignment or parameter passing -- allowing you to pass a primitive as an object (or vice versa), or assign a primitive to an object (or vice versa).</p>\n\n<p>So unfortunately, you would have to do it like this: (kudos Patrick, I switched to your way)</p>\n\n<pre><code>Integer.toString(myInt);\n</code></pre>\n'}, {'answer_id': 4256, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 5, 'selected': False, 'text': '<p>Ditto on what Justin said, but you should do this instead:</p>\n\n<pre><code>Integer.toString(myInt);\n</code></pre>\n\n<p>It saves an allocation or two and is more readable.</p>\n'}, {'answer_id': 4384, 'author': 'dlinsin', 'author_id': 198, 'author_profile': 'https://Stackoverflow.com/users/198', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>seems like a shortcoming of the\n  specification to me</p>\n</blockquote>\n\n<p>There are more shortcomings and this is a subtle topic. Check <a href="http://kiranthakkar.blogspot.com/2007/05/method-overloading-with-new-features-of.html" rel="noreferrer">this</a> out:</p>\n\n<pre><code>public class methodOverloading{\n   public static void hello(Integer x){\n      System.out.println("Integer");\n   }\n\n   public static void hello(long x){\n      System.out.println("long");\n   }\n\n   public static void main(String[] args){\n      int i = 5;\n      hello(i);\n   }\n}\n</code></pre>\n\n<p>Here "long" would be printed (haven\'t checked it myself), because the compiler choses widening over autoboxing. Be careful when using autoboxing or don\'t use it at all!</p>\n'}, {'answer_id': 7083, 'author': 'SaM', 'author_id': 883, 'author_profile': 'https://Stackoverflow.com/users/883', 'pm_score': 4, 'selected': False, 'text': '<p>One other way to do it is to use:</p>\n\n<pre><code>String.valueOf(myInt);\n</code></pre>\n\n<p>This method is overloaded for every primitive type and <code>Object</code>. This way you don\'t even have to think about the type you\'re using. Implementations of the method will call the appropriate method of the given type for you, e.g. <code>Integer.toString(myInt)</code>.</p>\n\n<p>See <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html" rel="noreferrer">http://java.sun.com/javase/6/docs/api/java/lang/String.html</a>.</p>\n'}, {'answer_id': 55352, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 1, 'selected': False, 'text': '<p>In C#, integers are neither reference types nor do they have to be boxed in order for <em>ToString()</em> to be called. They <strong>are</strong> considered objects in the Framework (as a ValueType, so they have value semantics), however. In the CLR, methods on primitives are called by "indirectly" loading them onto the stack (ldind).</p>\n'}, {'answer_id': 128153, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 3, 'selected': False, 'text': "<p>The valid syntax closest to your example is</p>\n\n<pre><code>((Integer) myInt).toString();\n</code></pre>\n\n<p>When the compiler finishes, that's equivalent to</p>\n\n<pre><code>Integer.valueOf(myInt).toString();\n</code></pre>\n\n<p>However, this doesn't perform as well as the conventional usage, <code>String.valueOf(myInt)</code>, because, except in special cases, it creates a new Integer instance, then immediately throws it away, resulting in more unnecessary garbage. (A small range of integers are cached, and access by an array access.) Perhaps language designers wanted to discourage this usage for performance reasons.</p>\n\n<p>Edit: I'd appreciate it if the downvoter(s) would comment about why this is not helpful.</p>\n"}, {'answer_id': 2123426, 'author': 'Dan Rosenstark', 'author_id': 8047, 'author_profile': 'https://Stackoverflow.com/users/8047', 'pm_score': 2, 'selected': False, 'text': '<p>As everyone has pointed out, autoboxing lets you simplify some code, but you cannot pretend that primitives are complex types.</p>\n\n<p>Also interesting: <a href="http://gafter.blogspot.com/2008/01/is-java-dying.html" rel="nofollow noreferrer">"autoboxing is a compiler-level hack"</a> in Java. Autoboxing is basically a strange kludge added onto Java. Check out <a href="http://bexhuff.com/2006/11/java-1-5-autoboxing-wackyness" rel="nofollow noreferrer">this post</a> for more details about how strange it is.</p>\n'}, {'answer_id': 21469629, 'author': 'supercat', 'author_id': 363751, 'author_profile': 'https://Stackoverflow.com/users/363751', 'pm_score': 2, 'selected': False, 'text': "<p>It would be helpful if Java defined certain static methods to operate on primitive types and built into the compiler some syntactic sugar so that</p>\n\n<pre><code>5.asInteger\n</code></pre>\n\n<p>would be equivalent to</p>\n\n<pre><code>some.magic.stuff.Integer.asInteger(5);\n</code></pre>\n\n<p>I don't think such a feature would cause incompatibility with any code that compiles under the current rules, and it would help reduce syntactic clutter in many cases.  If Java were to autobox primitives that were dereferenced, people might assume that it was mapping the dereferencing syntax to static method calls (which is effectively what happens in .NET), and thus that operations written in that form were no more costly than would be the equivalent static method invocations.  Adding a new language feature that would encourage people to write bad code (e.g. auto-boxing dereferenced primitives) doesn't seem like a good idea, though allowing dereferencing-style methods might be.</p>\n"}]
['https://Stackoverflow.com/questions/4242', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/122/']