Primitive Variables vs. Referenced Variables in Java

Referenced variables & types of referenced variables in Java?

In this blog, we’ll dive deep into the world of referenced variables and explore their types, how they work, and why they are so crucial in Java programming. By the end of this read, you’ll have a clear understanding of:

  1. Definition of reference variable?
  2. Difference between primitive and referenced variables?
  3. What values can be stored in a referenced variable?
  4. Types of reference variables?
    • Local
    • Static
    • Non-static
    • Final
    • Volatile
    • Transient
  5. Where referenced variables provided memory location by whom and when?
  6. Write a program to show the creation of all three types of reference variables with JVM architecture.

Whether you’re a beginner or an experienced developer, this guide will help you master the concept of referenced variables and use them effectively in your code. Let’s get started!”

1. Definition of reference variable?

Ans: The variables created by using referenced datatypes are called referenced variables. Instead of holding the actual data, it holds the address of where the object is located. Referenced variables are created by using array, class, interface or enum.

For Example:

int[]   a;
Example e;
String  s;
2. Difference between primitive and referenced variables?

Ans: In Java, variables are used to store data, but they come in two flavors: primitive variables and referenced variables. Primitive variables stores values directly, but referenced variables stores reference of the object.

For Example:

Note: The variable created by using referenced types is called referenced variable because it points or referenced to object’s memory.

3. What values can be stored in a referenced variable?

Ans: Below are the two possible values:

  • Default value null -> it can be stored in all types of referenced variables.
  • Object reference -> the object should be same type of referenced variable.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply