240 likes | 395 Views
Beginning Java for .NET developers. Why learn Java?. Complement your developer skills Be able to develop Android apps Target Linux OS. Where do I get Java?. Oracle (official vendor, acquirer of Sun) IBM (no Windows support – at least in Nov.2013) OpenJDK (Ubuntu) many others. IDEs.
E N D
Why learn Java? • Complement your developer skills • Be able to develop Android apps • Target Linux OS
Where do I get Java? • Oracle (official vendor, acquirer of Sun) • IBM (no Windows support – at least in Nov.2013) • OpenJDK (Ubuntu) • many others
IDEs • Eclipse • Netbeans (Oracle) • IntelliJ IDEA (JetBrains, makers of ReSharper) • many others
Common features • Object-oriented • Compiled, not interpreted • Statically typed • Type safe • Runtime (JVM) – based (available on many platforms) • Garbage collected • Allows native interoperability • Runs on mobile devices (smartphones, tablets, even feature-phones) • JLS / CLS – multiple languages on the same platform
Similar projects and technologies • Hibernate – NHibernate • log4j – log4net • Play – ASP.NET MVC • JUnit – NUnit • JavaFX – WPF • Swing/AWT – Winforms • JSP/JSF – ASP.NET (Webforms)
Subtle and not-so-subtle differences • Language differences (calls, conventions, execution, syntactic sugar, code organization, syntax) • Platform differences (architecture, class-system, execution, operations, data types and others)
Language - Calling methods • No out or ref parameter modifiers • No optional parameters • No named parameters • params written as “…”public void doSomething(Object…args) { … } • No extension methods
Language - Coding conventions • Methods are camelCased and not PascalCased • The opening brace is to be put on the same line :public void doSomething() {} • Interfaces are not prefixed with a capital I :Throwable, Runnable etc. • Enum values are all-uppercase • Abbreviation words in compound names can be all-caps : URLParser (as opposed to .NET’s UrlParser)
Language - Code execution • Switch allows fall-through by design • Convenient multicatch statement :try { … } catch(IOException | NetworkException e) { … } • Override return in finally • No #Ifdef • Try-with-resources as using equivalent :try(IOStream s = new IOStream()) { … }
Language – Convenience features • No asfeature. Test with instanceOf and then cast • No lambdas yet. Promised in Java 8. Use anonymous inner classes • Static method import. Like extensions methods but the other way around.import static com.something.Otherclass.someMethod;…someMethod(..); • No explicit interface implementation
Language – Organization • No nested packages in a single file.namespace Outer{ namespace Inner { … }} • Only one public class in a .java file • The public class name must match case-wise the filename • No partial classes • No partial methods
Language – Syntax • protectedmeans ‘protected internal’ • implements or extends instead of colon (‘:’) • No var facility • foreach syntax :for(Type variable : collection){…} • instanceOf instead of is • SomeClass.class instead of typeof(SomeClass) • Annotations are prefixed with @ and not ‘[‘, ‘]’ • .. can also be applied to assignment statements
Language – Syntax (cont’d) • No indexerspublic int this[int index] { get { … } } • No #regions • Binary numeric literals :int n217 = 0b11011001; • Underscore allowed (and ignored) in numeric literals :intcardNo = 1234_5678_9012_3456; • The base class is called superclass, the derived class is called subclass • Calling the superclass constructor is done within the constructor not outside, it is optional, but if done, must be the first statement of the constructor
Language – data types • Default access modifier (i.e. not specifying one) means package (kind of internal), for methods or fields. • String must be written with capital S. Seems irrelevant but it will be the most common typo. • Interfaces can have static final fields (“constants”) – before enums this was a way to simulate enums. • No operator overloading • No implicit or explicit conversions can be defined on types
Language – Enums • Reference type – like classes • Enums can have fields and constructor (though it must be private) • You can override methods. Typically toString() • Lazily created. Each enum value is instantiated at its first use. • Abstract methods, overridable in specific values • Allows inheritance. Although it has the semantic value of a superset, not subset. • valueOf(String name) instead of Enum.Parse • values() for enumerating all values • name() – gets the enum value name; typically toString() does the same but the latter can get overriden • ordinal() – gets the order index of the value
Platform – Architecture • @Override annotation is optional. Overload instead of override can occur. • Cloning is awkard, non-intuitive. Cloneable marker interface and protected clone(). • No static classes. Make it final, create a private constructor and throw an exception inside the constructor • Return type covariance : Override a method and return a subclass of the original method’s return type class. Typical use : overriding clone(). • You can alter a collection while iterating it without getting an exception, but only if done through the current iterator.
Platform – Classes • A method is virtual by default – not like in .NET where it’s sealed (final) by default. This is how Hibernate strived and NHibernate struggles. • Generics are implemented using type erasure. No List<primitive> and no arrays of generic types. • No (known) way to create a generic type, by reflection, at runtime. • Inner classes have outer class reference by default. Except static inner classes. • No method generators. i.e. yield • Type inference by ‘diamond’ style constructors.List<Integer> = new ArrayList<>();
Platform – Execution • Checked exceptions. Each method must declare what exceptions it can throw or catch them. • RuntimeExceptions are exempt • Errors are not catchable
Platform – Convenience features • No properties • No events • Three ways to simulate events : • Nested anonymous classes • Implementing interfaces and passing this • Lambdas (promised in Java 8)
Platform – Operations • Primitives have reference-types peers (wrappers) • Do not confuse them to their primitive counterparts. == operator will compare instances instead of values • Strings also should not be compared with == operator. Although there is string interning this will usually fail you • No checked mode. Numeric wrap-around always.
Platform – Data types • No unsigned numeric data types • No multi-dimensional arrays • No structs or other user-defined value-types • No dynamic infrastructure. Only in non-Java languages compiled to bytecode. • The primitives are not part of the type hierarchy. Only their wrappers are.
Platform – others • Upon deployment desktop apps don’t have an EXE or other prepared entry point, executable file • The Garbage Collector does not have a (separate) Large Object Heap, nor its associated issues (fragmentation)
Further reading • Enums : http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html • JLS : http://docs.oracle.com/javase/specs/ • Java 8 Release date : http://openjdk.java.net/projects/jdk8/