C# 和 Java 之间的主要区别是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/295224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:01:56  来源:igfitidea点击:

What are major differences between C# and Java?

c#.netclrjava

提问by Enes

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". Well nowadays you could say there is the Monoproject 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 CLRvs. JREthat 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 是“平台独立的”。现在你可以说有Mono项目,所以也可以考虑 C#,但我认为这有点夸张。为什么?好吧,当 Java 的新版本完成时,它可以同时在它支持的所有平台上使用,另一方面,Mono 实现中还缺少 C# 3.0 的多少特性?或者我们真的应该在这里比较CLRJRE
  • Java 不支持事件和委托。据我所知。
  • 在 Java 中,所有方法都是虚拟的
  • 开发工具:我相信目前还没有像 Visual Studio 这样的工具。特别是如果您使用过团队版本,您就会明白我的意思。

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 没有类、方法等的自定义属性。或者是吗?

采纳答案by Jon Skeet

Comparing Java 7 and C# 3

比较 Java 7 和 C# 3

(Some features of Java 7 aren't mentioned here, but the usingstatement advantage of all versions of C# over Java 1-6 has been removed.)

(这里没有提到 Java 7 的一些特性,但是usingC# 的所有版本相对于 Java 1-6的声明优势已被删除。)

Not all of your summary is correct:

并非所有的总结都是正确的:

  • In Java methods are virtual by defaultbut you can make them final. (In C# they're sealed by default, but you can make them virtual.)
  • There are plenty of IDEs for Java, both free (e.g. Eclipse, Netbeans) and commercial (e.g. IntelliJ IDEA)
  • 在 Java 中,默认方法是虚拟的但您可以使它们成为最终的。(在 C# 中,默认情况下它们是密封的,但您可以将它们设为虚拟。)
  • 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). 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 anyinstance 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 refand outfor 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 languagesupport for a decimal type. (java.math.BigDecimal provides something likeSystem.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.
  • 两者之间的泛型完全不同;Java泛型只是一个编译时的“技巧”(但在这方面很有用)。在 C# 和 .NET 中,泛型也在执行时维护,并且适用于值类型和引用类型,保持适当的效率(例如,List<byte>作为byte[]支持它,而不是装箱字节数组。)
  • C# 没有检查异常
  • Java 不允许创建用户定义的值类型
  • Java 没有运算符和转换重载
  • Java 没有用于简单实现迭代器的迭代器块
  • Java 没有 LINQ 之类的东西
  • 部分由于没有委托,Java 没有任何类似于匿名方法和 lambda 表达式的东西。匿名内部类通常扮演这些角色,但很笨拙。
  • Java 没有表达式树
  • C# 没有匿名内部类
  • C# 根本没有 Java 的内部类,事实上 - C# 中的所有嵌套类都类似于 Java 的静态嵌套类
  • Java 没有静态类(没有任何实例构造函数,不能用于变量、参数等)
  • Java 没有任何等效于 C# 3.0 匿名类型的内容
  • Java 没有隐式类型的局部变量
  • Java 没有扩展方法
  • Java 没有对象和集合初始值设定项表达式
  • 访问修饰符有些不同——在 Java 中(目前)没有直接等效的程序集,因此不知道“内部”可见性;在 C# 中,没有等同于 Java 中的“默认”可见性,它考虑了命名空间(和继承)
  • Java 和 C# 中的初始化顺序略有不同(C# 在链式调用基类型的构造函数之前执行变量初始化器)
  • Java 没有属性作为语言的一部分;它们是 get/set/is 方法的约定
  • Java 没有等同于“不安全”的代码
  • C#(和一般的 .NET)中的互操作比 Java 的 JNI 更容易
  • Java 和 C# 对枚举的想法有些不同。Java 更面向对象。
  • Java 没有预处理器指令(C# 中的#define、#if 等)。
  • Java 没有等效的 C#refout通过引用传递参数
  • Java 没有等价的部分类型
  • C# 接口不能声明字段
  • Java 没有无符号整数类型
  • Java 没有对十进制类型的语言支持。(java.math.BigDecimal 提供类似System.Decimal 的东西- 有差异 - 但没有语言支持)
  • Java 没有可空值类型的等价物
  • Java 中的装箱使用预定义(但“正常”)的引用类型,并对它们进行特定操作。C# 和 .NET 中的装箱是更透明的事情,CLR 为任何值类型的装箱创建了一个引用类型。

This is not exhaustive, but it covers everything I can think of off-hand.

这不是详尽无遗的,但它涵盖了我能想到的一切。

回答by bruno conde

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. For instance if you have a Foo<T>class the java compiler generates Byte Code as if it was Foo<Object>. This means casting and also boxing/unboxing will have to be done in the "background".

使用 Java 泛型,您实际上无法获得 .NET 的任何执行效率,因为当您在 Java 中编译泛型类时,编译器会去掉类型参数并在任何地方替换 Object。例如,如果您有一个Foo<T>类,java 编译器会像生成Foo<Object>. 这意味着必须在“背景”中进行铸造和装箱/拆箱。

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# 有一段时间了,在我看来,语言级别的主要区别是,正如你所指出的,代表。

回答by Winston Smith

The following is a great in depth reference by Dare Obasanjo on the differences between C# and Java. I always find myself referring to this article when switching between the two.

以下是 Dare Obasanjo 关于 C# 和 Java 之间差异的深入参考。在两者之间切换时,我总是发现自己参考了这篇文章。

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

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

回答by Morten Christiansen

C# has automatic properties which are incredibly convenient and they also help to keep your code cleaner, at least when you don't have custom logic in your getters and setters.

C# 具有非常方便的自动属性,它们还有助于使您的代码更简洁,至少当您的 getter 和 setter 中没有自定义逻辑时。

回答by Rafael Rom?o

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 有注解,它们的工作方式几乎相同。

回答by Abhishek kumar

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. 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# 有“显式成员实现”,它允许一个类专门实现一个接口的方法,与它自己的类方法分开。这也允许它实现两个不同的接口,它们碰巧有一个同名的方法。接口的方法不需要是公共的;它们只能通过该界面访问。

? 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 Absent 的特性

? 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 支持检查异常以更好地执行错误捕获和处理。

回答by Kanwar Singh

Please go through the link given below msdn.microsoft.com/en-us/library/ms836794.aspx It covers both the similarity and difference between C# and java

请通过下面给出的链接 msdn.microsoft.com/en-us/library/ms836794.aspx 它涵盖了 C# 和 java 之间的相似性和差异