Hacker News new | past | comments | ask | show | jobs | submit login
Java Was Strongly Influenced by Objective-C (gmu.edu)
94 points by almakdad on Feb 11, 2010 | hide | past | favorite | 50 comments



Goes to show that just because you model something after another, it doesn't mean they'll look or feel similar. The underlying design of Java might have been influenced by Objective C, but the use of the two languages feel very different.


Interview from http://www.gotw.ca/publications/c_family_interview.htm

(I'm including Stoustrup, since C++ was probably one of the influences of the choice of syntax familiy)

Q: What languages, or features of languages, inspired you?

Stroustrup: In addition to Simula67, my favorite language at the time was Algol68. I think that "Algol68 with Classes" would have been a better language than "C with Classes." However, it would have been stillborn.

Gosling: They're all over the map. Using Lisp, the thing that influenced me the most was the incredible difference garbage collection made. Using Simula and being a local maintainer of the Simula compiler was really what introduced me to objects and got me thinking about objects. Using languages like Pascal got me really thinking about modeling. Languages like Modula-3 really pushed things like exception mechanisms. I've used a lot of languages, and a lot of them have been influential. You can go through everything in Java and say, "this came from there, and this came from there."


Except it seems to be missing all the features that make Objective-C "Objective-C" in my mind. How do you implement method missing/message forwarding in Java? Oh, that's right, your code doesn't even compile.


Too be fair, message forwarding is a pain in the ass in ObjC as well.


It's not really that hard, is it? You just have to implement methodSignatureForSelector and forwardInvocation.

Compare to Java where it's not even possible to forward messages determined at runtime, you have to generate stubs before compiling. AFAIK.


You have to have a method to forward to that accepts the right number of arguments of the right type. It's a far cry from overriding methodNotFound. I tried to recreate the ActiveRecord find style in SimpleData (http://github.com/briancollins/SimpleData) and it is just so hackish.


Or just a method which takes a NSInvocation object...


Interesting revelation, thanks!


Sure you can ... object.sendMessage(messageName) ;) Or for already built classes ... see java.lang.reflect.Method

Personally if I were to choose, I'd trade some syntactic sugar for a decent garbage-collector. But that's just me :)


Message forwarding is not syntactic sugar!


