r/javahelp • u/BikashDatta • Aug 17 '21
"Java is not fully object oriented because it supports primitive data type like int, byte, long etc. which are not objects"- Please explain the quoted statement.
"Java is not fully object oriented because it supports primitive data type like int, byte, long etc. which are not objects."
How does the primitive data types makes java not fully object oriented? please explain with example.
35
u/Roachmeister Java Dev Aug 17 '21
An Object, such as an Integer, InputStream, etc., "knows" how to do things with itself. For example, in Java, every object "knows" how to convert itself into a String, via the toString() method. A Database object might "know" how to connect to an external data source via connect(). A mathematical Vector object "knows" how to calculate its own magnitude() or norm().
By contrast, in non object oriented languages, the data is completely separate from the procedures that act on the data. In such languages, to turn an Integer into a String, you would pass it to an external function:
String s = makeStr(i)versus
String s = i.toString()Similarly, connect(db) versus db.connect(), or calcMagnitude(v) versus v.magnitude().
To get to the point, in Java, the primitives such as int, long, double, etc., are in the latter category. You can convert the int 20 to a String with Integer.toString(20), but you can't call 20.toString(). (Contrast this with a language like Scala, in which you can call 20.toString()).
This is why I would call Java a "primarily" or "mostly" OO language. There are some good design reasons for why it was designed like this, which I won't get into here.
16
6
u/seanprefect Grumpy Guy Who Remembers Java 2 Aug 17 '21
So practically it's just someone arguing semantics. But what they're saying is a FULLY object oriented language would only have objects however java has some non object datatypes called primitives. Its the difference between int and Integer. Java is very much an OO language it's just not 100% OO in the theoretical sense.
2
u/ZukoBestGirl Aug 17 '21
IDK if this adds much to the discussion. But you know what objects are, if not, somene explained below.
Now, in java, everything is pass by reference, except when it's not. That's why some people say "java is pass by value. BuT oBjEcTs ArE rEfErEnCe VaLuEs" or some such.
TL;DR, you can't access pointers, you can't use pointers by hand, in java. The language and the compiler do that for you.
When you have a method, and it requires a parameter of a object. That object is then passed to the method:
Whatever w = new Whatever(); // Whatever - class; w - object
doWhatever(w); // w = OBJECT in questionInternally, java transforms that "w" parameter into a pointer. A memory location for that OBJECT, thus IRL what it would look like for the compiler is
Whatever w = new Whatever(); // initialized at memory location 0x244FA921
doWhatever(0x244FA921);Primitives are not like that. A primitive is a value, and is treated as such. No memory location.
int two = 2;
doWhatever(two);can be seen by the compiler so:
int two = 2; // memory location 0x11AD42FF has value of "2"
doWhatever(2);The memory location is just "when I call the variable two, you will have to look at a memory location to see what value it holds"
But the method doesn't care where a primitive is at. A primitive is only represented by its value and nothing else matters. Memory location exists to corelate that value with a human readable name.
Primitives aren't objects. They don't have related methods and states and the such. Just a value. Then the type represents memory size needed, and how that numeric value is interpreted to become an int, or double, or char or boolean, or byteArray or whatever.
1
u/LambdaThrowawayy Aug 18 '21
What; no, Java is fully pass by value. The thing that throws people off is that object variables are called references in Java, when they're actually just basically pointers.
https://www.journaldev.com/3884/java-is-pass-by-value-and-not-pass-by-reference
1
1
u/bdmiz Aug 17 '21
It worth to mention here some practical aspects of having primitive types. All classes implicitly inherit Object class. It means that at all places where you can put Object, you can put any other class. It also means that all classes inherit methods from Object class such as equals or toString. In contrast, a primitive type does not inherit Object and it doesn't have those methods and generally speaking cannot be used in places where an Object can be used (though autoboxing kind of hides this fact).
It is not possible to use primitives in generics: we can have List<Integer>, but we cannot have List<int>, because int is not an Object.
A primitive cannot be set to null.
In other words, in Java language, there is an object and there is something which is not an object (doesn't behave like an object). This technically makes Java not a pure object oriented language.
•
u/AutoModerator Aug 17 '21
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.