This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Why are primitive types still maintained in Java,as Wrapper classes for the same is supported?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.,

My question here is ,Why are primitive types still maintained in Java,as Wrapper classes for d same is present under the main class Number?, as it is clearly stated that Everything is an Object in java. Why primitive types are still maintained, as one can do all sort of operations and manipulations with itz co-class-ers itself? !

Thanks,
Vinay.
 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are still used in programs. (which are stored in stack). I don't think replacing everything with Wrapper objects is a good practice even though you can do so...
It's just waste of memory....!
 
Av Vinay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.

Regards,
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Av Vinay wrote:My question here is ,Why are primitive types still maintained in Java,as Wrapper classes for d same is present under the main class Number?, as it is clearly stated that Everything is an Object in java. Why primitive types are still maintained, as one can do all sort of operations and manipulations with itz co-class-ers itself? !



That's not correct. Not everything in Java is an object, there are primitives too. It's in a pure OO language everything is an object. Java isn't that pure.

Primitives were introduced in Java for performance reason at the very beginning. There's no way they can be removed now. And besides, they're still needed for performance reasons. Although fast today, the object creation/garbage collection cycle still adds overhead that people wouldn't accept just for the luxury of being be able to call Java "pure OO".
 
Av Vinay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your views and suggestions.

Cheers.,
Vinay
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that there are programming languages that do not have primitives - everything is really an object, and you can even call methods on numbers, for example (Ruby):

This calls the method "times" on the number 5.

There's really no reason anymore why a language needs to have primitive types, not even for performance - the compiler can be made smart enough to compile code to use primitives behind the scenes. However, in the time when Java was invented (second half of the 1990's) those ideas were not as advanced as they are now. Java was designed to be like C++, and C++ has primitive types also.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper Young wrote:There's really no reason anymore why a language needs to have primitive types, not even for performance - the compiler can be made smart enough to compile code to use primitives behind the scenes. However, in the time when Java was invented (second half of the 1990's) those ideas were not as advanced as they are now. Java was designed to be like C++, and C++ has primitive types also.



Correction. Java was designed NOT to be like C++.

You're claiming that "the compiler can be made smart enough to compile code to use primitives behind the scenes". That's possible but the problem is that no compiler is that smart today. Possible optimizations, like escape analysis, have been in the pipline for years in Sun's compiler but for unknown reasons they're not introduced. Maybe they don't give the expected performance improvements or maybe they're unsafe or maybe Sun is complacent because there's no competition or .... Who knows really but the fact remains - primitives are need today for performance reasons and it's likely to stay that way in the foreseeable future.

Nobody is forced to use primitives if they don't want to. Please feel free to quit using primitives right away. There's a wrapper class for every primitive and there's autoboxing to mimic primitive's syntax. Unfortunately Sun has decided it's better to hide the rift between primitives and objects with one kludge after another, than it is to bite the bullet and bridge the gap at a fundamental level.
 
Av Vinay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its clearly pointed out that, the basic moto of Java is not to add many syntactic sugars into its predessossor languages like C and C++, they are maintaining in Java also. As opposed to Boxing concept, Wrappers were introduced in the initial version of Java itself, So it triggers a question of using the primitive types in OO programming language.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

uj nossnahoj wrote:
Correction. Java was designed NOT to be like C++.



Yes, it absolutely was designed to look like C, Objective-C (Patrick Naughton's influence) and C++ (Gosling) to foster rapid adoption. Java pulls plenty of ideas from other languages, especially Smalltalk, but syntactically, it's C/Objective-C/C++ all the way. Yes, absolutely, it was designed to avoid many of C++'s flaws. But the syntax is just another version of "C with Classes."


You're claiming that "the compiler can be made smart enough to compile code to use primitives behind the scenes". That's possible but the problem is that no compiler is that smart today.



No Java compiler, or no compiler of any kind? Lots of compilers are this smart; Java compilers aren't because that's not how the semantics of Java are defined.


Possible optimizations, like escape analysis, have been in the pipline for years in Sun's compiler but for unknown reasons they're not introduced.



Escape analysis is actually in 1.6.0_14 and later; check the release notes. It's an option you need to turn on. It doesn't result in enormous performance gains because Sun's allocator is extremely good; avoiding an allocation doesn't accomplish much.

Now, as to the actual topic of this thread: the syntax is modeled after the C family of languages, and that's why it started out with primitives. We still have them because the folks behind Java are smart cookies, and they have taken great pains to avoid breaking any Java code that's ever been written. Pretty much every Java program ever written still works in a modern JVM; it's one of the things that's helped Java become such a dominant force.
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic