从 C# 到 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2109807/
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-10-29 19:30:46  来源:igfitidea点击:

Going from C# to Java

c#java

提问by Jla

I've been working with C# and more generally the .Net framework for a couple of years now. I often heard about the similarity between C# & the Java language and would like to learn more about the second one.

几年来,我一直在使用 C# 和更一般的 .Net 框架。我经常听说 C# 和 Java 语言之间的相似性,并且想了解更多关于第二个语言的信息。

  • Have you got any specific advice to learn Java when coming from C# ?
  • Any common errors a C# programmer would do when starting Java ?
  • Any documentation showing the habits you can keep and the ones you must change (still in a C# to Java optic, so something a bit more specific then a C# vs Java comparison) ?
  • 从 C# 开始时,您对学习 Java 有什么具体建议吗?
  • C# 程序员在启动 Java 时会犯的任何常见错误?
  • 任何显示您可以保持的习惯和必须改变的习惯的文档(仍然在 C# 到 Java 的光学中,所以比 C# 与 Java 的比较更具体一些)?

回答by Joey

Well, while C# and Java are superficially alike there are a number of small differences that might bite you. Generally I think the opposite direction—going from Java to C#—is less problematic. This is mainly due to C# being a more complex language so you might find many simplifications from common Java patterns but the other way around might be a little painful.

好吧,虽然 C# 和 Java 从表面上看很相似,但仍有许多细微的差异可能会让您大吃一惊。一般来说,我认为相反的方向——从 Java 到 C#——问题较少。这主要是因为 C# 是一种更复杂的语言,因此您可能会从常见的 Java 模式中找到许多简化,但反过来可能会有点痛苦。

Things to look out for (partial list, not guaranteed to be exhaustive):

需要注意的事项(部分清单,不保证详尽无遗):

  • 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 :-)

    • Patterns. Well, not quite, it's still the same stuff. But C# supports some patterns at the language level, while you still have to implement them yourself in Java. No events, but the Observer pattern (very prevalent in Swing—whenever you see a Listener, you know what to do :-))
    • Exception handling. Java has so-called checked exceptionswhich 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:-)

    • Types. While .NET has normal objects and value types, they both are objects and support methods, properties, &c. Java has a dichotomy of primitivetypes, such as int, float, char, &c. and classessuch as String. Doesn't matter much since they implemented auto-boxing, but sometimes it's still annoying to wrap intin Integer.
    • Polymorphism: All Java methods are virtualby default whereas c# methods are not.
  • Minor syntactic differences.
    • foreach (a in b)for (a : b)
    • Different access keywords. Things like internaland protected internaldon't exist. But unqualified members are visible to other classes in the same package (sort of internal, but then again not quite).
    • String comparison isn't done with ==in Java. You have to use .equals(). While in C# ==on strings is value equality, in Java ==is alwaysreference equality.
  • No ...

    • Properties. In Java this is generally done with the 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 :-)
    • Operator overloading. Deemed a hazard to the righteous programmer they weren't implemented for fear of abuse. Don't need them too often anyway, not even in C#, but sometimes they are nice and then you're missing something.
    • Indexers. You always have to access list items through myList.get(5)instead of the array-like syntax myList[5]. Just a mild inconvenience, though.
    • LINQ (though there exist implementations2but it's not as nicely integrated), or lambda functions3(no delegates anyway, but anonymous classes), extension methods, or partial classes (yes, that's a painful one when dealing with Swing, unless you're verydisciplined), and a few more things.
    • Multidimensional arrays. You can use jagged arrays (arrays of arrays), buttrue multidimensionality isn't there.
  • Generics are compile-time only, at runtime only Objects 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 Objects).
  • 不同的 ...

    • 命名约定。在 Java 中,只有类型名称以大写字母开头(即 PascalCase),其他所有类型都使用驼峰命名。不过,不是很难坚持。

      此外,接口通常不以I. 另一方面,您必须使用不同的关键字来实现它们。但是,在代码中间并没有真正帮助。

    • 类库:-)

      虽然显而易见,但这是我在学习语言时花费最多时间的事情。在处理已知范式时,语法差异会很快得到解决,但在某些情况下,了解标准库/类库/框架需要一些时间:-)

    • 图案。嗯,不完全是,它仍然是相同的东西。但是C#在语言层面支持了一些模式,而你仍然需要自己在Java中实现它们。没有事件,但是观察者模式(在 Swing 中非常普遍——每当你看到一个监听器,你就知道该怎么做 :-))
    • 异常处理。Java 有所谓的检查异常,这意味着异常必须被捕获或向上声明。通常这意味着你有

      catch (SomeException ex) {
        ex.printStackTrace();
      }
      

      经常在您的代码中1:-)

    • 类型。虽然 .NET 具有普通对象和值类型,但它们都是对象并支持方法、属性等。Java 具有原始类型的二分法,例如int, float, char, &c。和String。因为他们实现了自动装箱,所以没什么关系,但有时包裹intInteger.
    • 多态性:virtual默认情况下所有 Java 方法都是默认的,而 c# 方法不是。
  • 细微的语法差异。
    • foreach (a in b)for (a : b)
    • 不同的访问关键字。像internalprotected internal不存在的东西。但是不合格的成员对同一包中的其他类是可见的(有点internal,但又不完全是)。
    • ==Java 中没有进行字符串比较。你必须使用.equals(). 虽然在 C#==中字符串是值相等,但在 Java==总是引用相等。
  • 不 ...

    • 特性。在 Java 中,这通常是使用Foo getFoo()/void setFoo(Foo foo)模式完成的,当使用属性时,C# 会在背后默默生成该模式,但您必须在 Java 中显式执行此操作。通常,为了使语言本身更简单,Java 中的许多事情只是约定。尽管如此,大多数时候你最好坚持他们:-)
    • 运算符重载。认为对正直的程序员有危害,因为害怕滥用,他们没有实施。无论如何不要太频繁地需要它们,即使在 C# 中也不需要,但有时它们很好,然后你会错过一些东西。
    • 索引器。您总是必须通过myList.get(5)而不是类似数组的语法来访问列表项myList[5]。不过,这只是一个轻微的不便。
    • LINQ(虽然存在实现2但它没有很好地集成),或 lambda 函数3(无论如何都没有委托,但是匿名类)、扩展方法或部分类(是的,这在处理 Swing 时很痛苦,除非你再次非常有纪律),以及一些更多的东西。
    • 多维数组。您可以使用锯齿状数组(数组数组),但不存在真正的多维性。
  • 泛型仅在编译时可用,在运行时只剩下Objects。当编译器抱怨?泛型中的所有四个具有不同类型时,泛型中的通配符有时也很难解决。(但公平地说:在这种情况下,无论如何我都会在运行时需要类型信息,所以我又回到了Objects)。

General advice:Grab a friend with Java experience and let him glance over your code. While he probably can't tell you everything you should take care of when you directly ask him that question, he can spot strange things in code just fine and notify you of that. This has greatly helped me learning Java (although I learned Java first and then C#, so it might be different).

一般建议:找一个有 Java 经验的朋友,让他看看你的代码。当你直接问他这个问题时,他可能无法告诉你你应该注意的一切,但他可以很好地发现代码中的奇怪事物并通知你。这对我学习Java有很大帮助(虽然我是先学Java然后C#,所以可能会有所不同)。



1Yes, I know many catch blocks look different, but still, this is probably the archetypical one and not even that rare.
2Quaere, JaQue, JaQu, Querydsl
3There's lambdaj, though. Thanks for pointing that out, Esko.

1是的,我知道许多 catch 块看起来不同,但是,这可能是典型的块,甚至并不罕见
2 QuaereJaQueJaQuQuerydsl
3 不过lambdaj。感谢您指出这一点,Esko

回答by BFree

I honestly think the biggest hurdle for many C# developers trying to learn Java is learning a new IDE. Visual Studio is great, and when you're coding in C# for a long period of time, you get used to it. When having to move over to Eclipse or Netbeans, you suddenly feel lost. How do I set a breakpoint? Where's the immediate window? How do I create a windows app? etc etc... I know this sounds crazy, but I'm telling you, people get very attached to their IDE's and have a tough time getting used to new ones...

老实说,我认为许多尝试学习 Java 的 C# 开发人员的最大障碍是学习新的 IDE。Visual Studio 很棒,当您长时间使用 C# 进行编码时,您就会习惯它。当不得不转移到 Eclipse 或 Netbeans 时,您会突然感到迷茫。如何设置断点?直接窗口在哪里?如何创建 Windows 应用程序?等等等等......我知道这听起来很疯狂,但我告诉你,人们非常依赖他们的IDE并且很难适应新的......

回答by Anton Gogolev

Languages themselves are pretty similar, sans few keywords and Java lacking some features C# programmers got used to (properties, using, reified (non-type-erased) generics).

语言本身非常相似,没有几个关键字,而且 Java 缺乏 C# 程序员习惯的一些功能(属性、using、具体化(非类型擦除)泛型)。

The main problem here is knowledge of frameworks, of which there are thousands for Java.

这里的主要问题是框架的知识,其中有数千个用于 Java。

回答by Mr. Boy

The main language is fine. Getting to know the libraries will be one thing which takes time. If you're doing web-applications, there is a LOT to learn... equivalent technologies to WCF and ASP.net. You don't say what kind of area you work in... desktop, server, or web-server?

主要语言没问题。了解图书馆将是一件需要时间的事情。如果你在做 web 应用程序,有很多东西需要学习...... WCF 和 ASP.net 的等效技术。你没有说你在什么样的领域工作......桌面、服务器还是网络服务器?

回答by Florian Doyon

Biggest difference between C# and Java : In Java, all methods are virtual. Hence the reason why tools such as NUnit and such came from the Java world.

C# 和 Java 的最大区别:在 Java 中,所有方法都是virtual. 这就是为什么像 NUnit 之类的工具来自 Java 世界的原因。

回答by Kezzer

To be honest, if you're a competent C# programmer I don't believe there's much you do need to know apart from packaging and deployment of applications.

老实说,如果您是一名称职的 C# 程序员,我认为除了应用程序的打包和部署之外,您不需要了解太多东西。

Here's a good link http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp

这是一个很好的链接http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp

回答by sibidiba

I honestly recommend Java in a Nutshell. Most Java/any_other_lang introduction books are for totally novice readers explaining the concept of a loop for pages and recursion for a chapter... You can start writing Java programs within two days with this book. Of course it will take you a long time to understand what is going on under the hood and how to use all the available framework. But once the language itself is mastered, it is easy to get along even with google only resources.

老实说,我推荐Java in a Nutshell。大多数 Java/any_other_lang 介绍书都是为新手读者准备的,它们解释了页面循环和递归的概念……阅读本书,您可以在两天内开始编写 Java 程序。当然,您需要很长时间才能了解幕后发生的事情以及如何使用所有可用的框架。但是一旦掌握了语言本身,即使只有google的资源也很容易相处。

回答by Koshin

I made a transition from Java to C# and back to Java again. I think syntactically they are very similar and most of the trouble I had was learning the .NET APIs and learning how to use them effectively. Many times I was using 'syntactic sugar', writing my code as if it was in Java and then translating it to C#. I spent a lot of time on Microsoft's website reading and learning about the APIs which was a huge help.

我从 Java 过渡到 C#,然后又回到 Java。我认为它们在语法上非常相似,我遇到的大部分麻烦是学习 .NET API 并学习如何有效地使用它们。很多时候我都在使用“语法糖”,就像用 Java 编写代码,然后将其翻译成 C#。我花了很多时间在 Microsoft 的网站上阅读和学习 API,这对我有很大帮助。

回答by sean

Although, this is the other way around, I found the following link to be quite useful for comparing Java and C#.

虽然这是相反的方式,但我发现以下链接对于比较 Java 和 C# 非常有用。

C# From a Java Developer's Perspective

从 Java 开发人员的角度看 C#

回答by dsimcha

The biggest thing you need to learn is how to Greenspun C#'s functional style features in Java. For example, you can expect to make a lot of interfaces with only one method to get around Java's lack of lambda functions and delegates.

您需要学习的最重要的事情是如何在 Java 中使用 Greenspun C# 的函数式风格特性。例如,您可以期望使用一种方法来制作大量接口来解决 Java 缺少 lambda 函数和委托的问题。