It is. In Objective C: [obj doFoo] would be analogous to this in a Java-like language: MethodSpec s = new MethodSpec("doFoo"); (obj.hasMethod(ms) ? obj.performMethod(ms) : obj.handleMissingMethod(ms)

However, people associate "syntactic sugar" with a connotation of not being a significant difference. Many very useful tools in programming are merely syntactic sugar. For, while, and similar control flow structures are just syntactic sugar over either continuations or gotos.

Message passing OO is syntactic sugar. As evidence, look at the object system the Gtk folks made for themselves in C, or the various object systems Perl programmers have made for themselves.

The example of object systems in Perl illustrates another point about syntactic sugar: having a language which makes it possible for programmers to add their own syntactic sugar within the language is incredibly powerful. Moose is a great system, and Perl's flexible syntax is what makes it possible(although I don't really know Perl well enough to know how it's implemented, its syntax is very pleasant). CLOS is another example of the power of languages which allow programmers to add their own syntactic sugar.

Syntactic sugar can be very good. Message forwarding not only is a very useful piece of syntactic sugar, but also allows the creation of certain types of very convenient syntactic sugar to a language.


Objective C 2.0's garbage collector works just fine tyvm


... but not on the iphone, sadly.


The use of the word "influenced" to describe the effect had on Java's design process by Smalltalk, Objective-C and other languages of good repute is really somewhat misleading.

Certainly, there is some identifiable influence. For example, the concept of a JIT-ing virtual machine comes straight from Smalltalk (Deutsch & Schiffman, 1984), and it would also not surprise me if the APIs of Smalltalk and NeXTSTEP influenced the design of Java's somewhat. But that is largely where the influence ends: on the implementation of the language rather than on the language itself.

Java the language belongs on the same branch of the OO family tree with C++, C# and other strong statically-typed OO languages--not on the branch with Smalltalk and company. Yes, Java's object model is class-based, single-inheritance and single-dispatch, but its model has little else in common with Smalltalk's. If it was designed with Smalltalk in mind, then it constitutes a dangerous misunderstanding of it. I do not mean the fact that "everything" in Java is not an object, but rather that some things that very clearly should be objects, such as classes, are not objects is alone enough to disqualify it as one of Smalltalk's legitimate offspring.

The people who assert over and over again that Java was influenced by this language or that language do so because this language or that language has a better reputation among programmers than Java, and it is hoped that by doing so perhaps some of that reputation might rub-off on the hapless language.


The people who assert over and over again that Java was influenced by this language or that language do so because this language or that language has a better reputation among programmers than Java

The post was written by one of the people who worked on Java, not somebody trying to impress you by associating Java with some language you like better. Are you trying to say Patrick Naughton is just making this up?


As pointed out in my post, the alleged influence simply isn't there, or isn't there enough to justify the claim. Someone who cites Smalltalk as their inspiration before going off and creating a statically-typed OO language with primitive types and classes that aren't real objects either didn't understand Smalltalk or deliberately chose to distort the concepts imbued in it enough that they no longer have a right to claim it as an antecedent.

Gosling and others have been making claims of this sort for years, and yes, I do believe that they do so with the hope of improving their language's reputation, and further, to give their users the impression that they aren't missing out on anything by sticking with Java; that Gosing et al. studied the cream of the language crop when designing Java, took the good parts and left the cruft behind, so why bother trying anything else?


They cited Obj-C, not Smalltalk. Specific people and specific aspects of the design are mentioned, you haven't actually described how it isn't influenced by Obj-C. Which, incidentally, has primitive types, the plain C part can do static type-checking, etc. In any event the claim is the design is influenced by Obj-C not that it is actually just like Obj-C, a distinction you seem to have trouble making.


The claim is that interfaces ripped off prototypes. I'd say that interfaces are a lot closer to C++ abstract classes than prototypes. Whatever they claim, here's my test:

Tell a C++ compiler engineer to make language "C++ minus templates and multiple inheritance plus GC" and you'll probably end up very close to Java.

What do you tell an objective-C compiler engineer to get Java? "Start with obc-c and ..."?

Maybe it's convergent independent evolution, but if obc-c is a branch to the left from C and C++ is a branch to the right, then claiming that Java comes from obj-c means Java must have pulled a very hard right early in its development.


"They cited Obj-C, not Smalltalk."

The author also cites Goslings "lots of experience" with Smalltalk.

"In any event the claim is the design is influenced by Obj-C not that it is actually just like Obj-C, a distinction you seem to have trouble making."

The influence is not sufficient to justify that claim any more than Java's garbage collection would justify its designers in describing Java as "Lispy" or "Lisp-like."


"We were after the C++ programmers. We managed to drag a lot of them about halfway to Lisp." - Guy Steele, co-author of the Java spec


Guy Steele apparently measures distance on a log scale. :)


>very clearly should be objects, such as classes, are not objects

Well classes are objects themselves, but not necessarily related in a useful way to the objects which they instantiate. You can't do as much with them (other then look at them, or load new ones) - or do you mean more a prototype based OO?


See my post below.


> some things that very clearly should be objects, such as classes, are not objects

I don't get it. java.lang.Object.getClass() always returns an instance of java.lang.Class. What other behavior would you require before saying that Java classes are objects?


In Smalltalk, when you type the name of a class you are really referencing a global variable that refers to a class object. "Dictionary new" means send the message #new to Dictionary, and the Dictionary class object will respond by giving you a new instance of itself. "Dictionary compile: 'foo ^ 42' classified: #accessing" will add a method "foo" to Dictionary under the "accessing" category that will return 42 (an instance of SmallInteger). Classes in Smalltalk are living objects available for manipulation at all times. They have their own instance variables/methods, just like any other object. Java, C++ and C# treat the class as some sort of namespace into which you can place global functions/variables.


Right yes of course that is vastly different. One way is to think of classes as immutable (which they are, in that sense). Of course adding a method to a class is possible - but being statically compiled makes that somewhat less useful ;)


Perhaps learning Objective-C would make me a better Java coder. Alternately, maybe all the ObjC practices that would translate have already been written up as standard Java patterns or included in the standard Java books and all I'd get from it would be frustration because I can't apply anything neat I see.


I'm playing with some Objective-C at the moment. For me the most interesting thing is how blurred the line between ObjC/OOP/message passing and straight C is. It's the first language I've used where you can treat the high level part as not much more than a library with custom syntax. I'm sure the lispers live in this world all the time ;)


