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?
-
4Is this homework? It sounds like a homework question to me. – Scott Wisniewski Jun 10 '09 at 09:40
-
1It does a little bit Gishu. There's an (unofficial?) homework 'policy' on SO. For example, you should answer homework questions differently, e.g. not give the questioner a complete answer, but more pointers in the right direction, usefull links, etcetera. – Razzie Jun 10 '09 at 10:00
-
@Razzie - The answers tend to be quite comprehensive contrary to 'SO policy' - See the accepted answer in this instance. – Gishu Jun 10 '09 at 10:44
-
The homework "policy" that Razzie refers pertains when people simply ask for the code to be written for them (and not really for this question.) In these give-me-code cases hints, pointers, links, are more appropriate. – basszero Nov 11 '09 at 12:32
-
5I don't see the point of this question. – Cheeso Jan 13 '10 at 05:34
-
13I do. The poster has probably seen the term “fully object-oriented” somewhere and wants to know if Java, maybe the language he’s most familiar with, is object oriented. – zoul Jan 13 '10 at 05:47
16 Answers
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.

- 22,539
- 9
- 61
- 90

- 8,061
- 2
- 34
- 39
-
[Are Java's primitives first class citizens?](http://stackoverflow.com/q/26830806/2047418) – Filip Bartuzi Nov 09 '14 at 17:15
-
C#/.NET shows that one doesn't need to give up "Object Oriented Types" to implement "primitives" efficiently. – user2864740 Dec 11 '14 at 05:59
-
@user2864740 But that also came later, with the benefit of hindsight. – Tripp Kinetics Oct 12 '15 at 21:38
-
-
Is there any *evidence* that they didn't want to use primitives from the start? And that doing so was really a 'compromise' that actually got made? And that they ever even considered making primitives first-class objects in Java? – user207421 Jun 09 '18 at 01:03
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.

- 854,327
- 234
- 1,573
- 1,953
-
1Ruby (and Python) are pure Object oriented languages where all elements are objects. – notnoop Jan 13 '10 at 05:39
-
1@notnoop, I can write 'i = 7; print "hello", i' in Python. It's hard to believe that's truly OO. – paxdiablo Jan 13 '10 at 05:43
-
1
-
@paxdiablo thats just syntax sugar. Everything really is an object underneath. – D.C. Jan 13 '10 at 05:44
-
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 Jan 13 '10 at 05:48
-
@darren it's Squeak, with a u. And the full name is, of course Squeak Smalltalk (just googled the latter). – Jürgen A. Erhard Jan 13 '10 at 05:51
-
@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 Jan 13 '10 at 05:55
-
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 Jan 13 '10 at 06:02
-
I stand corrected - looks like even integers are objects in Python. Apologies to the Pythonistas out there. – paxdiablo Jan 13 '10 at 06:27
-
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 Jan 13 '10 at 06:32
-
1"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 Jan 13 '10 at 06:43
-
@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 Jan 13 '10 at 06:46
-
2You 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 Jan 13 '10 at 07:01
-
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 Jan 13 '10 at 07:02
-
3@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 Jan 13 '10 at 07:05
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.

- 288,378
- 40
- 442
- 569
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.

- 1,105
- 7
- 11
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. – C. K. Young Jan 13 '10 at 19:56
-
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. – C. K. Young Jan 13 '10 at 20:00
-
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.

- 24,152
- 13
- 73
- 111
-
2
-
5
-
3
-
1that downvote trigger-finger is itchy again? I don't get why people downvote to below zero without giving an explanation... – fretje Jun 10 '09 at 09:49
-
-
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 Jun 10 '09 at 09:51
-
That would be a good explanation indeed... I just don't like uncommented downvotes, especially if they put you below zero. – fretje Jun 10 '09 at 09:55
-
-
1Ok, 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 Jun 10 '09 at 10:08
-
2People complaining about this answer are just ignorant. Sun Java 6u15 is 87.0% +/- 0.2% OO. – Tom Hawtin - tackline Jun 10 '09 at 10:26
-
Is there any actual *evidence* that they ever even considered making primitives first-class objects in Java? – user207421 Jun 09 '18 at 01:05
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.

- 159,216
- 35
- 211
- 226
-
1In 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 Jun 10 '09 at 10:04
-
@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 Jun 10 '09 at 10:08
-
"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.

- 6,941
- 3
- 39
- 48
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).

- 43,948
- 41
- 217
- 277
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).

- 14,916
- 11
- 39
- 42
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++.

- 342,105
- 78
- 482
- 720
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.

- 10,533
- 3
- 39
- 57
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?

- 18,729
- 4
- 64
- 97
-
Ruby have all the data types (primitive and non primitive) as objects. Still ruby have arraylist, hashmap and arrays. – Silent Warrior Jun 10 '09 at 09:40
-
They are built-in; you can't make your own arraylist as efficient as built-in one. That's a trade-off. – alamar Jun 10 '09 at 09:46
-
1JavaScript has primitives too (numbers, strings, booleans, null, and undefined). – Jason Orendorff Dec 04 '09 at 23:14
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.

- 278,309
- 50
- 514
- 539
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.

- 145,806
- 30
- 211
- 305
Java is not fully object oriented. I would consider Smalltalk and Eiffel the most popular fully object oriented languages.

- 2,896
- 2
- 24
- 41