tags:

views:

767

answers:

16

Java has primitive data types which doesn't derive from object like in Ruby. So can we consider Java as a 100% object oriented language? Another question: Why doesn't Java design primitive data types the object way?

A: 

No. Javascript, for example, is.

What would those Integer and Long and Boolean classes be written in? How would you write an ArrayList or HashMap without primitive arrays?

alamar
Ruby have all the data types (primitive and non primitive) as objects. Still ruby have arraylist, hashmap and arrays.
Silent Warrior
They are built-in; you can't make your own arraylist as efficient as built-in one.That's a trade-off.
alamar
JavaScript has primitives too (numbers, strings, booleans, null, and undefined).
Jason Orendorff
+1  A: 

Java is not 100% OO. Java may going towards 99% OO (think of auto-boxing, Scala). I would say Java is now 87% OO.

Why java doesn't design primitive data types as object way ?

Back in the 90's there were Performance reasons and at the same time Java stays backward compatible. So they cannot take them out.

PeterMmm
I'm glad to hear you've got the exact figure.
Matthew Flaschen
87%? reminds me of: http://dilbert.com/strips/comic/2008-05-08/
Razzie
But they are wrong! Java is obviously 88.4% OO.
anon
that downvote trigger-finger is itchy again? I don't get why people downvote to below zero without giving an explanation...
fretje
Any source on those performance reasons back in the 90's btw?
Razzie
it wasn't me but I sure was tempted. Sorry, but completely made-up figures ("it is going to 99% (...) Java is now 87% OO) make me itch. That, and no source or explanation about 'back in the 90's there were Performance reasons'.
Razzie
That would be a good explanation indeed... I just don't like uncommented downvotes, especially if they put you below zero.
fretje
+1 for the LOL factor
Robert Munteanu
I agree fretje that downvotes should be accompanied with a comment.
Razzie
Ok, the 87% lacks the humor tag. There are very few things in life that are (or could be) 100%. Performance: remember in 97 Win95 64Mb RAM, running my first Applets and now thinking that all primitives living on the heap, maybe GC never lets run the Applets's code :-)
PeterMmm
People complaining about this answer are just ignorant. Sun Java 6u15 is 87.0% +/- 0.2% OO.
Tom Hawtin - tackline
+13  A: 

When Java first appeared (versions 1.x) the JVM was really, really slow. Not implementing primitives as first-class objects was a compromise they had taken for speed purposes, although I think in the long run it was a really bad decision.

"Object oriented" also means lots of things for lots of people. You can have class-based OO (C++, Java, C#), or you can have prototype-based OO (Javascript, Lua).

100% object oriented doesn't mean much, really. Ruby also has problems that you'll encounter from time to time.

What bothers me about Java is that it doesn't provide the means to abstract ideas efficiently, to extend the language where it has problems. And whenever this issue was raised (see Guy Steele's "Growing a Language") the "oh noes, but what about Joe Sixpack?" argument is given. Even if you design a language that prevents shooting yourself in the foot, there's a difference between accidental complexity and real complexity (see No Silver Bullet) and mediocre developers will always find creative ways to shoot themselves.

For example Perl 5 is not object-oriented, but it is extensible enough that it allows Moose, an object system that allows very advanced techniques for dealing with the complexity of OO. And syntactic sugar is no problem.

Alexandru Nedelcu
A: 

So can we consider java as 100% object oriented language?

No.

Another question : Why java doesn't design primitive data types as object way?

Mainly for performance reasons, possibly also to be more familiar to people coming from C++.

Michael Borgwardt
+2  A: 

Java, for the reason you mentioned, having primitives, doesn't make it a purely object-oriented programming language. However, the enforcement of having every program be a class makes it very oriented toward object-oriented programming.

Ruby, as you mentioned, and happened to be the first language that came to my mind as well, is a language that does not have primitives, and all values are objects. This certainly does make it more object-oriented than Java. On the other hand, to my knowledge, there is no requirement that a piece of code must be associated with a class, as is the case with Java.

That said, Java does have objects that wrap around the primitives such as Integer, Boolean, Character and such. The reason for having primitives is probably the reason given in Peter's answer -- back when Java was introduced in the mid-90's, memory on systems were mostly in the double-digit megabytes, so having each and every value be an object was large overhead.

