我已经使用C#和更一般的.NET框架工作了几年。我经常听到C#和Java语言之间的相似之处,希望学习更多关于第二种语言。
- 您有什么具体的建议来学习Java,当从C#转换时?
- 一个C#程序员在开始使用Java时可能犯的任何常见错误?
- 有没有文档展示可以保留的习惯和必须改变的习惯(仍然是从C#到Java的角度,因此需要比C# vs Java更具体的内容)?
嗯,虽然 C# 和 Java 表面上很相似,但有许多小差异可能会让你感到困扰。一般来说,我认为相反的方向——从 Java 转向 C#——不太会有问题。这主要是因为 C# 是一种更复杂的语言,因此你可能会发现许多常见的 Java 模式变得简单了,但反过来可能会有些痛苦。
需要注意的事项(部分列表,不保证详尽):
Different ...
Naming conventions. In Java only type names start with a capital letter (i. e. PascalCase), everything else uses camelCase. Not very hard to adhere to, though.
Also interfaces generally don't start with I
. On the other hand you have to implement them with a different keyword. Doesn't really help in the middle of the code, though.
Class library :-)
While obvious, this has been the thing I spent most time on when learning a language. When dealing with a known paradigm the syntax differences are quickly sorted out, but getting to know the standard library / class library / framework takes some time in some cases :-)
Exception handling. Java has so-called checked exceptions which means that an exception must either be caught or declared upwards. Usually this means that you have
catch (SomeException ex) {
ex.printStackTrace();
}
pretty often in your code1 :-)
int
, float
, char
, &c. and classes such as String
. Doesn't matter much since they implemented auto-boxing, but sometimes it's still annoying to wrap int
in Integer
.virtual
by default whereas c# methods are not.foreach (a in b)
→ for (a : b)
internal
and protected internal
don't exist. But unqualified members are visible to other classes in the same package (sort of internal
, but then again not quite).==
in Java. You have to use .equals()
. While in C# ==
on strings is value equality, in Java ==
is always reference equality.No ...
Foo getFoo()
/void setFoo(Foo foo)
pattern which C# generates silently behind your back when using properties but you have to do it explicitly in Java. Generally, to keep the language itself simpler many things in Java are just conventions. Still, most of the time you're better off adhering to them :-)myList.get(5)
instead of the array-like syntax myList[5]
. Just a mild inconvenience, though.Object
s remain. Also wildcards in generics can be hard to resolve sometimes when the compiler complains about all of the four ?
in your generics having different types. (Though to be fair: That was a case where I would have needed type information at runtime anyway so I reverted back to Object
s).一般建议:找一个有Java经验的朋友,让他浏览你的代码。虽然当你直接问他时他可能不能告诉你应该注意哪些问题,但他可以很好地发现代码中的奇怪之处并通知你。这对我学习Java非常有帮助(尽管我是先学了Java再学C#,所以可能会有所不同)。
1 是的,我知道很多catch块看起来不同,但是,这可能是原型之一,甚至并不罕见。
2 Quaere, JaQue, JaQu, Querydsl
3 不过有lambdaj。感谢指出这一点,Esko。
我认为许多C#开发者学习Java最大的障碍其实是要学习新的集成开发环境(IDE)。Visual Studio非常优秀,当你长时间使用C#进行编码时,你已经习惯了它。但是,当你需要转向使用Eclipse或Netbeans时,你会突然感到迷茫。如何设置断点?即时窗口在哪里?如何创建Windows应用程序等等...我知道这听起来很疯狂,但我告诉你,人们会非常依赖他们的IDE,并且难以适应新的IDE。
这些编程语言本身非常相似,除了一些关键字和 C# 程序员已经习惯的特性(如属性、using
和具有实体化泛型的能力)在 Java 中缺失。
主要问题在于对框架的了解,Java 框架数量有数千个。
主要语言没问题。了解库将是需要花费时间去做的一件事情。如果你正在做网络应用程序,有很多东西需要学习...相当于WCF和ASP.net的技术。 你没有说你所在的领域是桌面、服务器还是Web服务器?
C#和Java之间最大的区别是:在Java中,所有方法都是virtual
。这也是为什么像NUnit这样的工具来自于Java世界的原因。
你需要学习的最重要的事情是如何在Java中使用Greenspun C#的函数式编程特性。例如,你可以期望创建许多只有一个方法的接口来解决Java缺乏lambda函数和委托的问题。
我真诚地推荐Java in a Nutshell。大多数Java/任何其他语言的入门书籍都是为完全新手读者编写的,解释循环概念需要几页,递归则需要一章……你可以在两天内开始使用这本书编写Java程序。当然,理解底层发生了什么以及如何使用所有可用框架需要很长时间。但是一旦掌握了语言本身,即使只使用谷歌资源,也很容易上手。