1

First of all, I know that Java is call by value.

However, in a certain article, there is no such thing as an address in Java, and you should use the word reference. In my opinion, both words are interpreted with the same meaning. Am I wrong?

  1. What is the difference between address and reference, and do you have such a strict definition that you have to distinguish between the two?

  2. Some people use the term memory reference. Is this the address then? Or is it a reference?

I've searched on various sites, but I can't find an article that can answer my question, so I'm posting this question. thank you.

paiwmsn
  • 67
  • 1
  • 9
  • We don't use "address" in java, you probably mean [C/C++ Pointers vs Java References](https://www.geeksforgeeks.org/is-there-any-concept-of-pointers-in-java/). – zhh Aug 17 '21 at 08:52
  • There is no such thing as an address that you can _usefully make use of_ in Java. Things are stored in memory, at a particular address; but you can't say "please show me the contents of ", you can only access memory if you have a reference to it (as returned, ultimately, by a call to `new`); nor can you say "please show me the contents of the address next to ". – Andy Turner Aug 17 '21 at 08:54

3 Answers3

1

Java spec doesn't introduce definition of address, so if we not talking about JVM implementation there is no such thing.

If we look deeper we can say that difference between address and reference is caused by GC. Specifically by its ability to relocate objects.

Lets say we have object o in memory. Its address is 100500. After GC all references pointing to o will still point to it, but 100500 is not where that object is located now.

This is only my opinion. There is, as I said before, no official definition of address.

talex
  • 17,973
  • 3
  • 29
  • 66
1

Address - the exact location of the object in memory.

Reference is a variable through which you can access the object.

In Java you can get memory location of object, but you probably shouldn't.

vszholobov
  • 2,133
  • 1
  • 8
  • 23
0

Answering this to correct myself please reply if I am wrong:

As we know we do coding to make communication between computer language for human reading. So, when we say reference or reference variable we are talking about the Objects address in memory (which could be anything in the computer's own language). so in stack memory, we hold the reference to the object(Object gets stored in heap memory), and that address in stack memory we use reference variable to interpret between human and computer. As an ex. we created an Object sample. Which gets stored to heap memory and its address is let's say 3456. So, we assign the variable sample to this address 3456 to make it easier to use while coding.

vivek s
  • 91
  • 1
  • 3