objective-c和java之间的区别

时间:2021-05-19 21:28:19

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编写应用程序。有哪些根本区别? (语法除外)

5 个解决方案

#1


39  

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.
  • 内存管理更加手动。在iPhone上,你必须使用retain / release(OS X有垃圾收集)。这实际上并不像听起来那么糟糕。如果您遵循规则,并将实例变量包装在getter和setter中,您将发现自己很少需要编写保留或释放。更新:在我写这篇文章后的一段时间,Apple引入了自动引用计数(ARC)。 ARC发现,clang静态分析仪能够发现几乎每一个缺失(或额外)的保留或释放。因此,他们通过让编译器自动保留和释放来扩展原则。除了关于强弱关系的一些简单规则(即对象是否声称拥有另一个对象),您或多或少会忘记内存管理。此外,ARC可在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.
  • 图书馆要小得多。特别是,您会注意到只有三个集合类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类,这是我真正希望他们添加的功能。

#2


29  

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.

转型并不算太糟糕。两种语言在对象模型甚至一些语法方面都遵循类似的规则。我实际上做了相反的过渡。我开始使用Objective-C for iPhone,然后选择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对象连接到代码中的声明非常简单。仪器提供了一种检查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并不会太难。只需准备好你的[]键并练习输入“发布”!

#3


4  

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

谷歌Java与目标c这是一个看起来相当不错的...看看这个链接http://www.peachpit.com/articles/article.aspx?p=377302

#4


2  

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.

除此之外,许多事情都是相似的:单继承,后期绑定等。目标C不提供方法重载,但这有点微不足道。 Java和Objective-C在对象模型的工作方式上并没有太大差别。 OBJ。 C有一些技巧,比如类别,但你最初不需要担心这些。

See the related C# question suggested 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提醒我库的差异 - 我几乎忘记了这个重要的方面)。

#5


0  

Any object declared in Objective C is a pointer of another

在Objective C中声明的任何对象都是另一个对象的指针

#1


39  

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.
  • 内存管理更加手动。在iPhone上,你必须使用retain / release(OS X有垃圾收集)。这实际上并不像听起来那么糟糕。如果您遵循规则,并将实例变量包装在getter和setter中,您将发现自己很少需要编写保留或释放。更新:在我写这篇文章后的一段时间,Apple引入了自动引用计数(ARC)。 ARC发现,clang静态分析仪能够发现几乎每一个缺失(或额外)的保留或释放。因此,他们通过让编译器自动保留和释放来扩展原则。除了关于强弱关系的一些简单规则(即对象是否声称拥有另一个对象),您或多或少会忘记内存管理。此外,ARC可在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.
  • 图书馆要小得多。特别是,您会注意到只有三个集合类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类,这是我真正希望他们添加的功能。

#2


29  

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.

转型并不算太糟糕。两种语言在对象模型甚至一些语法方面都遵循类似的规则。我实际上做了相反的过渡。我开始使用Objective-C for iPhone,然后选择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对象连接到代码中的声明非常简单。仪器提供了一种检查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并不会太难。只需准备好你的[]键并练习输入“发布”!

#3


4  

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

谷歌Java与目标c这是一个看起来相当不错的...看看这个链接http://www.peachpit.com/articles/article.aspx?p=377302

#4


2  

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.

除此之外,许多事情都是相似的:单继承,后期绑定等。目标C不提供方法重载,但这有点微不足道。 Java和Objective-C在对象模型的工作方式上并没有太大差别。 OBJ。 C有一些技巧,比如类别,但你最初不需要担心这些。

See the related C# question suggested 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提醒我库的差异 - 我几乎忘记了这个重要的方面)。

#5


0  

Any object declared in Objective C is a pointer of another

在Objective C中声明的任何对象都是另一个对象的指针