Objective-c 和 java 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2982762/
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
Difference between objective-c and java
提问by sheLa
i'm experienced with Java and want to learn objective-c to write apps for the iPhone. What are some fundamental differences? (other than syntax)
我有 Java 经验,想学习 Objective-c 来为 iPhone 编写应用程序。有哪些根本区别?(语法除外)
回答by Romain Hippeau
Google Java vs Objective c
Here is one that looks pretty good ...
Look at this link http://www.peachpit.com/articles/article.aspx?p=377302
Google Java vs Objective c
这是一个看起来不错的...
看这个链接http://www.peachpit.com/articles/article.aspx?p=377302
回答by Michael Ekstrand
The biggest difference that will affect you immediately, besides an entirely different set of libraries[1], is that Objective-C doesn't provide garbage collector. The Apple libraries provide some garbage collection related routines and objects, I believe using reference counting, but you don't have the garbage collection you're used to in Java.
除了一组完全不同的库 [1] 之外,会立即影响您的最大区别是 Objective-C 不提供垃圾收集器。Apple 库提供了一些与垃圾收集相关的例程和对象,我相信使用引用计数,但您没有在 Java 中习惯的垃圾收集。
Other than that, many things will be similar: single inheritance, late binding, etc. Objective C doesn't provide method overloading, but that's a somewhat trivial difference. Java and Objective-C aren't too far apart in terms of how their object model works. Obj. C has a few tricks up its sleeve, such as categories, but you don't need to worry about those at first.
除此之外,许多事情将是相似的:单继承、后期绑定等。Objective C 不提供方法重载,但这是一个有点微不足道的区别。Java 和 Objective-C 在对象模型的工作方式方面相差不远。对象。C 有一些技巧,例如类别,但您一开始不必担心这些。
See the related C# questionsuggested by Remus for more (and much more detailed) information (and thanks to Remus for reminding me of the library difference - I nearly forgot that important facet).
请参阅Remus 建议的相关C# 问题以获取更多(并且更详细)的信息(感谢 Remus 提醒我库差异 - 我几乎忘记了这个重要方面)。
回答by strange quark
First, Objective-C doesn't provide a garbage collector for iPhone. On the Mac, a garbage collector is present.
首先,Objective-C 没有为 iPhone 提供垃圾收集器。在 Mac 上,存在垃圾收集器。
But, Possibly the biggest difference for me is that there are 2 files for each class. A header file (.h) where you have to declare instance variables, properties, and methods. Then is the implementation (.m) file where you write your methods. Properties in Objective-C have to be "synthesized" with the @synthesize keyword to create the getter and setter methods.
但是,对我来说最大的区别可能是每个类有 2 个文件。必须在其中声明实例变量、属性和方法的头文件 (.h)。然后是您编写方法的实现 (.m) 文件。Objective-C 中的属性必须使用 @synthesize 关键字“合成”以创建 getter 和 setter 方法。
The transition isn't too bad. Both languages follow similar rules in terms of object models and even some of the syntax. I actually made the opposite transition. I started with Objective-C for iPhone, then picked up Java to do Android development.
过渡还不错。这两种语言在对象模型甚至一些语法方面都遵循相似的规则。我实际上做了相反的转变。我从 iPhone 的 Objective-C 开始,然后拿起 Java 来做 Android 开发。
On an unrelated note, building your UI is much easier using Apple's tools. Interface builder is drop-dead simple. Hooking up UI objects in the nib files to their declarations in code is so easy. Instruments provides an easy way to check CPU usage, memory leaks, allocations, and so on. Plus, just in terms of features, overall polish, and ease of use, I'll take XCode and Apple's tools to Eclipse any day.
顺便提一下,使用 Apple 的工具构建 UI 会容易得多。界面构建器非常简单。将 nib 文件中的 UI 对象连接到它们在代码中的声明非常简单。Instruments 提供了一种简单的方法来检查 CPU 使用率、内存泄漏、分配等。另外,就功能、整体润色和易用性而言,我随时都会将 XCode 和 Apple 的工具带到 Eclipse。
If you're "fluent" in Java, the move to Objective-C won't be too hard. Just get your [] keys ready and practice typing "release"!
如果您“流利”使用 Java,那么迁移到 Objective-C 不会太难。只需准备好您的 [] 键并练习键入“release”!
回答by JeremyP
Conceptually, the biggest difference is that Objective-C is dynamically typed and you don't call methods, you send messages. This means that the Objective-C runtime does not care what type your object is, only whether it will respond to the messages you send it. This in turn means that you could (for example) create a class with an objectForIndex: method and use it in place of an NSArray as long as the code that uses it only calls objectForIndex:
从概念上讲,最大的区别是 Objective-C 是动态类型的,您不调用方法,而是发送消息。这意味着 Objective-C 运行时并不关心你的对象是什么类型,只关心它是否会响应你发送的消息。这反过来意味着您可以(例如)使用 objectForIndex: 方法创建一个类并使用它代替 NSArray,只要使用它的代码只调用 objectForIndex:
This allows you to do all sorts of funky things, like have one object pose as an object of a different class and you can add methods at run time or add collections of methods (called categories) to prebuilt classes like NSString at compile time. Most of the time you'll never bother with any of those tricks, except the categories.
这允许你做各种时髦的事情,比如让一个对象作为不同类的对象,你可以在运行时添加方法或在编译时向预构建的类(如 NSString)添加方法集合(称为类别)。大多数时候,除了类别之外,您永远不会打扰任何这些技巧。
On a more practical level you'll notice:
在更实际的层面上,您会注意到:
- the syntax is different
- Memory management is more manual. On the iPhone, you have to use retain/release (OS X has garbage collection). This is not actually as bad as it sounds. If you follow the rules, and wrap your instance variables in getters and setters you'll find yourself rarely having to write retain or release. Update:some time after I wrote this, Apple introduced automatic reference counting (ARC). ARC grew out of the observation that the clang static analyser was capable of spotting just about every single missing (or extra) retain or release. So they extended the principle by having the compiler put in the retains and releases automatically. Apart from some simple rules about strong and weak relationships (i.e. whether an object claims to own another object or not), you can more or less forget about memory management. Also, ARC is available on iOS.
- All methods are public. This is a direct consequence of the message sending paradigm, but you can't define private or protected methods.
- The library is much smaller. In particular, you will notice that there are only three collection classes NSArray, NSDictionary and NSSet (plus their mutable versions). The philosophy is that you program to the interface. The runtime worries about what the implementation should be.
- 语法不同
- 内存管理更加手动。在 iPhone 上,你必须使用保留/释放(OS X 有垃圾收集)。这实际上并不像听起来那么糟糕。如果您遵循规则,并将您的实例变量包装在 getter 和 setter 中,您会发现自己很少需要编写保留或释放。更新:在我写完这篇文章一段时间后,Apple 引入了自动引用计数 (ARC)。ARC 源于对 clang 静态分析器能够发现几乎所有丢失(或额外)保留或释放的观察结果。因此,他们通过让编译器自动放入保留和释放来扩展该原则。除了一些关于强弱关系的简单规则(即一个对象是否声称拥有另一个对象),您或多或少可以忘记内存管理。此外,ARC 可在 iOS 上使用。
- 所有方法都是公开的。这是消息发送范式的直接结果,但您不能定义私有或受保护的方法。
- 图书馆要小得多。特别是,您会注意到只有三个集合类 NSArray、NSDictionary 和 NSSet(加上它们的可变版本)。其理念是您对界面进行编程。运行时担心实现应该是什么。
ETA: I forgot one important thing, you'll miss from Java. Objective-C does not support name spaces. This is why you'll see OBjective-C classes with two (or more) letter prefixes and it's the feature I really wish they would add.
ETA:我忘记了一件重要的事情,你会错过 Java。Objective-C 不支持命名空间。这就是为什么您会看到带有两个(或更多)字母前缀的 OBjective-C 类,这是我真正希望他们添加的功能。
回答by ThE uSeFuL
Any object declared in Objective C is a pointer of another
在 Objective C 中声明的任何对象都是另一个对象的指针