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)
• OpenJ...
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) – base...
Similar projects and technologies
•
•
•
•
•
•
•

Hibernate – NHibernate
log4j – log4net
Play – ASP.NET MVC
JUnit – NUnit
J...
Subtle and not-so-subtle differences
• Language differences
(calls, conventions, execution, syntactic
sugar, code organiza...
Language - Calling methods
•
•
•
•

No out or ref parameter modifiers
No optional parameters
No named parameters
params wr...
Language - Coding conventions
• Methods are camelCased and not PascalCased
• The opening brace is to be put on the same li...
Language - Code execution
• Switch allows fall-through by design
• Convenient multicatch statement :
try { … } catch(IOExc...
Language – Convenience features
• No as feature. Test with instanceOf and then
cast
• No lambdas yet. Promised in Java 8. ...
Language – Organization
• No nested packages in a single file.
namespace Outer
{
namespace Inner
{
…
}
}

• Only one publi...
Language – Syntax
• protected means ‘protected internal’

• implements or extends instead of colon (‘:’)
• No var facility...
Language – Syntax (cont’d)
• No indexers

public int this[int index] { get { … } }

• No #regions
• Binary numeric literal...
Language – data types
• Default access modifier (i.e. not specifying one)
means package (kind of internal), for methods
or...
Language – Enums
•
•
•
•
•
•
•

•
•
•

Reference type – like classes
Enums can have fields and constructor (though it must...
Platform – Architecture
• @Override annotation is optional. Overload instead

•
•
•

•

of override can occur.
Cloning is ...
Platform – Classes
• A method is virtual by default – not like in .NET where
it’s sealed (final) by default. This is how H...
Platform – Execution
• Checked exceptions. Each method must
declare what exceptions it can throw or catch
them.
• RuntimeE...
Platform – Convenience features
• No properties
• No events
• Three ways to simulate events :
– Nested anonymous classes
–...
Platform – Operations
• Primitives have reference-types peers (wrappers)
• Do not confuse them to their primitive
counterp...
Platform – Data types
•
•
•
•

No unsigned numeric data types
No multi-dimensional arrays
No structs or other user-defined...
Platform – others
• Upon deployment desktop apps don’t have an
EXE or other prepared entry point, executable
file
• The Ga...
Further reading
• Enums : http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
• JLS : http://docs.oracle.com/java...
Upcoming SlideShare
Loading in …5
×

Beginning Java for .NET developers

16,657 views

Published on

A (not so) brief comparison of the Java and .NET platform from the point of view of the .NET developer

Published in: Technology
  • Be the first to comment

Beginning Java for .NET developers

  1. 1. Beginning Java for .NET developers
  2. 2. Why learn Java? • Complement your developer skills • Be able to develop Android apps • Target Linux OS
  3. 3. Where do I get Java? • Oracle (official vendor, acquirer of Sun) • IBM (no Windows support – at least in Nov.2013) • OpenJDK (Ubuntu) • many others
  4. 4. IDEs • • • • Eclipse Netbeans (Oracle) IntelliJ IDEA (JetBrains, makers of ReSharper) many others
  5. 5. 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
  6. 6. Similar projects and technologies • • • • • • • Hibernate – NHibernate log4j – log4net Play – ASP.NET MVC JUnit – NUnit JavaFX – WPF Swing/AWT – Winforms JSP/JSF – ASP.NET (Webforms)
  7. 7. Subtle and not-so-subtle differences • Language differences (calls, conventions, execution, syntactic sugar, code organization, syntax) • Platform differences (architecture, classsystem, execution, operations, data types and others)
  8. 8. 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
  9. 9. 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)
  10. 10. 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()) { … }
  11. 11. Language – Convenience features • No as feature. 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
  12. 12. 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
  13. 13. Language – Syntax • protected means ‘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
  14. 14. Language – Syntax (cont’d) • No indexers public int this[int index] { get { … } } • No #regions • Binary numeric literals : int n217 = 0b11011001; • Underscore allowed (and ignored) in numeric literals : int cardNo = 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
  15. 15. 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
  16. 16. 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
  17. 17. 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.
  18. 18. 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<>();
  19. 19. Platform – Execution • Checked exceptions. Each method must declare what exceptions it can throw or catch them. • RuntimeExceptions are exempt • Errors are not catchable
  20. 20. 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)
  21. 21. 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.
  22. 22. 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.
  23. 23. 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)
  24. 24. 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/

×