This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes what is primitive data type ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is primitive data type ? " Watch "what is primitive data type ? " New topic
Author

what is primitive data type ?

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Oct 22, 2009
Posts: 237
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Oct 22, 2009
Posts: 237
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is primitive data type ?
 
Similar Threads
Nontransient primitive type
wrapper classes
is Date a datatype ????
What is primitive data type?
objects