Login
Copyright

Non-Primitive Data Types in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Reference Data Types in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Primitive Versus Non-Primitive
  • 1:48 Class Data Types
  • 2:19 Interface Data Types
  • 2:38 Array Data Types
  • 3:19 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Login or Sign up

Timeline
Autoplay
Autoplay
Create an account to start this course today
Try it free for 5 days!
Create An Account

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 15 years experience in Human Resources Information Systems and has a PhD in Information Technology Management

Like life forms, non-primitive data types in Java are more sophisticated than their primitive brethren. This lesson covers the non-primitive types, also called reference types. Code examples are provided to show their use and functionality.

Primitive Versus Non-Primitive

The primitive data types include byte, int, long, short, float, double, and char. They are part of the core of Java and you don't need anything special to use them. For example, the following declares a long variable for a partNumber:


Java primitive data declaration


A data type that is primitive, such as the long variable, actually stores the value. If we give a value to the partNumber value, for example 4030023, that is what Java stores.

Non-primitive, or reference data types, are the more sophisticated members of the data type family. They don't store the value, but store a reference to that value. Instead of partNumber 4030023, Java keeps the reference, also called address, to that value, not the value itself.

Reference types can be a class, interface, or array variable. Remember that a class is a set of plans for a given object. There are thousands of tree objects, but the parent set of plans would belong in the tree class. Variables can exist inside the tree class, such as height or tree type. These are reference variables.

An array is a single object that contains multiple values of the same type. We could have declared our integer for partNumbers as an array to hold a given number of partNumbers in a single object.

The diagram you're looking at on your screen illustrates an example of primitive variables versus a reference variable. Notice how the values are stored in the primitive variables but the non-primitive or reference variable points to an address in memory.


Java non-primitive diagram


Now let's take a closer look at each type of reference data type, starting with the class.

Class Data Types

Let's say we declare a new class called Product:


Java class definition


In order to create a new non-primitive or reference variable for this class, we have to create a new instance of the Product class. The new keyword is used to create an object. Look at the following example where we'll be creating a new Product called car wax.

The Java code is as follows:


Java class variable


So now we have a variable of carWax: But it's really an instance of the Product class, and not a set value like the primitive variables.

Interface Data Types

An interface is like a dashboard or control panel for a class. It has the buttons, but the function is elsewhere. We won't go into detail on implementing interfaces since the focus is on the interface as a non-primitive, or reference, data type.


Java non-primitive interface


Since we have the interface, we can create a variable, much like we did with the class variable:


Declare interface variable


Array Data Types


Java array example


Like the other non-primitive or reference data types, the array doesn't hold the actual value, but an address in memory. To show this in action, take a look at the following code. We'll fill some of the buckets with values, and print out the value of the array we created.


Java array fill values


To unlock this lesson you must be a Study.com Member.
Create your account

Register for a free trial

Are you a student or a teacher?
I am a teacher
What is your educational goal?
 Back

Unlock Your Education

See for yourself why 10 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back

Earning College Credit

Did you know… We have over 95 college courses that prepare you to earn credit by exam that is accepted by over 2,000 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it free for 5 days!
Create An Account
Support