Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Java is not significantly different from C#. On a purely syntactic level, here are some pointers that may get you through the day:</p> <ol> <li><p>In Java you have two families of exceptions: <code>java.lang.Exception</code> and everything that derives from it, and <code>RuntimeException</code>. This is meaningful because in Java exceptions are <em>checked</em>; this means that in order to throw any non-runtime exception you also need to add a <code>throws</code> annotation to your method declaration. Consequently, any method using yours will have to catch that exception or declare that <em>it</em> also throws the same exception. A lot of exceptions you take for granted, such as <code>NullPointerException</code> or <code>IllegalArgumentException</code>, in fact derive from <code>RuntimeException</code> and you therefore don't need to declare them. Checked exceptions are a point of contention between two disciplines, so I'd recommend you try them out for yourself and see if it helps or annoys you. On a personal level, I think checked exceptions improve code factoring and robustness significantly.</p></li> <li><p>Although Java has supported autoboxing for quite a while, there are still quite a few differences between the C# and Java implementations that you should be aware of. Whereas in C# you can interchangeably use <code>int</code> as both a value type and reference type, in Java they're literally not the same type: you get the primitive value type <code>int</code> and the library reference type <code>java.lang.Integer</code>. This manifests in two common ways: you can't use the value types as a generic type parameter (so you'll use <code>ArrayList&lt;Integer&gt;</code> instead of <code>ArrayList&lt;int&gt;</code>), and the utility methods (such as <code>parse</code> or <code>toString</code>) are statically implemented in the reference type (so it's not <code>int a; a.toString();</code> but rather <code>int a; Integer.toString( a );</code>).</p></li> <li><p>Java has two distinct types of nested classes, C# only has one. In Java a static class that is not declared with the <code>static</code> modifier is called an <em>inner class</em>, and has implicit access to the enclosing class's instance. This is an important point because, unlike C#, Java has no concept of delegates, and inner classes are very often use to achieve the same result with relatively little syntactic pain.</p></li> <li><p>Generics in Java are implemented in a radically different manner than C#; when generics were developed for Java it was decided that the changes will be purely syntactic with no runtime support, in order to retain backwards compatibility with older VMs. With no direct generics support in the runtime, Java implements generics using a technique called <a href="http://download.oracle.com/javase/tutorial/java/generics/erasure.html" rel="noreferrer">type erasure</a>. There are quite a few disadvantages to type erasure over the C# implementation of generics, but the most important point to take from this is that <em>parameterized generic types in Java do not have different runtime types</em>. In other words, after compilation the types <code>ArrayList&lt;Integer&gt;</code> and <code>ArrayList&lt;String&gt;</code> are <em>equivalent</em>. If you work heavily with generics you'll encounter these differences a lot sooner than you'd think.</p></li> </ol> <p>There are, in my opinion, the three hardest aspects of the language for a C# developer to grok. Other than that there's the development tools and class library.</p> <ol> <li><p>In Java, there is a direct correlation between the package (namespace), class name and file name. Under a common root directory, the classes <code>com.example.SomeClass</code> and <code>org.apache.SomeOtherClass</code> will literally be found in <code>com/example/SomeClass.class</code> and <code>org/apache/SomeOtherClass.class</code> respectively. Be wary of trying to define multiple classes in a single Java file (it's possible for private classes, but not recommended), and stick to this directory structure until you're more comfortable with the development environment.</p></li> <li><p>In Java you have the concepts of class-path and class-loader which do not easily map to C# (there are rough equivalents which are not in common use by most .NET developers). Classpath tells the Java VM where libraries and classes are to be found (both yours and the system's shared libraries!), and you can think of class loaders as the context in which your types live. Class loaders are used to load types (class files) from various locations (local disk, internet, resource files, whatnot) but also constrain access to those files. For instance, an application server such as Tomcat will have a class loader for each registered application, or context; this means that a static class in application A will not be the same as a static class in application B, even if they have the same name and even if they share the same codebase. AppDomains provide somewhat similar functionality in .NET.</p></li> <li><p>The Java class library is similar to the BCL; a lot of the differences are cosmetic, but it's enough to get you running for the documentation (and/or Google) over and over again. Unfortunately I don't think there's anything to do here — you'll just build familiarity with the libraries as you go.</p></li> </ol> <p>Bottom line: the only way to grok Java is to use it. The learning curve isn't steep, but prepare to be surprised and frustrated quite often over the first two or three months of use.</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. 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.
    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