C++ 有哪些常见的 Java 陷阱/陷阱? 程序员?
正如问题所说,C++ 程序员在转向 Java 时面临哪些常见/主要问题? 我正在寻找一些广泛的主题名称或示例以及工程师必须做出的日常调整。 然后我可以去深入阅读这一点。
我对那些在 C++ 领域工作多年并且必须使用 Java 的工程师的意见特别感兴趣,但其他人的任何指点甚至书籍推荐都非常受欢迎。
As the question says, what are some common/major issues that C++ programmers face when switching to Java? I am looking for some broad topic names or examples and day to day adjustments that engineers had to make. I can then go and do an in-depth reading on this.
I am specifically interested in opinions of engineers who have worked in C++ for years and had to work with Java but any pointers from others or even book recommendations are more than welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我从 C++ 到 Java 的最大障碍是放弃过程代码。 我非常习惯在程序中将所有对象捆绑在一起。 如果没有java中的过程代码,我到处都会进行循环引用。 我必须学习如何从对象调用对象而不使它们相互依赖。 这是最大的障碍,但也是最容易克服的。
第二个个人问题是文档。 JavaDoc 很有用,但许多 Java 项目都错误地认为所需要的只是 JavaDoc。 我在 C++ 项目中看到了更好的文档。 这可能只是个人对代码之外的文档的偏好。
第三,java中实际上有指针,只是没有指针算术。 在java中它们被称为引用。 不要以为你可以忽略事物所指向的地方,它会大吃一惊。
== 和 .equals 不相等。
== 将查看指针(引用)而 .equals 将查看引用指向的值。
My biggest hurdle crossing from C++ to Java was ditching procedural code. I was very used to tying all my objects together within procedures. Without procedural code in java, I made circular references everywhere. I had to learn how to call objects from objects without them being dependents of each other. It was the Biggest hurdle but the easiest to overcome.
Number 2 personal issue is documentation. JavaDoc is useful but to many java projects are under the misconception that all that is needed is the JavaDoc. I saw much better documentation in C++ projects. This may just be a personal preference for documentation outside of the code.
Number 3. There are in fact pointers in java, just no pointer arithmetic. In java they are called references. Don't think that you can ignore where things are pointing at, it will come back with a big bite.
== and .equals are not equal.
== will look at the pointer(reference) while .equals will look at the value that the reference is pointing at.
泛型(而不是模板),具体是这样的它们是使用类型擦除实现的。
Generics (instead of templates), specifically the way they were implemented using type erasure.
既然您提到了书籍推荐,请务必阅读Effective Java,第二版。 - 它解决了我看到的大多数陷阱都在答案中列出。
Since you mention book recommendations, definitely read Effective Java, 2nd ed.—it addresses most of the pitfalls I've seen listed in the answers.
当人们想到复制构造函数时,意外创建了一个引用:
Creating a reference by accident when one was thinking of a copy constructor:
习惯有垃圾收集器。 无法依靠析构函数来清理 GC 未处理的资源。
一切都是按值传递的,因为传递的是引用而不是对象。
没有复制构造函数,除非您需要克隆。 无赋值运算符。
默认情况下所有方法都是虚拟的,这与 C++ 相反。
对接口的显式语言支持 - C++ 中的纯虚拟类。
Getting used to having a garbage collector. Not being able to rely on a destructor to clean up resources that the GC does not handle.
Everything is passed by value, because references are passed instead of objects.
No copy constructor, unless you have a need to clone. No assignment operator.
All methods are virtual by default, which is the opposite of C++.
Explicit language support for interfaces - pure virtual classes in C++.
这些细微的语法差异让我着迷。 缺乏析构函数。
另一方面,能够为每个类编写一个 main(非常方便或测试)真的很好; 当你习惯了它之后,jar 文件的结构和技巧真的很棒; 事实上,语义是完全定义的(例如, int 在任何地方都是相同的),这真是太好了。
It's all the little bitty syntax differences that got me. Lack of destructors.
On the other hand, being able to write a main for each class (immensely handy or testing) is real nice; after you get used to it, the structure and tricks available with jar files are real nice; the fact that the semantics are completely defined (eg, int is the same everywhere) is real nice.
我最糟糕的问题是始终牢记内存的所有权。 在 C++ 中,这是一件必要的事情,并且它在开发人员的头脑中创建了一些难以克服的模式。 在 Java 中,我可以忘记它(无论如何,在很大程度上),这使得一些在 C++ 中非常尴尬的算法和方法成为可能。
My worst problem was keeping in mind the ownership of memory at all times. In C++, it's a necessary thing to do, and it creates some patterns in developer's mind that are hard to overcome. In Java, I can forget about it (to a very high degree, anyway), and this enables some algorithms and approaches that would be exceedingly awkward in C++.
Java中没有对象,只有对象的引用。 例如:
但是:
There are no objects in Java, there are only references to objects. E.g :
But :
我读过的关于 Java“陷阱”的最好的书是 Java Puzzlers: Traps, Pitfalls和极端情况。 它不是专门针对 C++ 开发人员,但其中充满了您需要注意的示例。
The best book of Java "gotchas" that I've read is Java Puzzlers: Traps, Pitfalls, and Corner Cases. It's not specifically aimed at C++ developers, but it is full of examples of things you want to look out for.
将方法参数指定为final并不意味着你一开始认为它意味着什么,
因为java是按值传递的,它正在做你所要求的事情,只是不是很明显,除非你理解java如何传递引用的值而不是目的。
Specifying a method parameter as final doesn't mean what you at first think it means
because java is pass by value it is doing what you're asking, just not immediately obvious unless you understand how java passes the value of the reference rather than the object.
另一个值得注意的是关键字
final
和const
。 Java 将 const 定义为保留关键字,但没有详细说明其用法。 也不复制它更改引用的对象
Another notable one is the keyword
final
andconst
. Java defines the const as a reserved keyword but doesn't specify much of its usage. Alsodoesn't copy the objects it changes the reference
所有方法都是虚拟的。
参数化类型(泛型)实际上并不创建特定于参数的代码(即
List
使用与List
Varargs 很容易。
All methods are virtual.
Parameterized types (generics) don't actually create code parameter-specific code (ie,
List<String>
uses the same bytecode asList<Object>;
the compiler is the only thing that complains if you try to put anInteger
in the former).Varargs is easy.
相反,请使用这种模式:
您最终会经常做类似的事情。
Instead use this pattern:
You'll end up doing stuff like that a lot.