(Large, of course is relative. I can't remember the exact numbers, but an object overhead was around 50-100 bytes of memory. Definitely more than the minimum of several bytes required for primitive values)

Today, with many desktops with multiple gigabytes of memory, the overhead of objects are less of an issue.

coobird
In ruby when you're declaring a method or a variable outside of a class, it is implicitly added to Kernel, the main object.Wrapping all methods inside classes doesn't make it more OO.
Alexandru Nedelcu
@Alexandru Nedelcu: Ah, thank you for the information :) And also, it's quite true that just because one wraps code in classes doesn't make it object-oriented. It's easy to write procedural code in a object-oriented programming language.
coobird
+7  A: 

To get to true 100% OO think Smalltalk for instance, where everything is an object, including the compiler itself, and even if statements: ifTrue: is a message sent to a Boolean with a block of code parameter.

Remus Rusanu
+1 for citing ifTrue:
dfa
A: 

This is one of those questions that really only matters in an academic sense. Ruby is optimized to treat ints, longs, etc. as primitives whenever possible. Java just made this explicit. If Java had primitives be objects, there would be IntPrimitive, LongPrimitive, etc (by whatever name) classes. which would most likely be final without special methods (e.g. no IntPrimitive.factorial). Which would mean for most purposes they would be primitives.

Matthew Flaschen
A: 

One reason Java can't obviously do away with non-object primitives (int, etc.) is that it does not support native data members. Imagine:

class int extends object
{
    // need some data member here.  but what type?
    public native int();
    public native int plus(int x);
    // several more non-mutating methods
};

On second thoughts, we know Java maintains internal data per object (locks, etc.). Maybe we could define class int with no data members, but with native methods that manipulate this internal data.

Remaining issues: Constants -- but these can be dealt with similarly to strings. Operators are just syntactical sugar and + and would be mapped do the plus method at compile time, although we need to be careful that int.plus(float) returns float as does float.plus(int), and so on.

Ultimately I think the justification for primitives is efficiency: the static analysis needed to determine that an int object can be treated purely as JVM integer value may have been considered too big a problem when the language was designed.

Edmund
+1  A: 

Java clearly is not 100% OO. You can easily program it in a procedural style. Most people do. It's true that the libraries and containers tend not to be as forgiving of this paradigm.

Tom Hawtin - tackline
+2  A: 

"Why java doesn't design primitive data types as object way ?"

At Sun developer days, quite a few years ago I remember James Gosling answering this. He said that they'd liked to have totally abstracted away from primitives - to leave only standard objects, but then ran out of time and decided to ship with what they had. Very sad.

cartoonfox
+12  A: 

No, because it has data types that are not objects (such as int and byte). I believe Smalltalk is truly object-oriented but I have only a little experience with that language (about two months worth some five years ago).

I've also heard claims from the Ruby crowd but I have zero experience with that language.

This is, of course, using the definition of "truly OO" meaning it only has objects and no other types. Others may not agree with this definition.


It appears, after a little research into Python (I had no idea about the name/object distinction despite having coded in it for a year or so - more fool me, I guess), that it may indeed be truly OO.

The following code works fine:

#!/usr/bin/python
i = 7
print id(i)
print type(i)
print i.__str__()

outputting:

6701648
<type 'int'>
7

so even the base integers are objects here.

