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
  • 57
  • 7
  • 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

2 Answers2

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
  • 16,886
  • 2
  • 27
  • 60
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
  • 1,778
  • 1
  • 4
  • 16