What are primitive data types in Java? Well, you remember us talking about data types and Object Oriented programming right? Well, in Java, primitives are data types and have nothing to do with Object Oriented programming!
In Java, everything “extends” from Objects, except primitive data types. Think of primitives like the building blocks from which programming languages are built. If you were to read it in a catchy headline it would be “Programming 1.0 – Primitive Types”, vs. “Programming 2.0 – Objects”.
So what do primitive data types look like in Java?
int aPrimitiveInteger;
double aPrimitiveDouble;
char[] aPrimitiveString;
Okay, so what you'll notice is that the primitive version of an Integer
is int
, the primitive version of a Double
is double
and the primitive version of a String
is an array of char
s. (note: if you don't know what an array is, learn here)
Now that we know what these things look like, how do they work? Let's start by showing what the differences are… consider this code:
private static int anInt;
private static Integer anotherInt;
public static void main (String[] args)
{
System.out.println(anInt);
System.out.println(anotherInt);
}
What is the outcome of this code?
0
null
This is the first big difference between a primitive data type and an Object, primitives have default values that are assigned to them as soon as they are declared (in all cases except when the primitives are local variables). So in other words, as soon as I write the code private static int anyInt;
that variable will have the value of 0
. Now I could have easily chosen to give it a value like this:
private static int aPrimitiveInteger = 32;
But the point I'm trying to get at, is that you don't have to give the primitive an initial value, it'll have one as soon as it's created (again, in most cases). Primitive int
‘s are NEVER null
. Whereas, their Object counterparts Integer
always have a value of null
if they're not assigned a value.
Why you should care about Primitive Data Types
Okay, so that's mildly interesting, but why do I really care about primitives? Well, the most compelling reason to use a primitive over its Object counterpart is performance. Primitives perform much better in Java than their Object counterparts. Let's say you have to write some code that will calculate the latitude and longitude of some given coordinates, but, those coordinates are in some other format (let's say UTM). There is a complicated formula for converting UTM coordinates into latitude/longitude coordinates. If you were to use nothing but Java Objects to convert these values, it would take MUCH longer to do so than if you were to use Java primitives. Now, this type of situation doesn't present itself often, but it's good to know this information anyway, because if you want to become a programmer in the real world, you'll need to be armed with this kind of knowledge. In the real world, whether you're writing code for your online blog, or for an international software beheamouth, you want to create code that runs as fast as possible, because we all hate waiting on computers right?
Alright, so if primitives are faster than Objects, then why don't we always use primitives? Well, that's probably because their Object counterparts have lots of built in methods that help us do things. For example, you'll likely be in a situation when you may need to get the String equivalent of an Integer value, consider this method:
public void displayPhoneNumber(String areaCode, String number)
{
System.out.println("(" + areaCode + ")" + number);
}
Now let's say all you have is Integers to pass into this method. You'll need to first convert your Integer into a String before Java will let you run your code, so you'll need to do this:
Integer myAreaCode = 416
Integer myNumber = 5558262
displayPhoneNumber(myAreaCode.toString(), myNumber.toString());
You see? We've converted our Integer to a String by using the toString()
method of the Integer Object. If we had used a primitive int
, we would not be able to do this.
So there's clearly a trade-off of performance vs. convenience. If you're dealing with straight up numbers and calculations, primitives are a solid choice, if you need to be manipulating your variables' data then using the Object equivalent makes more sense. In any case, you should now have a better understanding of what primitive data types are in Java. As always, if you have questions or comments please leave a comment below, and if you love what you've read, please Tweet/Like/Stumble this post and share the love :)