Yes we do.


One of my favorite things Objective-C has over Java is no NullPointerExceptions (it's legal to send a message to nil).

One of my least favorite things Objective-C has is EXC_BAD_ACCESS (usually caused by memory mismanagement).


Learning another programming language will almost always make you a better coder. It gives you perspective on the language you spend most of your time in, and lets you see patterns that are non-obvious is your 'main' language.

To really benefit from learning another programming language, make sure that you actually learn the language, not just hack around in it for the weekend and say you know it.


Most languages are influenced by languages they proceed.

Simula, C++, C#, and Java are clearly method calling languages. Smalltalk and Objective-C are clearly message passing languages. That is the biggest distinction.

You will always be able to find a particular feature in a particular language that is influenced by languages that have preceded it.


I just want to remember everyone that Objective-C is nothing more than Smalltalk with C backward compatibility. Also, the static type checking in early Java is pretty much the same thing as in early C++ (when both were without templates/generics).


I think I would argue with the "nothing more" part. It has split and evolves from its origins. Also, the combination has some decent advantages over straight Smalltalk.


I think 'remind' might be the word you are looking for.


Don't Classes in Java get boiled off in the compiler? There are no "class objects" at runtime that I've ever seen.


What do you mean?

Classes are in some sense like objects, meaning they can hold their own state, but there is no notion of meta-classes like in Smalltalk.

The class in the JVM is like a module that holds functions and attributes. And the JVM byte-code is very class-centric.

The objects themselves are probably not what you think they are. The JVM currently has no notion of "call method next() on object O" ... that's done with something like ... invoke_virtual Enumerable.next(obj). The bytecode "invoke_virtual" is responsible for doing the single-dispatching (subtype-polymorphism) at runtime, otherwise it looks just like an invoke_static ... so the compiler is crucial for generating the correct byte-code, because the class has to be known at runtime ;)


There are. Think about classloaders and the whole dynamic code loading thing. Every single object has a class object associated with it.


But can you pass classes around as objects after they're loaded? Do they have a value beyond their identifier?


Yep, you can pass them around, they do have values, in fact, they can have different values for the same type sometimes. This has been the case since well before Java even had reflection - having them around is fairly central to the Java dynamic loading and security model.


Yes, although Java's reflection is somewhat of a pain in the ass and limited in many ways. But you can take a Class object and call newInstance(), and it will invoke the class's no-arg constructor.


Moreover, reflection is less of a language feature and more of a very low-level library feature. In ObjC, one can write

    Class c = [SomeRandomClass class];
    id thingy = [[c alloc] init];
but Java won't let you do the equivalent:

    Class c = SomeRandomClass;
    Object x = new c;


  Class c = SomeRandomClass.class;
  Object x=c.newInstance();


Yes, but that's making use of runtime magic (the Class class) which isn't used outside metaprogramming.


When you define a class, say "Foo", instances of Foo are Foo objects. "Foo", itself, is an instance of a class named "Class" which is a direct descendent of the class "Object".

In Java's type system, classes are 'first class objects':

http://en.wikipedia.org/wiki/First-class_object


    // unseen?
    if (foo.getClass() instanceof someClassObject) ...


In this case isn't someClassObject just a literal that's resolved at compile-time and erased into a simple identifier symbol?

You can't pass someClassObject as an argument to anything but an operator like new or instanceof, and you can't assign to or from it, it doesn't itself belong to a class. It isn't an object.


> In this case isn't someClassObject just a literal that's resolved at compile-time and erased into a simple identifier symbol?

No. Its a variable assigned in runtime.

   public void yesYouCan (Object foo, Class<?> someClassObject) {
      // compile time?
      if(foo.getClass() instanceof someClassObject) { ... }
   }
Regardless, class and symbolic resolution occur at both compilation (to create the binary clsss files) and in runtime in the JVM (to load and execute the binary generated by the compiler):

http://java.sun.com/docs/books/jls/second_edition/html/execu...

> You can't pass someClassObject as an argument to anything but an operator like new or instanceof, and you can't assign to or from it, it doesn't itself belong to a class. It isn't an object.

No, its quite possible/common to write methods that take class objects as parameters.


  if(foo instanceof SomeClassObject) ...
or

  if(SomeClassObject.isInstance(foo)) ...


I always felt that Java's design resembled Modula-3.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: