从C#去到Ja​​va [英] Going from C# to Java

查看:113
本文介绍了从C#去到Ja​​va的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在工作与C#和更普遍的.NET Framework以几年。我经常听到关于C#与放大器之间的相似性; Java语言和想了解更多关于第二个。

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#来时,你有没有得到任何具体的建议学习Java?

  • 任何常见的错误启动Java?

  • 当C#程序员会做
  • 显示你可以保持习惯的人的任何文档,你必须改变(仍在C#与Java视神经,所以一些更具体的再一个C#与Java的比较)?

推荐答案

好了,而C#和Java一样表面也有一些可能咬你小的差异。一般来说,我觉得相反的方向持续从Java到C#-is问题较少。这主要是由于C#是一个更复杂的语言,所以你可能会从常见的Java模式找到很多简化,但周围的其他方法可能会有点痛苦。

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.

旅游看出(部分名单,但不保证面面俱到):

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


  • 不同的... ...

  • Different ...


  • 命名约定。在Java中只键入名称以大写字母(即PascalCase),其他的一切使用驼峰。不是很难坚持,虽然。

  • 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.

另外接口一般不与启动I 。在另一方面,你有一个不同的关键字来实现它们。并不能真正帮助在代码的中间,虽然

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.

类库: - )

虽然明显,这一直是学习一门语言时,我花时间最多的东西。当与一个已知的范式语法的差异正在迅速整理出来处理,但逐渐了解标准库/类库/框架需要在某些情况下一些时间: - )

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

异常处理。 Java有所谓的 checked异常的,这意味着一个例外必须要么被捕获或向上申报。通常,这意味着你有

Exception handling. Java has so-called checked exceptions which means that an exception must either be caught or declared upwards. Usually this means that you have

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



:-)相当经常在你的代码 1


  • 的foreach(A在B)→ 为(A:B)

  • 不同的访问关键字。像内部受保护的内部事情不存在。但不合格的成员是其它类可见在同一封装(排序内部,但随后又不太)。

  • 字符串比较不与 == 在Java中完成的。你必须使用 .equals()。而在C# == 对字符串值是平等的,在Java中 == 总是的参考平等。

  • foreach (a in b)for (a : b)
  • Different access keywords. Things like internal and protected internal don'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 always reference equality.

没有...


  • 属性。在Java中,这通常与完成的美孚的getFoo() / 无效setFoo(富富)其中C#生成默默模式你背后使用属性的时候,但你必须明确地做到这一点在Java中。一般情况下,保持语言本身在Java中简单很多东西都只是约定。尽管如此,大部分的时间你最好坚持他们: - )

  • 运算符重载。视为危害他们不是因为怕滥用实现正义的程序员。不需要他们过于频繁,无论如何,甚至没有在C#中,但有时他们很不错,那么你就失去了一些东西。

  • 索引器。你总是必须通过 myList.get(5)而非阵列的语法 myList中[5] 。只是一个温和的不便,虽然。

  • LINQ(尽管存在实施 2 ,但它不是完美的整合在一起),或者lambda函数 3 (无代表反正,但匿名类),扩展方法,或部分类(是的,这是一个痛苦的与Swing打交道时,除非你的非常的纪律),和一些更多的东西。

  • 多维数组。您可以使用交错数组(数组的数组),buttrue多面性是不存在的。只有

  • 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 implementations2 but 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 very disciplined), and a few more things.
  • Multidimensional arrays. You can use jagged arrays (arrays of arrays), buttrue multidimensionality isn't there.

一般建议:的抓斗与Java经验的朋友,让他浏览一下你的代码。虽然他也许不能告诉你一切,你应该照顾,当你直接问过他这个问题,他可以当场代码奇怪的事情就好了,并通知您的。这极大地帮助我学习Java(虽然我第一次学习Java和那么C#,所以它可能是不同的)。

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).

1 是的,我知道很多catch块看起来不同,不过,这可能是原型之一的甚至不是罕见结果
2 Quaere 哈克,的JaQu Querydsl <​​/A>结果
3
lambdaj ,虽然。感谢您指出了这一点,艾司科

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

这篇关于从C#去到Ja​​va的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