Copyright

Reference Data Types in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Abstract Data Types: Definition & Example

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:03 Reference Material
  • 0:33 Primitive Data Types
  • 1:38 Reference Data Types
  • 2:54 Java Usage Considerations
  • 3:48 Lesson Summary
Save Save Save

Want to watch this again later?

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

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Thomas Wall

Thomas is a professional software developer, online instructor, consultant and has a Masters degree.

In this lesson you'll learn about the Java reference types, how to create a reference variable and use it effectively in your programs, as well as the difference between reference types and primitive data types.

Reference Material

Whether it's a personal reference used on a job application, or a reference to a Wikipedia article, we use them every day. A reference allows us indirect access to something, without the overhead of having what's being referenced be physically present. Multiple people can refer to the same online recipe without each person needing their own copy. The reference to the recipe, the web address, can be passed from one person to another without having to transfer the recipe in its entirety.

Primitive Date Types

Variables in Java are identifiers that represent data stored in some form. Java has two kinds of data, primitive, a basic type of data that serves as a fundamental building block, and reference, which refers to where the data is stored. There are eight primitive data types and each uses a fixed-size block of memory to hold the value associated with a variable of that type.

Because everything about a primitive data type is already known by Java, a variable representing a primitive data type can be initialized with data at the time it's declared. Consider this code you're looking at on screen.

Primitive Data Type Creation

This code declares, creates, initializes, and manipulates variables representing primitive data types.

The printed output is:

value=66

myAge=65

It's important to note that when one primitive data type variable is assigned to another primitive data type variable, the actual content of the source variable is copied into the memory storage used by the destination variable, completely replacing any previous value. This is called 'assign by value.'

Reference Data Types

What about variables representing arrays, or instances of class objects whose size isn't fixed and may even change after they're created? After memory storage is initially allocated for the array or class object, using the new operator, a reference to where the object resides is stored in the variable instead of the actual data. Whenever the variable is accessed in the code, the reference is used to locate the specific array or object. Consider the code on your screen right now, which utilizes the built-in Java class Point, which houses the x and y coordinates associated with a particular Point object.


Java Reference Variable Creation


The printed output is what you see on screen now:

point1=Point[x=4,y=5]

point3=Point[x=4,y=5]

point2=Point[x=2,y=6]

point4=Point[x=2,y=6]

Unlike variables declared as primitive data types, variables declared as reference types use 'assign by reference.' When one such variable is assigned to another, the reference stored in the source variable is copied into the destination variable completely replacing any reference that was there, so both variables now refer to the same object's storage memory.

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

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 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 risk-free for 30 days!
Create an account
Support