简体   繁体   English

C#和Java有什么主要区别?

[英]What are major differences between C# and Java?

I just want to clarify one thing. 我只想澄清一件事。 This is not a question on which one is better, that part I leave to someone else to discuss. 这不是一个更好的问题,我要留给其他人讨论。 I don't care about it. 我不在乎。 I've been asked this question on my job interview and I thought it might be useful to learn a bit more. 在我的求职面试中,我被问到这个问题,我认为学习更多内容可能会有所帮助。

These are the ones I could come up with: 这些是我能想到的:

  • Java is "platform independent". Java是“独立于平台的”。 Well nowadays you could say there is the Mono project so C# could be considered too but I believe it is a bit exaggerating. 那么现在你可以说有Mono项目,所以C#也可以考虑,但我相信它有点夸张。 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? 那么,当Java的新版本完成时,它同时可以在它支持的所有平台上使用,另一方面,Mono实现中仍然缺少多少C#3.0功能? Or is it really CLR vs. JRE that we should compare here? 或者我们应该在这里比较真正的CLRJRE吗?
  • Java doesn't support events and delegates. Java不支持事件和委托。 As far as I know. 我所知道的。
  • In Java all methods are virtual 在Java中,所有方法都是虚拟的
  • Development tools: I believe there isn't such a tool yet as Visual Studio. 开发工具:我相信还没有像Visual Studio这样的工具。 Especially if you've worked with team editions you'll know what I mean. 特别是如果你使用团队版本,你就会知道我的意思。

Please add others you think are relevant. 请添加您认为相关的其他人。

Update: Just popped up my mind, Java doesn't have something like custom attributes on classes, methods etc. Or does it? 更新:刚刚出现了我的想法,Java没有类似于类,方法等的自定义属性。或者它呢?

Comparing Java 7 and C# 3 比较Java 7和C#3

(Some features of Java 7 aren't mentioned here, but the using statement advantage of all versions of C# over Java 1-6 has been removed.) (这里没有提到Java 7的一些功能,但是已经删除了所有版本的C#而不是Java 1-6的using语句优势。)

Not all of your summary is correct: 并非所有摘要都是正确的:

  • In Java methods are virtual by default but you can make them final. 在Java中,默认情况下方法是虚拟的但您可以将它们设为最终方 (In C# they're sealed by default, but you can make them virtual.) (在C#中,它们默认是密封的,但您可以将它们设置为虚拟。)
  • There are plenty of IDEs for Java, both free (eg Eclipse, Netbeans) and commercial (eg IntelliJ IDEA) Java有很多IDE,包括免费(例如Eclipse,Netbeans)和商业(例如IntelliJ IDEA)

Beyond that (and what's in your summary already): 除此之外(以及您的摘要中已有的内容):

  • Generics are completely different between the two; 仿制药在两者之间完全不同; Java generics are just a compile-time "trick" (but a useful one at that). Java泛型只是一个编译时的“技巧”(但是很有用)。 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 (eg a List<byte> as a byte[] backing it, rather than an array of boxed bytes.) 在C#和.NET中,泛型也在执行时维护,并且适用于值类型和引用类型,保持适当的效率(例如, List<byte>作为byte[]支持它,而不是盒装字节数组。)
  • C# doesn't have checked exceptions C#没有检查异常
  • Java doesn't allow the creation of user-defined value types Java不允许创建用户定义的值类型
  • Java doesn't have operator and conversion overloading Java没有运算符和转换重载
  • Java doesn't have iterator blocks for simple implemetation of iterators Java没有迭代器块来简单实现迭代器
  • Java doesn't have anything like LINQ Java没有像LINQ那样的东西
  • Partly due to not having delegates, Java doesn't have anything quite like anonymous methods and lambda expressions. 部分由于没有代理,Java没有任何类似匿名方法和lambda表达式的东西。 Anonymous inner classes usually fill these roles, but clunkily. 匿名内部类通常会填充这些角色,但很笨拙。
  • Java doesn't have expression trees Java没有表达式树
  • C# doesn't have anonymous inner classes C#没有匿名内部类
  • C# doesn't have Java's inner classes at all, in fact - all nested classes in C# are like Java's static nested classes 事实上,C#根本没有Java的内部类--C#中的所有嵌套类都像Java的静态嵌套类
  • Java doesn't have static classes (which don't have any instance constructors, and can't be used for variables, parameters etc) Java没有静态类(没有任何实例构造函数,也不能用于变量,参数等)
  • Java doesn't have any equivalent to the C# 3.0 anonymous types Java没有任何与C#3.0匿名类型相同的东西
  • Java doesn't have implicitly typed local variables Java没有隐式类型的局部变量
  • Java doesn't have extension methods Java没有扩展方法
  • Java doesn't have object and collection initializer expressions Java没有对象和集合初始化表达式
  • The access modifiers are somewhat different - in Java there's (currently) no direct equivalent of an assembly, so no idea of "internal" visibility; 访问修饰符有些不同 - 在Java中(当前)没有直接等效的程序集,所以不知道“内部”可见性; in C# there's no equivalent to the "default" visibility in Java which takes account of namespace (and inheritance) 在C#中,没有相当于Java中的“默认”可见性,它考虑了命名空间(和继承)
  • 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和C#中的初始化顺序略有不同(C#在对基类型构造函数的链接调用之前执行变量初始值设定项)
  • Java doesn't have properties as part of the language; Java没有属性作为语言的一部分; they're a convention of get/set/is methods 它们是get / set /是方法的约定
  • Java doesn't have the equivalent of "unsafe" code Java没有相当于“不安全”的代码
  • Interop is easier in C# (and .NET in general) than Java's JNI Interop在C#(和一般的.NET)中比Java的JNI更容易
  • Java and C# have somewhat different ideas of enums. Java和C#对枚举有一些不同的看法。 Java's are much more object-oriented. Java更加面向对象。
  • Java has no preprocessor directives (#define, #if etc in C#). Java没有预处理器指令(C#中的#define,#if等)。
  • Java has no equivalent of C#'s ref and out for passing parameters by reference Java没有相当于C#的refout用于通过引用传递参数
  • Java has no equivalent of partial types Java没有等效的部分类型
  • C# interfaces cannot declare fields C#接口不能声明字段
  • Java has no unsigned integer types Java没有无符号整数类型
  • Java has no language support for a decimal type. Java没有十进制类型的语言支持。 (java.math.BigDecimal provides something like System.Decimal - with differences - but there's no language support) (java.math.BigDecimal提供类似 System.Decimal的东西 - 有差异 - 但是没有语言支持)
  • Java has no equivalent of nullable value types Java没有等价的可空值类型
  • Boxing in Java uses predefined (but "normal") reference types with particular operations on them. Java中的Boxing使用预定义(但“正常”)引用类型,并对它们进行特定操作。 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. C#和.NET中的拳击是一个更透明的事情,CLR为任何值类型创建了用于装箱的引用类型。

This is not exhaustive, but it covers everything I can think of off-hand. 这并非详尽无遗,但它涵盖了我能想到的一切。

The following is a great in depth reference by Dare Obasanjo on the differences between C# and Java. 以下是Dare Obasanjo对C#和Java之间差异的深入参考。 I always find myself referring to this article when switching between the two. 在两者之间切换时,我总是发现自己指的是这篇文章。

http://www.25hoursaday.com/CsharpVsJava.html http://www.25hoursaday.com/CsharpVsJava.html

C#具有非常方便的自动属性,它们还有助于保持代码清洁,至少在getter和setter中没有自定义逻辑时。

Features of C# Absent in Java • C# includes more primitive types and the functionality to catch arithmetic exceptions. Java中缺少C#的特性•C#包含更多原始类型和捕获算术异常的功能。

• Includes a large number of notational conveniences over Java, many of which, such as operator overloading and user-defined casts, are already familiar to the large community of C++ programmers. •包含大量基于Java的符号方便,其中许多(例如运算符重载和用户定义的转换)已经为大型C ++程序员社区所熟悉。

• Event handling is a "first class citizen"—it is part of the language itself. •事件处理是“一等公民” - 这是语言本身的一部分。

• Allows the definition of "structs", which are similar to classes but may be allocated on the stack (unlike instances of classes in C# and Java). •允许定义“结构”,它类似于类,但可以在堆栈上分配(与C#和Java中的类实例不同)。

• C# implements properties as part of the language syntax. •C#将属性作为语言语法的一部分。

• C# allows switch statements to operate on strings. •C#允许switch语句对字符串进行操作。

• C# allows anonymous methods providing closure functionality. •C#允许匿名方法提供闭包功能。

• C# allows iterator that employs co-routines via a functional-style yield keyword. •C#允许通过函数式yield关键字使用协同例程的迭代器。

• C# has support for output parameters, aiding in the return of multiple values, a feature shared by C++ and SQL. •C#支持输出参数,有助于返回多个值,C ++和SQL共享的功能。

• C# has the ability to alias namespaces. •C#具有别名空间的功能。

• C# has "Explicit Member Implementation" which allows a class to specifically implement methods of an interface, separate from its own class methods. •C#具有“显式成员实现”,它允许类专门实现接口的方法,与其自己的类方法分开。 This allows it also to implement two different interfaces which happen to have a method of the same name. 这允许它还实现两个碰巧具有相同名称的方法的不同接口。 The methods of an interface do not need to be public; 接口的方法不需要公开; they can be made to be accessible only via that interface. 它们只能通过该界面访问。

• C# provides integration with COM. •C#提供与COM的集成。

• Following the example of C and C++, C# allows call by reference for primitive and reference types. •遵循C和C ++的示例,C#允许通过引用调用原始类型和引用类型。

Features of Java Absent in C# C#中缺少Java的功能

• Java's strictfp keyword guarantees that the result of floating point operations remain the same across platforms. •Java的strictfp关键字保证浮点运算的结果在不同平台上保持不变。

• Java supports checked exceptions for better enforcement of error trapping and handling. •Java支持已检查的异常,以便更好地实施错误捕获和处理。

Another good resource is http://www.javacamp.org/javavscsharp/ This site enumerates many examples that ilustrate almost all the differences between these two programming languages. 另一个很好的资源是http://www.javacamp.org/javavscsharp/这个网站列举了许多例子,这些例子说明了这两种编程语言之间几乎所有的差异。

About the Attributes, Java has Annotations, that work almost the same way. 关于属性,Java具有注释,其工作方式几乎相同。

Generics: 泛型:

With Java generics, you don't actually get any of the execution efficiency that you get with .NET because when you compile a generic class in Java, the compiler takes away the type parameter and substitutes Object everywhere. 使用Java泛型,实际上并没有获得.NET所带来的任何执行效率,因为当您在Java中编译泛型类时,编译器会删除type参数并在任何地方替换Object。 For instance if you have a Foo<T> class the java compiler generates Byte Code as if it was Foo<Object> . 例如,如果你有一个Foo<T>类,java编译器会生成字节代码,就像它是Foo<Object> This means casting and also boxing/unboxing will have to be done in the "background". 这意味着必须在“背景”中进行转换以及装箱/拆箱。

I've been playing with Java/C# for a while now and, in my opinion, the major difference at the language level are, as you pointed, delegates. 我一直在玩Java / C#一段时间了,在我看来,语言层面的主要区别在于,如你所指出的那样,代表们。

请查看下面给出的链接msdn.microsoft.com/en-us/library/ms836794.aspx它涵盖了C#和java之间的相似之处和区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM