answersLogoWhite

0

What is reference variable in java?

Updated: 8/10/2023
User Avatar

Wiki User

11y ago

Best Answer

A reference is a memory address. In some languages, like C, memory addresses can be stored in pointer variables. Thus a pointer is a reference variable. They are said to point to the memory address, and allow indirect access to that memory address. Since they are variable, they can be re-assigned (unless declared constant, in which case they are not variable).

However, in other languages, like C++, a reference is neither a pointer nor a variable -- it is an alias, an alternate name for an already existing object. References have no storage of their own thus they cannot be reassigned once initialised. In that respect they are like constant pointers. However, since they have storage of their own, there is no need to use indeirection. It's a bit like referrring to someone named William as Bill, Billy or Will -- they're all alternate names (aliases) for the same person.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Reference variable is used to create an alias name for a previously defined variable. Its use is that the same data object can be referred by two names and then these names can be used interchangeably.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

References are aliases, alternate names for objects that already have a name. They allow us to refer to an object by more than one name. This makes it possible for functions to refer to an object argument through a reference. The function uses one name for the object while the object itself has its own name. This is really no different to the way Bill and Billy can be references to someone named William. They all refer to the same person.

Not all languages support references. For instance, C has no concept of a reference, but it does have pointer variables. Pointers store memory addresses, thus any object that has an addressable identity can be referred to by that address. Thus if we store that address, we can refer to it through the pointer variable.

C++, which evolved from C, can also use pointers as references, but it also supports references in their own right.

The key difference is that pointers have an identity of their own, references do not. The address of a reference is always the object being referred to. After all; it is just an alternate name for that object. Conversely, the address of a pointer is the identity of the pointer, not the value of the pointer (the address being referred to).

References also differ from pointers in that pointers may be null whereas references can never be null. A null pointer is a pointer that holds no address (is assigned the value zero) and therefore points at nothing in particular. Thus when passing objects to functions that expect a valid object, pass a reference. If the object is an optional argument, pass a pointer.

Pointers must also be tested to ensure they are non-null before attempting to access the memory being pointed at. Dereferencing a null pointer has undefined behaviour.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

"A reference in Java Script affectively means a variable data type whose value takes the form of an address. A variable in Java Script is a terms that is used for an item of data that is named by an identifier. Each of these variables has a type which it can be classified under, such as 'int' or 'Object' and a scope. Commonly, in Java, variable have to be one of two types: 'Primitive' type or 'reference' type, and cannot be both. This statement leads to major simplifications within the syntax involved in certain expressions that are relative to C++. This provides a vast amount of flexibility for programmers using Java Script, but can lead to errors that are undetectable when compiling, and are only shown to exist when the writtenprogram is actually running.

A reference variable in Java contains an address, or a reference to an address (similar to pointer variables in C++). Java does not, however, allow this address to be arbitrarily (randomly) edited or changed in any way. This goes for pointer variables in C++ as well. So, reference variables are similar to pointer variables in C++. They provide a method through which to access another variable or memory address that has a variable, and allow changes to the data within the afore-mentioned memory address. A reference variable provides direct access to this memory address.

"

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

It is meaningless because a reference is not a variable of any kind. A reference is simply an alias for an already existing object and, once assigned, cannot be reassigned to another object. Although a reference is not variable, the object it refers to can be.

Note that a reference is not the same as a pointer, although they share some similar properties. A pointer is a variable that stores the address of an object and allows the object to be accessed through indirection (dereferencing). However, pointers can be reassigned and may be NULL while references cannot. References are easier to work with, but pointers are more flexible.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

By assigning an object (or reference) to them at the point of instantiation.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

test

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is reference variable in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Difference between reference variable and instance variable?

An object is the actual storage space in memory in which some collection of data resides.A reference variable is a variable which refers to the memory location of an object.Look at the pseudocode below:Object obj = new Object();Here obj is the reference variable, and the data to which it refers is the object.


What is reference data types in java?

A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed. Ex: ArrayList lst = new ArrayList(); The above line creates a reference variable lst which refers to an ArrayList object


What is native variable in java?

native is a key word used in java method. there is no variable as native in java


What is a variables in java?

A variable in java is something that holds a value and has a name attached to it. This value can change and hence its named a variable.There are two types of variables in Java:• Primitives - A primitive variable can be one of eight types: char, boolean, byte, short, int, long, double, or float. Once a primitive has been declared, its primitive type can never change, although in most cases its value can change.• Reference variables - A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed.


How you make shared and synchronized variable in java thread?

A Variable that is shared as well as synchronized cannot be created in Java. These two terms are mutually exclusive and a variable that is synchronized in java cannot be shared and vice versa

Related questions

Difference between reference variable and instance variable?

An object is the actual storage space in memory in which some collection of data resides.A reference variable is a variable which refers to the memory location of an object.Look at the pseudocode below:Object obj = new Object();Here obj is the reference variable, and the data to which it refers is the object.


What is reference data types in java?

A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed. Ex: ArrayList lst = new ArrayList(); The above line creates a reference variable lst which refers to an ArrayList object


Differences between pass by value pass by reference in java?

pass by value is the technique where a copy of the variable is passed to the method as argument. This value can be modified inside the method but that would not affect the original value. Pass by reference is the technique where the reference to the actual variable is passed to the method as argument. Any changes to this variable would affect and alter the original value. Usually primitive data types are passed by value and objects are passed by reference in java.


What is native variable in java?

native is a key word used in java method. there is no variable as native in java


Does java support call by reference?

No , Java does not support call by reference.


What is a polymorphism in java programming?

Polymorphism can be considered as the ability of one thing being multiple other things (though partially or fully). Am I confusing you? I believe yes. To put it in simpler words, any java object that can pass more than one Is-A test can be considered polymorphic. Other than objects of type Object, all Java objects are polymorphic in that they pass the IS-A test for their own type and for class Object. Remember that the only way to access an object is through a reference variable, and there are a few key things to remember about references: • A reference variable can be of only one type, and once declared, that type can never be changed (although the object it references can change). • A reference is a variable, so it can be reassigned to other objects, (unless the reference is declared final). • A reference variable's type determines the methods that can be invoked on the object the variable is referencing. • A reference variable can refer to any object of the same type as the declared reference, or-this is the big one-it can refer to any subtype of the declared type! • A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.


What is a variables in java?

A variable in java is something that holds a value and has a name attached to it. This value can change and hence its named a variable.There are two types of variables in Java:• Primitives - A primitive variable can be one of eight types: char, boolean, byte, short, int, long, double, or float. Once a primitive has been declared, its primitive type can never change, although in most cases its value can change.• Reference variables - A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed.


Why you use the reference variable?

The reference variable controls an object. Without the reference variable, you would have no way of accessing the object.


How you make shared and synchronized variable in java thread?

A Variable that is shared as well as synchronized cannot be created in Java. These two terms are mutually exclusive and a variable that is synchronized in java cannot be shared and vice versa


What is the syntax of global variable in java?

There's no global variables in Java.


What are the differences between C and Java reference variables?

Java does not have the concept of Reference Variables. We cannot access the memory location where the data is stored in Java.


What is a variable type in Java?

int