Channels ▼

Eric Bruno

Dr. Dobb's Bloggers

A Modern Primitive Discussion

February 27, 2012

A lot can be said — and a lot has been said — about Java's inclusion of primitive types. You know, types like int, long, byte, char, and so on (eight of them in total). And in some cases, people like to throw String in there because it acts a little like a primitive since it's immutable. However, the String class isn't a primitive since it's truly a class and hence renders true objects when instantiated. But I digress.

Why are they there, when Java is purportedly an object-oriented language? Here are the main reasons:

  • Primitives are comfortable (coming from C/C++)
  • They're fast! Here's why:
    • The stack frame contains the actual value, which can be operated on directly. With objects, however, the stack frames contain references into the heap, which contains the value(s).
    • With primitives, there's no garbage collection.
    • Speed of operations is faster as the value is right in the stack — again, there's no object dereferencing.
  • The available operators are fixed, hence there's no ambiguity.

However, the fact that Java also includes Object-ified versions of primitives (i.e. Integer, Boolean, and so on) can lead to some confusion. So, beyond the confusion to beginners as to the differences between int and Integer, why is having primitives in Java problematic? In fact, the auto boxing and unboxing features of Java (introduced in Java 1.5) help to eliminate or at least "smooth out" these differences. For example:

Before autoboxing:

  int meaning=42;
  Integer theMeaning = new Integer(meaning);

...after:

  int meaning=42;
  Integer theMeaning  = meaning;

...or just:

  Integer theMeaning  = 42;

Before unboxing (which is really just the reverse of auto boxing):

  Boolean ready = new Boolean( true );
  if ( ready.booleanValue() == true ) { /* … */ }

...after:

  Boolean theCondition = new Boolean( true );
  if ( ready ) { /* … */ }

However, a lot of confusion remains. For example, you create objects using the new keyword, and use the dot-notation to pass messages (or call methods on) the resulting objects. Neither apply to primitives. This means that operators such as ++ work directly on the values of primitives, while they (kind of) result in method calls on objects. This isn't much of a problem, really, except that with objects, you never work directly with the data, hence the variable itself doesn't change. This means that you have a chance to bounds check or otherwise perform some error checking before the value is modified. With primitives, you do not.

Things get a little more confusing, and error-prone, when you tell a new Java programmer that you can compare two primitive types as such:


  int i = …
  int x = …
  if ( i == x ) { /* … */ }

But you cannot do this with String (or other) objects, because it compares the object references themselves, not necessarily the data they represent. For example, the following will yield unintended results:


  String root = "Eric";
  …
  String user = "Eric";
  if ( user == root ) { /*…*/ } // Not what was intended

Instead, you need to do the following:


  if ( user.equals( root ) ) { /*…*/ } // Good!

With other objects, this gets even more ambiguous and error-prone:


  Boolean conditionOne = new Boolean(true); 
  Boolean conditionTwo = new Boolean(true); 
  if ( conditionOne == conditionTwo ) { /*…*/ } // Error!!!

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video