Upload
gabriela-matei
View
239
Download
0
Embed Size (px)
Citation preview
7/28/2019 Why Java Sucks and Csharp Rocks
1/118
Why
Java Sucks & C# RocksZhaoJie @ SNDAApril, 2010
7/28/2019 Why Java Sucks and Csharp Rocks
2/118
7/28/2019 Why Java Sucks and Csharp Rocks
3/118
Experiences
Pascal, VB 5 & 6, Delphi before 2002 Java programmer from 2002 to 2004
C# programmer since 2004
7/28/2019 Why Java Sucks and Csharp Rocks
4/118
Im going to talk about ...
7/28/2019 Why Java Sucks and Csharp Rocks
5/118
Languages features
Programming paradigms
Usage patterns
Why we should throw Java language away
7/28/2019 Why Java Sucks and Csharp Rocks
6/118
Im NOT going to makeyou ...
7/28/2019 Why Java Sucks and Csharp Rocks
7/118
Believe CLR is better than JVM
Believe .NET is better than Java platform
Get rid of Java language right away
Use C# instead of Java language
Use .NET instead of Java platform
7/28/2019 Why Java Sucks and Csharp Rocks
8/118
2002 - Java 1.4 & C# 1.0 2004 - Java 5.0 & C# 2.0 2006 - Java 6 2008 - C# 3.0 2010 - Java 7 & C# 4
Timeline
7/28/2019 Why Java Sucks and Csharp Rocks
9/118
So Java programmers,hold your breath ...
7/28/2019 Why Java Sucks and Csharp Rocks
10/118
Lets start from thevery beginning ...
7/28/2019 Why Java Sucks and Csharp Rocks
11/118
Is Java a pure object-oriented language?
7/28/2019 Why Java Sucks and Csharp Rocks
12/118
No.Primitive types are not
objects.
7/28/2019 Why Java Sucks and Csharp Rocks
13/118
ArrayList list = new ArrayList();list.add(5); // cannot compileint i = (int)list.get(0); // cannot compile
int hash = 3.hashCode(); // cannot compile
In Java you can NOT ...
7/28/2019 Why Java Sucks and Csharp Rocks
14/118
You have to ...
ArrayList list = new ArrayList();
Integer five = Integer.valueOf(5);list.add(five);int i = ((Integer)list.get(0)).intValue();
Integer three = Integer.valueOf(3);int hash = three.hashCode();
7/28/2019 Why Java Sucks and Csharp Rocks
15/118
In C# ... No primitive types
All types are subtypes of Object. Just value types and reference types Programmers can build custom value types Value types can be assigned to Object
variables without explicit casting.
7/28/2019 Why Java Sucks and Csharp Rocks
16/118
So you can always ...
ArrayList list = new ArrayList();list.Add(5);int i = (int)list[0];
int hash = 3.GetHashCode();
7/28/2019 Why Java Sucks and Csharp Rocks
17/118
Thank God!Here comes Java 5.0!
7/28/2019 Why Java Sucks and Csharp Rocks
18/118
Finally you can ...
ArrayList list = new ArrayList();list.add(5); // auto boxing
// auto unboxingint i = (Integer)list.get(0);
// you still cant do this!int hash = 3.hashCode();
7/28/2019 Why Java Sucks and Csharp Rocks
19/118
Java 5.0 also brings ... Annotation - allows language constructs to
be tagged with additional data Enumerations - the enum keyword
creates a typesafe, ordered list of values Varargs - make passing an array as
parameter easie r then b efore Enhanced for loop - simplied iteration
http://en.wikipedia.org/wiki/Type_safetyhttp://en.wikipedia.org/wiki/Type_safetyhttp://en.wikipedia.org/wiki/Type_safetyhttp://en.wikipedia.org/wiki/Type_safety7/28/2019 Why Java Sucks and Csharp Rocks
20/118
But most of thesefeatures were alreadyprovided by C# 1.0 ...
7/28/2019 Why Java Sucks and Csharp Rocks
21/118
... so who is thecopy cat?
7/28/2019 Why Java Sucks and Csharp Rocks
22/118
OK, lets forget it andlook deep inside ...
7/28/2019 Why Java Sucks and Csharp Rocks
23/118
Annotations in Java areinterfaces.
Attributes in C# areclasses.
7/28/2019 Why Java Sucks and Csharp Rocks
24/118
Differences?Lets start coding!
7/28/2019 Why Java Sucks and Csharp Rocks
25/118
Annotations shortages
Strange convention Anemic object Hard to unit test
7/28/2019 Why Java Sucks and Csharp Rocks
26/118
Well, Java 5.0 alsobrings Generics, but ...
7/28/2019 Why Java Sucks and Csharp Rocks
27/118
Type erasure: typeinformation will be lost
after compiling!
7/28/2019 Why Java Sucks and Csharp Rocks
28/118
Whats the output?
List a = new ArrayList();List b = new ArrayList();
bool sameType = a.getClass() == b.getClass();System.out.println(sameType);
7/28/2019 Why Java Sucks and Csharp Rocks
29/118
And you cant do this
public class MyClass {public static void myMethod(Object item) {
if (item instanceof E) { // Compiler error...
}E item2 = new E(); // Compiler errorE[] iArray = new E[10]; // Compiler error
}}
7/28/2019 Why Java Sucks and Csharp Rocks
30/118
And use-site varianceis strange and stupid.
7/28/2019 Why Java Sucks and Csharp Rocks
31/118
Well, maybe Im biased.I can give you a
separate talk about it.
7/28/2019 Why Java Sucks and Csharp Rocks
32/118
Let keep moving.
7/28/2019 Why Java Sucks and Csharp Rocks
33/118
The same time Javareleased 5.0 ...
7/28/2019 Why Java Sucks and Csharp Rocks
34/118
C# 2.0 comes with
Better generics (comparing to Java 5.0) Anonymous methods (hello, closure!) yield keyword - iterator creation cant be
more easier
7/28/2019 Why Java Sucks and Csharp Rocks
35/118
Why closure matters?
7/28/2019 Why Java Sucks and Csharp Rocks
36/118
And yield is onlyabout iterator?
7/28/2019 Why Java Sucks and Csharp Rocks
37/118
Lets see some samples.
7/28/2019 Why Java Sucks and Csharp Rocks
38/118
So closure and yield are ...
Increasing productivity dramatically
Coming with new programming patterns Important features for async / parallel /
reactive programming
The primitives for Fibers - lightweightcomputation units
7/28/2019 Why Java Sucks and Csharp Rocks
39/118
Two years after C# 2.0, Java 6 released with ...
7/28/2019 Why Java Sucks and Csharp Rocks
40/118
Nothing!
7/28/2019 Why Java Sucks and Csharp Rocks
41/118
Nothing!!
7/28/2019 Why Java Sucks and Csharp Rocks
42/118
Nothing!!!
7/28/2019 Why Java Sucks and Csharp Rocks
43/118
Give me a break andleave me alone ...
7/28/2019 Why Java Sucks and Csharp Rocks
44/118
OK, I admit it ...
7/28/2019 Why Java Sucks and Csharp Rocks
45/118
Java 6 is cool because of ...
pluggable annotations (JSR 269): customized
compile-time processing (Project Lombok) dramatic JVM improvements: compiler
performnace, start-up time, GC, JIT...
scripting language support (JSR 223): Rhino JavaScript for Java included
http://en.wikipedia.org/wiki/Rhino_%2528JavaScript_engine%2529http://en.wikipedia.org/wiki/Rhino_%2528JavaScript_engine%2529http://en.wikipedia.org/wiki/Rhino_%2528JavaScript_engine%2529http://en.wikipedia.org/wiki/Rhino_%2528JavaScript_engine%2529http://projectlombok.org/http://projectlombok.org/7/28/2019 Why Java Sucks and Csharp Rocks
46/118
But were talking aboutlanguage, so ...
7/28/2019 Why Java Sucks and Csharp Rocks
47/118
Lets move on toC# 3.0,
which brings us ...
7/28/2019 Why Java Sucks and Csharp Rocks
48/118
LINQ(Language Integrated
Query)
7/28/2019 Why Java Sucks and Csharp Rocks
49/118
Only LINQ?
7/28/2019 Why Java Sucks and Csharp Rocks
50/118
Yes! The great LINQ!
7/28/2019 Why Java Sucks and Csharp Rocks
51/118
LINQ is made up of ... Extension method - safely add methods to
types without breaking anything
Lambda expression - make C# a betterfunctional language Anonymous type - compile time auto
generated types Expression tree - language-level code in the
form of data
7/28/2019 Why Java Sucks and Csharp Rocks
52/118
Now tell me, which onedo you prefer?
7/28/2019 Why Java Sucks and Csharp Rocks
53/118
BBCode to HTML
// Java
Util.stripWhites(Util.stripXss(Util.bbToHtml(bbCode)))
// C#bbCode.BBToHtml().StripXss().StripWhites()
7/28/2019 Why Java Sucks and Csharp Rocks
54/118
Sorting an array// C#users.Sort((u1, u2) => u1.Age - u2.Age);
// JavaArrays.sort(users,new Comparator() {
public int compare(User u1, User u2) {return u1.Age - u2.Age;
}});
7/28/2019 Why Java Sucks and Csharp Rocks
55/118
JSON Output (C#)
List userList = ...;var json = new {
errorCode = 0,message = "OK",users = userList.Select(u => new {
name = u.FirstName + " " + u.LastName,age = (DateTime.Now - u.BirthData).Year
}),};
7/28/2019 Why Java Sucks and Csharp Rocks
56/118
JSON Output (Java)JSONObject obj = new JSONObject();obj.put("errorCode", 0);obj.put("message", "OK");
JSONArray userArray = new JSONArray();for (User u: userList) {
JSONObject objUser = new JSONObject();objUser.put("name", /* get full name */);objUser.put("age", /* calculate age */);
userArray.add(objUser);}
obj.put("users", userArray);
7/28/2019 Why Java Sucks and Csharp Rocks
57/118
Is C# just full of Syntactic Sugar?
7/28/2019 Why Java Sucks and Csharp Rocks
58/118
I dont think so.
7/28/2019 Why Java Sucks and Csharp Rocks
59/118
It really changed my mind
Real-world Functional Programming Write declarative code (anti-for) Make code / API more and more elegant
7/28/2019 Why Java Sucks and Csharp Rocks
60/118
Please write a programto index a list of
keywords (like books).
7/28/2019 Why Java Sucks and Csharp Rocks
61/118
Explanation
Input: List Output: Map The key of map is a to z
Each list in the map are sorted
7/28/2019 Why Java Sucks and Csharp Rocks
62/118
Lets see the Java coderst ...
7/28/2019 Why Java Sucks and Csharp Rocks
63/118
List keywords = ...;Map result = new HashMap();
for (String k: keywords) {char firstChar = k.charAt(0);
if (!result.containsKey(firstChar)) {
result.put(firstChar, new ArrayList());}
result.get(firstChar).add(k);}
for (List list: result.values()) {Collections.sort(list);
}
7/28/2019 Why Java Sucks and Csharp Rocks
64/118
List keywords = ...;Map result = new HashMap();
for (String k: keywords) {char firstChar = k.charAt(0);
if (!result.containsKey(firstChar)) {
result.put(firstChar, new ArrayList());}
result.get(firstChar).add(k);}
for (List list: result.values()) {Collections.sort(list);
}
7/28/2019 Why Java Sucks and Csharp Rocks
65/118
Quite easy, ah?Guess how does C#
code look like?
7/28/2019 Why Java Sucks and Csharp Rocks
66/118
List keywords = ...;var result = keywords
.GroupBy(k => k[0]).ToDictionary(g => g.Key,g => g.OrderBy(k => k).ToList());
7/28/2019 Why Java Sucks and Csharp Rocks
67/118
List keywords = ...;var result = keywords
.GroupBy(k => k[0]).ToDictionary(g => g.Key,g => g.OrderBy(k => k).ToList());
7/28/2019 Why Java Sucks and Csharp Rocks
68/118
Amazing, isnt it?
7/28/2019 Why Java Sucks and Csharp Rocks
69/118
What about the Javacommunity?
7/28/2019 Why Java Sucks and Csharp Rocks
70/118
Well, the Functional Java project is out!
But ...
http://functionaljava.org/http://functionaljava.org/http://functionaljava.org/http://functionaljava.org/7/28/2019 Why Java Sucks and Csharp Rocks
71/118
... do you really want towrite code like this?
http://functionaljava.org/examples/1.5/http://functionaljava.org/examples/1.5/7/28/2019 Why Java Sucks and Csharp Rocks
72/118
keywords.groupBy(
new F {public Character f(String s) { return s.charAt(0); }
}).toMap(
new F {public Character f(Grouping g) {
return g.getKey();}
},new F {
public List f(Grouping g) {return g
.orderBy(new F {
public String f(String s) { return s; }})
.toList();}
});
7/28/2019 Why Java Sucks and Csharp Rocks
73/118
Its really a pain withoutlambda expressions.
7/28/2019 Why Java Sucks and Csharp Rocks
74/118
Fortunately, Java 7 willprovide a sort of lambda syntax.
7/28/2019 Why Java Sucks and Csharp Rocks
75/118
keywords.groupBy({ String k => k.charAt(0) })
.toMap({ Grouping g => g.getKey() },{ Grouping g =>
g.orderBy({ String c => c }).toList() });
7/28/2019 Why Java Sucks and Csharp Rocks
76/118
Much better,but still noisy because
of lacking ...
7/28/2019 Why Java Sucks and Csharp Rocks
77/118
Type Inference
7/28/2019 Why Java Sucks and Csharp Rocks
78/118
C# is an FPL because of
First class functions (as .NET delegates)
Elegant lambda expression syntax Enough type inference for most cases Full set of functional primitives in BCL Great extensibility (by extension method)
7/28/2019 Why Java Sucks and Csharp Rocks
79/118
Fixed-point combinatorstatic Func Fix(
Func f)
{return x => f(Fix(f))(x);
}
var fib = Fix(f => x =>x
7/28/2019 Why Java Sucks and Csharp Rocks
80/118
Is it really useful in realworld programming?
7/28/2019 Why Java Sucks and Csharp Rocks
81/118
Yes, of course.I cant live without the
functional features now.
7/28/2019 Why Java Sucks and Csharp Rocks
82/118
Let me prove it to you.
7/28/2019 Why Java Sucks and Csharp Rocks
83/118
Expression Trees
7/28/2019 Why Java Sucks and Csharp Rocks
84/118
Code as Expressions
Serializable code
Consistent coding style Strong typing Compile-time verication Generate dynamic method easily
7/28/2019 Why Java Sucks and Csharp Rocks
85/118
Many projects useExpressions.
Let me show you sth.
7/28/2019 Why Java Sucks and Csharp Rocks
86/118
Java 7 is coming Automatic Resource Management Collection Literals
Improved Type Inference for GenericsInstance Creation
Language support for JSR 292 Simplied Varargs Method Invocation Support for Strings in Switch Statements
7/28/2019 Why Java Sucks and Csharp Rocks
87/118
Most of them areprovided by C# now
7/28/2019 Why Java Sucks and Csharp Rocks
88/118
7/28/2019 Why Java Sucks and Csharp Rocks
89/118
Taste a bit
7/28/2019 Why Java Sucks and Csharp Rocks
90/118
Ruby-like Markup Builder
dynamic b = new XmlMarkupBuilder();var persons = new [] {
new Person("Tom", 10),new Person("Jerry", 8)
};XElement xml =
b.persons(from p in personsselect b.person(p.Name, age: p.Age));
7/28/2019 Why Java Sucks and Csharp Rocks
91/118
Ruby-like Markup Builder
dynamic b = new XmlMarkupBuilder();var persons = new [] {
new Person("Tom", 10),new Person("Jerry", 8)
};XElement xml =
b.persons(from p in personsselect b.person(p.Name, age: p.Age));
Convariance &
7/28/2019 Why Java Sucks and Csharp Rocks
92/118
Convariance &
Contravariance Improving the generics type system More powerful and safer than Javas use-site
variance
Really a brain fucker
7/28/2019 Why Java Sucks and Csharp Rocks
93/118
Summary
(maybe I should put a table here...)
7/28/2019 Why Java Sucks and Csharp Rocks
94/118
C# always make mehappy when coding ...
7/28/2019 Why Java Sucks and Csharp Rocks
95/118
... and it brings us lotsof useful code patterns
7/28/2019 Why Java Sucks and Csharp Rocks
96/118
But Java keeps me awayfrom the great JVM
7/28/2019 Why Java Sucks and Csharp Rocks
97/118
Java is popular since its ...
Fast - designed for the JVM, as C isdesigned for the machine
Stable - its always one and only one way todo things
Easy - most of the features are quite easyto learn
7/28/2019 Why Java Sucks and Csharp Rocks
98/118
But whats wrong with Java?
7/28/2019 Why Java Sucks and Csharp Rocks
99/118
Java is too noisy
The only way to work is not simple Write more and do less Little room for smart people.
7/28/2019 Why Java Sucks and Csharp Rocks
100/118
Java is weird ...
int a = 1000, b = 1000;System.out.println(a == b); // true
Integer c = 1000, d = 1000;System.out.println(c == d); // false
Integer e = 100, f = 100;System.out.println(e == f); // true
d b
7/28/2019 Why Java Sucks and Csharp Rocks
101/118
... and becoming even more
// C#dynamic o = ...;
int i = o.SomeMethod(true, hello);
// JavaObject o = ...;
Object[] args = new Object[] { true, hello };InvokeDynamic.SomeMethod(o, args);
f h
7/28/2019 Why Java Sucks and Csharp Rocks
102/118
If JVM is machine ...
Java byte code is the machine code Java language is the ASM.
7/28/2019 Why Java Sucks and Csharp Rocks
103/118
Would you like toprogram with ASM?
7/28/2019 Why Java Sucks and Csharp Rocks
104/118
What should we do?
7/28/2019 Why Java Sucks and Csharp Rocks
105/118
Lets use C# instead!
7/28/2019 Why Java Sucks and Csharp Rocks
106/118
Just kidding
W C# i
7/28/2019 Why Java Sucks and Csharp Rocks
107/118
We cant use C# since ...
JVM is powerful Java libraries are great Platform migration is a nightmare
7/28/2019 Why Java Sucks and Csharp Rocks
108/118
So choose anotherlanguage!
L JVM
7/28/2019 Why Java Sucks and Csharp Rocks
109/118
Languages on JVM
JRuby
Jython Groovy Clojure Scala
All h l
7/28/2019 Why Java Sucks and Csharp Rocks
110/118
All these languages are
More powerful
Run on JVM Interop with Java seamlessly Mature enough Successful in real world
7/28/2019 Why Java Sucks and Csharp Rocks
111/118
Scala makes me write Java program again.
Wh S l ?
7/28/2019 Why Java Sucks and Csharp Rocks
112/118
Why Scala? a statically-compiled language a pure object-oriented languages clean & powerful generic support (including
convariance & contravariance) enough functional features actor-based programming model reall scalable
7/28/2019 Why Java Sucks and Csharp Rocks
113/118
What do you think?
C t t i
7/28/2019 Why Java Sucks and Csharp Rocks
114/118
Contact me via
Email: [email protected] Twitter: @j effz_cn Blog: http://blog.zhaojie.me/
http://blog.zhaojie.me/http://blog.zhaojie.me/http://blog.zhaojie.me/mailto:[email protected]:[email protected]7/28/2019 Why Java Sucks and Csharp Rocks
115/118
Show me your idea ...
7/28/2019 Why Java Sucks and Csharp Rocks
116/118
... and prove it to me
F t T lk
7/28/2019 Why Java Sucks and Csharp Rocks
117/118
Future Talks
Why F# matters
Why Scala matters (TBD.) Generics in Java, Scala, C# & F# The beauty of Parallel Library Real-world reactive programming
7/28/2019 Why Java Sucks and Csharp Rocks
118/118
Thanks