• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is primitive data type ?

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java, what is primitive data type ? Could we say, those primitive type is defined in Operating System or assembly language ? why people say Java is not pure OOP just because it has primitive data type ?

Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, they are not defined by the OS or in assembler. What it means is that the contents of a reference to a number is the number itself. The same question came up I think on this forum a couple of days ago; a search might help.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward Chen wrote:In java, what is primitive data type ? Could we say, those primitive type is defined in Operating System or assembly language ? why people say Java is not pure OOP just because it has primitive data type ?
Thanks.


Primitives in Java are: boolean, char, float, double, long, integer, short and byte.

You can say that those types have extensive support by most processors and therefore can be very efficiently implemented . That's why they were introduced in Java in the first place.

The reason people think primitives subtract frome the OO pureness of Java is because in a pure OO language everything should be treated uniformly as objects. But if you feel that way you don't have to use primitives if you don't want to. You can use the so called wrapper classes instead. Each primitive has a corresponding wrapper class like: Boolean, Character, Float ......
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect they weren't so much introduced into Java as copied from C/C++. Alot of Java syntax was copied from C/C++ presumably to make it appear familiar to C/C++ programmers, including the names of 7 of the 8 primitive types/keywords.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I suspect they weren't so much introduced into Java as copied from C/C++. Alot of Java syntax was copied from C/C++ presumably to make it appear familiar to C/C++ programmers, including the names of 7 of the 8 primitive types/keywords.


Of course the C-like syntax was paramount to get early acceptance. Java had to look familiar to the programmers of the time. But it couldn't afford to be inefficient either. This is an excerpt from an interview with Gosling, the inventor of Java, published in JavaWorld in 2001,

---
Bill Venners: Why are there primitive types in Java? Why wasn't everything just an object?

James Gosling: Totally an efficiency thing (*). There are all kinds of people who have built systems where ints and that are all objects. There are a variety of ways to do that, and all of them have some pretty serious problems. Some of them are just slow, because they allocate memory for everything. Some of them try to do objects where sometimes they are objects, sometimes they are not (which is what the standard LISP system did), and then things get really weird. It kind of works, but it's strange.
---

(*) My emphasis.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an interesting answer from James Gosling, but it's not really necessary to have primitive types at the language level for efficiency. It's possible to make everything an object in the programming language itself, and have to compiler optimize it so that it uses primitive types behind the scenes whenever it makes sense for efficiency.

Newer programming languages, such as Scala for example, do this - for the programmer, everything looks like an object, but the compiler really uses primitives behind the scenes. The Java programming language can't be changed in this way without breaking compatibility, so this is probably never going to be added to the Java programming language.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:That's an interesting answer from James Gosling, but


Java already has changed in this direction. You have the wraper classes and you have autoboxing. Is it fast - no. Is Scala fast - probably not. Is any language without direct support for primitives fast - not that I've heard of.

Please dream on but when push comes to shove real programming languages need direct support for primitives to be efficient. At least for the foreseeable future.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uj, I'm not sure you have understood what I meant.

Primitives are not necessary at the language level for efficiency. That doesn't mean that the JVM shouldn't have primtives at all. Scala doesn't have primitives, and it is just as fast as Java. You use Scala's classes that represent numbers, and the Scala compiler is smart enough to convert that to bytecode that uses the JVM's primitives. But as a programmer you don't need to know that there's a distinction between primitives and non-primitives. The compiler can make this distinction for you.

I don't mean Java's wrapper classes - those are not optimized by the compiler into primitives at the JVM level (and therefore they are relatively inefficient).

Your argument sounds just like old-time C or C++ programmers, who thought 10 years ago that Java would be useless because it doesn't have pointers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic