beginning java for.net developers. why learn java? complement your developer skills be able to...

24
Beginning Java for .NET developers

Upload: morgan-andrews

Post on 11-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

Beginning Java for .NET developers

Page 2: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

Why learn Java?

• Complement your developer skills• Be able to develop Android apps• Target Linux OS

Page 3: 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

Page 4: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

IDEs

• Eclipse• Netbeans (Oracle)• IntelliJ IDEA (JetBrains, makers of ReSharper)• many others

Page 5: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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

Page 6: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

Similar projects and technologies

• Hibernate – NHibernate• log4j – log4net• Play – ASP.NET MVC• JUnit – NUnit• JavaFX – WPF• Swing/AWT – Winforms• JSP/JSF – ASP.NET (Webforms)

Page 7: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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)

Page 8: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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

Page 9: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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)

Page 10: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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()) { … }

Page 11: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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

Page 12: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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

Page 13: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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

Page 14: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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 :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

Page 15: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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

Page 16: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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

Page 17: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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.

Page 18: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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<>();

Page 19: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

Platform – Execution

• Checked exceptions. Each method must declare what exceptions it can throw or catch them.

• RuntimeExceptions are exempt• Errors are not catchable

Page 20: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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)

Page 21: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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.

Page 22: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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.

Page 23: Beginning Java for.NET developers. Why learn Java? Complement your developer skills Be able to develop Android apps Target Linux OS

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)