[英]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: 这些是我能想到的:
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: 并非所有摘要都是正确的:
Beyond that (and what's in your summary already): 除此之外(以及您的摘要中已有的内容):
List<byte>
as a byte[]
backing it, rather than an array of boxed bytes.) List<byte>
作为byte[]
支持它,而不是盒装字节数组。) ref
and out
for passing parameters by reference ref
和out
用于通过引用传递参数 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.