我只想澄清一件事。这不是哪个更好的问题,这部分我留给别人讨论。我不在乎。
我在面试时被问到过这个问题,我想多了解一点可能会有用。
以下是我能想到的:
Java is "platform independent". Well nowadays you could say there is the Mono project so C# could be considered too but
I believe it is a bit exaggerating. Why? Well, when a new release of Java is done it is simultaneously available on all platforms it supports, on the other hand how many features of C# 3.0 are still missing in the Mono implementation? Or is it really CLR vs. JRE that we should compare here?
Java doesn't support events and delegates. As far as I know.
In Java all methods are virtual
Development tools: I believe there isn't such a tool yet as Visual Studio. Especially if you've worked with team editions you'll know what I mean.
请补充其他你认为相关的。
更新:
我突然想到,Java在类、方法等上没有自定义属性。真的是这样吗?
c#在Java中不存在的特性
c#包含了更多的基本类型和捕获算术异常的功能。
•在Java上提供了大量方便的表示法,其中许多,例如操作符重载和用户定义的强制类型转换,对于大型c++程序员社区来说已经很熟悉了。
事件处理是“一等公民”——它是语言本身的一部分。
•允许定义“struct”,它类似于类,但可以在堆栈上分配(不像c#和Java中的类实例)。
c#实现属性作为语言语法的一部分。
c#允许switch语句操作字符串。
c#允许匿名方法提供闭包功能。
c#允许迭代器通过函数风格的yield关键字使用协同例程。
c#支持输出参数,帮助返回多个值,这是c++和SQL共享的特性。
c#具有别名命名空间的能力。
c#有“显式成员实现”,它允许一个类具体实现接口的方法,与它自己的类方法分开。这也允许它实现两个恰好具有相同名称的方法的不同接口。接口的方法不需要是公共的;它们只能通过该接口访问。
c#提供了与COM的集成。
•遵循C和c++的例子,c#允许通过引用调用原语和引用类型。
Java的特性在c#中不存在
Java的strictfp关键字保证了浮点运算的结果在不同平台上保持相同。
Java支持检查异常,以更好地执行错误捕获和处理。
比较Java 7和c# 3
(这里没有提到Java 7的一些特性,但是所有版本的c#相对于Java 1-6使用语句的优势已经被删除了。)
并非所有的总结都是正确的:
在Java中,方法默认是虚拟的,但你可以将它们设为final。(在c#中,它们默认是密封的,但你可以将它们设为虚拟的。)
Java有很多ide,有免费的(如Eclipse、Netbeans),也有商业的(如IntelliJ IDEA)。
除此之外(以及你的总结中已经提到的内容):
Generics are completely different between the two; Java generics are just a compile-time "trick" (but a useful one at that). In C# and .NET generics are maintained at execution time too, and work for value types as well as reference types, keeping the appropriate efficiency (e.g. a List<byte> as a byte[] backing it, rather than an array of boxed bytes.)
C# doesn't have checked exceptions
Java doesn't allow the creation of user-defined value types
Java doesn't have operator and conversion overloading
Java doesn't have iterator blocks for simple implemetation of iterators
Java doesn't have anything like LINQ
Partly due to not having delegates, Java doesn't have anything quite like anonymous methods and lambda expressions. Anonymous inner classes usually fill these roles, but clunkily.
Java doesn't have expression trees
C# doesn't have anonymous inner classes
C# doesn't have Java's inner classes at all, in fact - all nested classes in C# are like Java's static nested classes
Java doesn't have static classes (which don't have any instance constructors, and can't be used for variables, parameters etc)
Java doesn't have any equivalent to the C# 3.0 anonymous types
Java doesn't have implicitly typed local variables
Java doesn't have extension methods
Java doesn't have object and collection initializer expressions
The access modifiers are somewhat different - in Java there's (currently) no direct equivalent of an assembly, so no idea of "internal" visibility; in C# there's no equivalent to the "default" visibility in Java which takes account of namespace (and inheritance)
The order of initialization in Java and C# is subtly different (C# executes variable initializers before the chained call to the base type's constructor)
Java doesn't have properties as part of the language; they're a convention of get/set/is methods
Java doesn't have the equivalent of "unsafe" code
Interop is easier in C# (and .NET in general) than Java's JNI
Java and C# have somewhat different ideas of enums. Java's are much more object-oriented.
Java has no preprocessor directives (#define, #if etc in C#).
Java has no equivalent of C#'s ref and out for passing parameters by reference
Java has no equivalent of partial types
C# interfaces cannot declare fields
Java has no unsigned integer types
Java has no language support for a decimal type. (java.math.BigDecimal provides something like System.Decimal - with differences - but there's no language support)
Java has no equivalent of nullable value types
Boxing in Java uses predefined (but "normal") reference types with particular operations on them. Boxing in C# and .NET is a more transparent affair, with a reference type being created for boxing by the CLR for any value type.
这不是详尽的,但它涵盖了我能想到的一切。