+ 3
What exactly is reference in context of programming.
I have come across the word "reference" a lot of times and yet have a vague understanding about this word. So i want to ask what is reference in programming. Any help would be appreciable. Please give example. Thanks in advance P.S: I am neither asking for reference variable nor reference-types.
13 Antworten
+ 8
Ashish
An important side effect is that two variables can refer to the same object, as Anton Böhler mentioned. For example,
Sololearner sl1 = new Sololearner();
Sololearner sl2 = sl1;
Here, both sl1 and sl2 refer to the same instance of the Sololearner class.
+ 7
References determine when managed objects are released from memory and also affect what causes them to be retained.
• C/C++ Pointers vs Java References — https://www.geeksforgeeks.org/is-there-any-concept-of-pointers-in-java/
+ 5
D_Stark My friend,
When it comes to Java, I learned it this way, (I don't know where this definition came from but I saved it because helped me understand).
**A 'reference' is an address that indicates where an object's variables and methods are stored.
You aren't actually using objects when you assign an object to a variable or pass an object to a method as an argument. You aren't even using copies of the objects. Instead, you're using references to those objects.
+ 5
D_Stark Buddy,🤓
😏 I think so... 😄🍻
+ 5
Ashish You are very welcome.😉
😊Glad we were able to help.👍
+ 4
a reference is a pointer.
it points to a location in memory.
in Java every variable that stores an Object actually stores a reference to the Object. because of that when we do this:
int[] arr1 = {1, 2, 3};
int[] arr2 = arr1;
both arr1 and arr2 will store the same reference.
if we change on of the indexes in one of the arrays both will be affected!
hope this helped.
happy coding! 😄
+ 4
D_Stark Also,
References in Java become particularly important when arguments are passed to methods, becouse there are no explicit pointers or pointer arithmetic in Java, as there are in C and C++.
You know,
When we create an object from a class, Java allocates the amount of memory the object requires to store the object. Then, if you assign the object to a variable, the variable is actually assigned a 'reference' to the object, not the object itself. This reference is the address of the memory location where the object is stored.
So my friend, I think that you have right.👍🍻
+ 3
A reference is somthing that refers to somthing else its very abstract so its difficult to actully explain what a refrence looks like, how it knows its type or how it's doing what it does, other then that all I know is that its a hexadecimal address that points to somthing in memory.
I would actully like to know more about refrences 😁.
+ 2
It's pointer to start adress in ram in which alocated some variable, method, or class.
+ 2
Danijel Ivanović I think this is close to what I was trying to explain 😅
Pointer: A pointer is a variable that stores a memory address, for the purpose of acting as an alias to what is stored at that address.
Do think this is what java variables are doing (as always behind the scenes) 😅
+ 1
Danijel Ivanović would you say that the hexadecimal address that they refer to in books is the refrence ? Because it would make sense that when a refrence variable is created without assigning an object then the variable refrence is pointing to nothing?
+ 1
Danijel Ivanović yes thats correct 👌
what I learned is that the new keyword returns the refrenece of the newly created object and this is stored in the variable.
but for some reason I am also sure there is a refrence created by the variable that points to were the object is before it assigns the object refrence 🤔
+ 1
Danijel Ivanović D_Stark thanks you so much! At last, i feel like i would be able to understand reference whenever this word would rise in typical softwares documentations.