paxdiablo
Ruby (and Python) are pure Object oriented languages where all elements are objects.
notnoop
@notnoop, I can write 'i = 7; print "hello", i' in Python. It's hard to believe that's truly OO.
paxdiablo
Smalltalk / Sqeak is fully object oriented.
darren
@paxdiablo thats just syntax sugar. Everything really is an object underneath.
darren
Well, it's just as easy to say everything is procedural underneath *that* or manipulating electrons under *that*. But OO has always been, to me, treating things as objects in the source. What are the methods that can be applied to i, such as i.toFloat() for example?
paxdiablo
@darren it's Squeak, with a u. And the full name is, of course Squeak Smalltalk (just googled the latter).
jae
@paxdiablo, Haven't programmed in Python, but I know Ruby is totally object-oriented. For example, `7.next` or `7.prime?` are functions I am calling on an Integer object. I think you can do the same in Python.
Anurag
my google skills failed: i couldnt find anything about Python number methods. However, opening a Python shell lets me do `7.05.__str__()`, however I couldn't do `12.__str__()`. However again, I imagine that's because the digit itself is considered a constant. It did let me do `num = 5` `num.__str__()`
seanmonstar
But the most interesting is Smalltalk where everything is an object. Assignments, expressions, and control-structures like for-loops, if-else, etc are all handled by passing messages to objects. A remarkable achievement considering the language came out 30 years ago.
Anurag
I stand corrected - looks like even integers are objects in Python. Apologies to the Pythonistas out there.
paxdiablo
Couldn't you even re-assign Integers in Python? as in "4 = 2" to permanently change the meaning of 4 to 2? I'm pretty positive you can do that for True/False at least.
Michael Stum
"4 = 2: SyntaxError: can't assign to literal" - nice try subverting the language but I'm pretty sure Guido wouldn't be that stupid :-)
paxdiablo
@MichaelStum Python strings, tuples and integers are immutable, you cannot change their values. Changing the value of a string really returns a new string. http://docs.python.org/reference/datamodel.html `4=2` is a syntax error, `can't assign to literal`
Schwern
You can do 'True = False' in Python and it really does change the value of True. But I couldn't mess with integers in the same way. It might be kind of fun sticking that in someone's code for about 5 minutes.
donut
Yes, that's the name/objerct distinction I was referring to. "i = 7" creates an int object 7 and binds the name i to it. Then "i = 8" creates a new object 8 and re-binds i to that, leaving the 7 dangling and subject to garbage collection (although I'm assuming Python is smart enough to have some stock objects always in existence for the common cases).
paxdiablo
@donut, that's likely because True is a name bound to a true object. You can rebind it to the false object if you want. Unfortunately you don't seem to be able to bind it back with 'True = True', although you can do 'True = not True' to create a new true object (damn you sadists).
paxdiablo
A: 

I'd say that full-OO languages are those which have their elements (classes, methods) accessible as objects to work with.

From this POV, Java is not fully OOP language, and JavaScript is (no matter it has primitives).

Ondra Žižka
A: 

According to Concepts in Programming Languages book, there is something called Ingalls test, proposed by Dan Ingalls a leader of the Smalltalk group. That is:

Can you define a new kind of integer, put your new integers into rectangles (which are already part of the window system), ask the system to blacken a rectangle, and have everything work?

And again according to the book Smalltalk passes this test but C++ and Java do not. Unfortunately book is not available online but here are some supporting slides (slide 33 answers your question).

Szere Dyeri
+2  A: 

The problem is that object-oriented is not really well defined and can mean a lot of things. This article explains the problem in more detail: http://www.paulgraham.com/reesoo.html

Also, Alan Kay (the inventor of Smalltalk and author(?) of the term "object-oriented") famously said that he hadn't C++ in mind when thought about OOP. So I think this could apply to Java as well.

The language being fully OO (whatever that means) is desirable, because it means better orthogonality, which is a good thing. But given that Java is not very orthogonal anyway in other respects, the small bit of its OO incompleteness probably doesn't matter in practice.

J S
A: 

Java is not fully object oriented. I would consider Smalltalk and Eiffel the most popular fully object oriented languages.

bertolami
+3  A: 

No, Java is not, since it has primitive data types, which are different from objects (they don't have methods, instance variables, etc.). Ruby, on the other hand, is completely OOP. Everything is an object. I can do this:

1.class

And it will return the class of 1 (Fixnum, which is basically a number). You can't do this in Java.

Fixnum isn't exactly synonymous with number per se. A fixnum is, roughly, a non-bignum integer, i.e., one that fits within your machine's word size. Try `100000000.class` versus `1000000000000000000000000.class`. The main reason for the distinction is that a fixnum can fit in your stack, whereas a bignum would most likely live in your heap.
Chris Jester-Young
On my 64-bit machine, running MRI, all integers in [-2^62, 2^62) are fixnums. This is normal; usually an implementation would reserve the top few bits (2, in MRI's case) for "tagging" whether the rest of the word specifies a fixnum, or whether it's an address to a heap object, or whether it's free, etc.
Chris Jester-Young
Ah, yes, you are correct. My apologies for not being attentive to details :)