Reference variable in programming
Last Updated :
20 May, 2024
Reference Variable is a reference to an existing variable, which is defined with the help of the & operator. In other words, reference variables are aliases of some existing variables, and then either of the two variables can be used. Updating the reference variable is same as updating the original variable.
Reference variable in C++:
In C++, the term "reference variable" refers to a variable that acts as an alias for another variable. Unlike pointers, which store addresses, reference variables directly refer to the variable they are bound to. Once initialized, a reference variable cannot be reseated or made to refer to another variable.
Below is the implementation of Reference variable in C++:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string name = "GeeksforGeeks";
string& referenceVar = name;
// Changes made to the reference variable is same as
// changes made to original variable
reverse(referenceVar.begin(), referenceVar.end());
cout << "name = " << name << "\n";
cout << "referenceVar = " << referenceVar << "\n";
return 0;
}
Outputname = skeeGrofskeeG
referenceVar = skeeGrofskeeG
Reference variable in Java:
In Java, we can access an object only through reference variable. A reference variable is declared as a special data type and this data type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables. A reference variable that is declared as final cannot be reassigned as a reference to another object. In this, the data inside the object can be changed, but the reference variable cannot be changed.
Below is the implementation of Reference Variable in Java:
Java
public class ReferenceVariableDemo {
public static void main(String[] args) {
// Declare and initialize reference variables
String[] languages = {"Java", "C++", "Python"};
String[] ref = languages;
// Modify data through reference
ref[2] = "JavaScript";
// Print original array
System.out.println("Original Array:");
for (String lang : languages) {
System.out.println(lang);
}
// Print modified array through reference
System.out.println("\nModified Array:");
for (String lang : ref) {
System.out.println(lang);
}
}
}
OutputOriginal Array:
Java
C++
JavaScript
Modified Array:
Java
C++
JavaScript
Reference variables are a basic concept of programming, through which a programming element is assigned a "reference" to data or memory. Learning the way these reference variables are built, used, benefits, and the techniques to make them work correctly can see developers using the variables to develop independent, strong, and superior software for use in various programming languages.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Types of Network Topology Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
12 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Types of Operating Systems Operating Systems can be categorized according to different criteria like whether an operating system is for mobile devices (examples Android and iOS) or desktop (examples Windows and Linux). Here, we are going to classify based on functionalities an operating system provides.8 Main Operating System
11 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read