Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, while C# and Java are superficially alike there are a number of small differences that might bite you. Generally I think the opposite direction—going from Java to C#—is less problematic. This is mainly due to C# being a more complex language so you might find many simplifications from common Java patterns but the other way around might be a little painful.</p> <p>Things to look out for (partial list, not guaranteed to be exhaustive):</p> <ul> <li><p>Different ...</p> <ul> <li><p>Naming conventions. In Java only type names start with a capital letter (i. e. PascalCase), everything else uses camelCase. Not very hard to adhere to, though.</p> <p>Also interfaces generally don't start with <code>I</code>. On the other hand you have to implement them with a different keyword. Doesn't really help in the middle of the code, though.</p></li> <li><p>Class library :-)</p> <p>While obvious, this has been the thing I spent most time on when learning a language. When dealing with a known paradigm the syntax differences are quickly sorted out, but getting to know the standard library / class library / framework takes some time in some cases :-)</p></li> <li>Patterns. Well, not quite, it's still the same stuff. But C# supports some patterns at the language level, while you still have to implement them yourself in Java. No events, but the Observer pattern (very prevalent in Swing—whenever you see a Listener, you know what to do :-))</li> <li><p>Exception handling. Java has so-called <em>checked exceptions</em> which means that an exception must either be caught or declared upwards. Usually this means that you have</p> <pre><code>catch (SomeException ex) { ex.printStackTrace(); } </code></pre> <p>pretty often in your code<sup>1</sup> :-)</p></li> <li>Types. While .NET has normal objects and value types, they both are objects and support methods, properties, &amp;c. Java has a dichotomy of <em>primitive</em> types, such as <code>int</code>, <code>float</code>, <code>char</code>, &amp;c. and <em>classes</em> such as <code>String</code>. Doesn't matter much since they implemented auto-boxing, but sometimes it's still annoying to wrap <code>int</code> in <code>Integer</code>.</li> <li>Polymorphism: All Java methods are <code>virtual</code> by default whereas c# methods are not.</li> </ul></li> <li>Minor syntactic differences. <ul> <li><code>foreach (a in b)</code> &rarr; <code>for (a : b)</code></li> <li>Different access keywords. Things like <code>internal</code> and <code>protected internal</code> don't exist. But unqualified members are visible to other classes in the same package (sort of <code>internal</code>, but then again not quite).</li> <li>String comparison isn't done with <code>==</code> in Java. You have to use <code>.equals()</code>. While in C# <code>==</code> on strings is value equality, in Java <code>==</code> is <em>always</em> reference equality.</li> </ul></li> <li><p>No ...</p> <ul> <li>Properties. In Java this is generally done with the <code>Foo getFoo()</code>/<code>void setFoo(Foo foo)</code> pattern which C# generates silently behind your back when using properties but you have to do it explicitly in Java. Generally, to keep the language itself simpler many things in Java are just conventions. Still, most of the time you're better off adhering to them :-)</li> <li>Operator overloading. Deemed a hazard to the righteous programmer they weren't implemented for fear of abuse. Don't need them too often anyway, not even in C#, but sometimes they are nice and then you're missing something.</li> <li>Indexers. You always have to access list items through <code>myList.get(5)</code> instead of the array-like syntax <code>myList[5]</code>. Just a mild inconvenience, though.</li> <li>LINQ (though there exist implementations<sup>2</sup> but it's not as nicely integrated), or lambda functions<sup>3</sup> (no delegates anyway, but anonymous classes), extension methods, or partial classes (yes, that's a painful one when dealing with Swing, unless you're <em>very</em> disciplined), and a few more things.</li> <li>Multidimensional arrays. You can use jagged arrays (arrays of arrays), buttrue multidimensionality isn't there.</li> </ul></li> <li>Generics are compile-time only, at runtime only <code>Object</code>s remain. Also wildcards in generics can be hard to resolve sometimes when the compiler complains about all of the four <code>?</code> in your generics having different types. (Though to be fair: That was a case where I would have needed type information at runtime anyway so I reverted back to <code>Object</code>s).</li> </ul> <p><em>General advice:</em> Grab a friend with Java experience and let him glance over your code. While he probably can't tell you everything you should take care of when you directly ask him that question, he can spot strange things in code just fine and notify you of that. This has greatly helped me learning Java (although I learned Java first and then C#, so it might be different).</p> <hr> <p><sup>1</sup> Yes, I know many catch blocks look different, but still, this is probably the archetypical one and <a href="http://googletesting.blogspot.com/2009/09/checked-exceptions-i-love-you-but-you.html" rel="noreferrer">not even that rare</a>.<br> <sup>2</sup> <a href="http://xircles.codehaus.org/projects/quaere" rel="noreferrer">Quaere</a>, <a href="http://code.google.com/p/jaque/" rel="noreferrer">JaQue</a>, <a href="http://h2database.com/html/jaqu.html" rel="noreferrer">JaQu</a>, <a href="http://source.mysema.com/display/querydsl/Querydsl" rel="noreferrer">Querydsl</a><br> <sup>3</sup> There's <a href="http://code.google.com/p/lambdaj/" rel="noreferrer">lambdaj</a>, though. Thanks for pointing that out, <a href="https://stackoverflow.com/users/44523/esko">Esko</a>.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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