How are references implemented in Oracle's JVM? - java

While searching for an explanation on how a reference variable is implemented in Java I came across this question:
What's inside a reference variable in Java?
In there was a comment by Samuel_xL saying that specifying the vendor name would be a better question.
So my question is that how an instance variable in implemented in Oracle JVM? Is it a pointer to an address? I know that a reference holds bits that tell the JVM how to access the object.
But how is it structured??

From what I've been able to determine, object references are stored either as a type called oop (ordinary object pointer) or narrowOop, depending on whether the JVM is using compressed object pointers or not. An oop is a C++ class that wraps a pointer to a Java object, and a narrowOop is a 32-bit unsigned integer that has to be converted into a proper pointer in order to access the object; they have no internal structure. You can find the declarations here: http://hg.openjdk.java.net/jdk7/jdk7/hotspot/file/tip/src/share/vm/oops/oopsHierarchy.hpp

Related

Doesn't java have pointer? [duplicate]

My Java book explains that to use objects, we can assign them to reference variables. How is that different from a pointer to an object? Does Java have pointers?
Thanks :)
A reference is sort of like a pointer that you can't do arithmetic on... although it's more opaque. While the underlying bits may be an address in virtual memory, they don't have to be. They're just a way of getting to an object (or representing the null value). So while they're not exactly the same, if you're used to thinking of a pointer as "a way of identifying an object or navigating to it" (in some sense) then yes, those thoughts apply to references too.
Java doesn't have pointers as such (unlike, say, C# which has references and pointers - the latter being used in "unsafe" code).
The terms "reference" and "pointer" are basically equivalent. Much of the literature I've seen about the basics of Java claims that Java has no pointers. But if you try to use a null reference you get a NullPointerException. So it's all semantics.
(The real difference is, in C or C++ the term "pointer" strictly means an integer that happens to be the memory address of some data. Whereas in Java the term "reference" more closely matches the C++ "reference" concept. You can't work with the memory address directly even if you want to, but you use it the same way.)
Cat x = new Cat();
This line creates a Cat object in memory and stores a reference to it in x.
x now contains a reference to the Cat object, but if you were to do:
x = x + 1;
This would not give the next memory address like in C, but would give a compiler error. Java doesn't allow control to the reference or memory location.
If we think of references or pointers as a kind of means to access or handle the object in memory, they are much the same. However, they are different in terms of implementation. Following are the differences:
Pointers support pointer arithmetic and this makes them a kind of unsafe. Because they provide the programmer with an access level that is much more than needed. However, references provide an handle to the object through a much more abstract hided implementation.
Pointers are less strongly typed and references are more strongly typed. By this I mean the code is much simple in case of references and slightly less formal in case of pointers. You need to make use of reinterpret_cast kind of things to use pointers casts and on the other hand, references support easy casts of the kind of primitive kind of casts in "C".
Pointers are unsafe and references are safe. If you have an option between the two select references. In languages like C#, you have this freedom.
Pointers exists in C/C++ and references exist in Java. Do not confuse references in C/C++ with references in Java. References in C/C++ is a kind of synonym used for the rvalue of a pointer in C/C++.
No, Java does not have pointers. The fundamental concepts in Java are "values" vs "references".
A reference is a pointer that you can't normally see the value of (i.e., the memory address). The only operations allowed are to set it (from another reference) and to reference through it to the referred-to object. It can be set from a reference-valued expression, such as the new operator, or from another reference (which is syntactically a simple reference-valued expression).
In terms of Java syntax - it's true, that Java doesn't have pointers, but to clarify situation - Java Runtime has pointers.
Every time GC occurs, there is a big chance, that your object stored in the heap will physically change it's memory address. This is achievable only by using what is really called Pointer. As you come from C++ world - you know reference restrictions. But it doesn't mean that JVM doesn't use references =).
For further information take a look at Ordinary Object Pointer
So, as you have brief understanding of core JVM memory management, you can consider Java Reference term as Pointer at runtime.
No, there is no pass by reference at all. Only pass by value, since the real Java method argument is a copy of Java Reference, therefore - another Pointer.
So, in syntax usage way - Java Reference is closer to C++ Reference, but in real Runtime world it's close to Pointer.
P.s. for more clarification it's better to read more articles about Ordinary Object Pointer or take a loot at oop.hpp.
As Java does not support pointers. It may appear that references are special kind of pointers. But we must note the key difference: with a pointer, we can point any address (which is actually a number slot in a memory). So, it is quite possible that with a pointer, we can point an invalid address also and then we may face surprises issues during runtime. But reference types will always
point to valid addresses or they will point to null.
pointer only contain the address but Reference does not contains address if we say frankly then
address of the object is assigned to the index or we say can hash code and case code is given to the reference variable if we will see the content of the reference variable it starts with class Name # and after it some Hexadecimal Code. These nos are not address it is a index value or hash code.
second point
we can not perform any Arithmetic operations on values the Content of reference value

When does the distinction between an actual object and the reference to said object become important? -Java

I am coming to Java from C++ and am having trouble grasping the true distinction between an object and a reference to the object.
When an object is instantiated a region of memory is reserved and to access/operate upon the data in the memory we use the reference to the object. This i understand but in my textbook the author states,
"An object reference variable that appears to hold an object actually contains a reference
to that object. Strictly speaking, an object reference variable and an object are different,
but most of the time the distinction can be ignored."
So I am curious as to when this distinction cannot be ignored. It kind of sounds like the way Pointers are distinguished from the address they point to. Do all Java reference variables operate in a similar fashion as C/C++ pointer variables? I.E are reference variables simply a pointer or reference to a memory address?
I have already gone through What are classes, references and objects? and I am still unclear as to how the distinction actually matters when working in Java. If anyone could provide an explanation or a link to a thorough explanation It would be greatly appreciated.

What is Object Reference Variable? [duplicate]

This question already has answers here:
What exactly is a reference in Java?
(4 answers)
Closed 9 years ago.
What is Object Reference variable in java?
Does the reference variable holds the memory address of the object?
I am confused. Please do explain.
I'm not sure I have the elegance to properly answer this, but...
An Object is an instance of a Class, it is stored some where in memory
A reference is what is used to describe the pointer to the memory location where the Object resides.
A variable is a means by which you can access that memory location within your application (its value is "variable"). While a variable can only point to a single memory address (if its not null), it may change and point to different locations through out the life cycle of the application
What is Object Reference variable in java?
Simply, it is a variable whose type is an object type; i.e. some type that is either java.lang.Object or a subtype of java.lang.Object.
Does the reference variable hold the memory address of the object?
Probably yes, but possibly no.
It depends on how the JVM represents object references. In most JVMs, the object reference is represented behind the scenes using a memory address or pointer. But it could also be represented as an index into an array ... or something else. (Indeed, I've messed around with an experimental JVM where an object reference was actually an index into an array of pointers.)
The point is that Java object references are an abstraction that is designed to hide the representation / implementation details from you. The actual representation should not concern you ... since it doesn't matter if you program in pure Java. You can't get hold of the actual memory address in pure Java ... and that's a good thing. The JVM (specifically the garbage collector) is liable to change an object's actual memory address without telling you. If an application could obtain and use object addresses, it would need to deal with that, and it is a fundamentally difficult problem.
Object Reference variable is just like pointer in c but not exactly a pointer.
Its depend's upon JRE provide some JRE treated just like a pointer and some other JRE treated as pointer to pointer.
so refernce variable just define a way to reach your object. Java is platform independent language so memory management is different in different devices so its difficult to give a unique way to reach the object.
yes Object reference is the variable that holds the memory location of the real object
In Java all objects are referred to by references for instance
Object o = "foo";
The above example has a reference, o, to the object "foo".

What exactly is a reference in Java?

What exactly is a reference in Java? Is it a memory address? Is a Java reference the equivalent of a dereferenced C++ pointer?
In other words, given the following:
Object o1 = new Object();
Object o2 = new Object();
o1 == o2
Is the above comparison the equivalent of comparing two pointers in C++?
o1 == o2 is pretty much equivalent to comparing two pointers in C/C++, yes.
But there are a two main differences between references in Java and pointers in C/C++ that are quite important:
Java references can't do pointer arithmetic: you can't "add 3" to a reference, you can only let it point to another (known) object
Java references are stongly typed: you can't "reinterpret" what lies on the other end of a reference unless you reinterpret it as a type that that object actually is.
Also a short note about the word "reference": C++ has references that act quite differently from both pointers in C and references in Java (but I don't know enough about C++ to tell you the specifics).
For a thorough discussion of this, see this related question on programmers.SE.
What exactly is a reference in Java?
It is an index of an object. It can be thought of as like a pointer but it differs in the fact that it
can change at any time.
does not always have a direct relationship with memory addresses.
is usually 32-bit in a 64-bit JVM.
you can't re-interpreter what the reference refers to. You can only change the type of the reference itself.
Is the above comparison the equivalent of comparing two pointers in C++?
Yes.
On Compresses Oops which allows a 64-bit JVM to sue 32-bit references.
Java HotSpotâ„¢ Virtual Machine Performance Enhancements - Compressed Oops
Compressed oops in the Hotspot JVM
IBM V6 - More effective heap usage using compressed references
Yes, a reference is basically the same thing as a pointer. By the way, if you call a method on a null reference, you get... a NullPointerException.
Note that it doesn't have to be a memory address, though. A given object can be stored elsewhere during a program execution, and still keep the same reference. But you don't need to care, as pointer arithmetic doesn't exist in Java.
A reference is a "pointer" to a memory address in Java, even though Java eleminates direct manipulation of pointers, unlike C++. Objects in Java are never passed to methods or returned by methods, it is always a reference that is being passed.

What is the difference between a pointer and a reference variable in Java?

My Java book explains that to use objects, we can assign them to reference variables. How is that different from a pointer to an object? Does Java have pointers?
Thanks :)
A reference is sort of like a pointer that you can't do arithmetic on... although it's more opaque. While the underlying bits may be an address in virtual memory, they don't have to be. They're just a way of getting to an object (or representing the null value). So while they're not exactly the same, if you're used to thinking of a pointer as "a way of identifying an object or navigating to it" (in some sense) then yes, those thoughts apply to references too.
Java doesn't have pointers as such (unlike, say, C# which has references and pointers - the latter being used in "unsafe" code).
The terms "reference" and "pointer" are basically equivalent. Much of the literature I've seen about the basics of Java claims that Java has no pointers. But if you try to use a null reference you get a NullPointerException. So it's all semantics.
(The real difference is, in C or C++ the term "pointer" strictly means an integer that happens to be the memory address of some data. Whereas in Java the term "reference" more closely matches the C++ "reference" concept. You can't work with the memory address directly even if you want to, but you use it the same way.)
Cat x = new Cat();
This line creates a Cat object in memory and stores a reference to it in x.
x now contains a reference to the Cat object, but if you were to do:
x = x + 1;
This would not give the next memory address like in C, but would give a compiler error. Java doesn't allow control to the reference or memory location.
If we think of references or pointers as a kind of means to access or handle the object in memory, they are much the same. However, they are different in terms of implementation. Following are the differences:
Pointers support pointer arithmetic and this makes them a kind of unsafe. Because they provide the programmer with an access level that is much more than needed. However, references provide an handle to the object through a much more abstract hided implementation.
Pointers are less strongly typed and references are more strongly typed. By this I mean the code is much simple in case of references and slightly less formal in case of pointers. You need to make use of reinterpret_cast kind of things to use pointers casts and on the other hand, references support easy casts of the kind of primitive kind of casts in "C".
Pointers are unsafe and references are safe. If you have an option between the two select references. In languages like C#, you have this freedom.
Pointers exists in C/C++ and references exist in Java. Do not confuse references in C/C++ with references in Java. References in C/C++ is a kind of synonym used for the rvalue of a pointer in C/C++.
No, Java does not have pointers. The fundamental concepts in Java are "values" vs "references".
A reference is a pointer that you can't normally see the value of (i.e., the memory address). The only operations allowed are to set it (from another reference) and to reference through it to the referred-to object. It can be set from a reference-valued expression, such as the new operator, or from another reference (which is syntactically a simple reference-valued expression).
In terms of Java syntax - it's true, that Java doesn't have pointers, but to clarify situation - Java Runtime has pointers.
Every time GC occurs, there is a big chance, that your object stored in the heap will physically change it's memory address. This is achievable only by using what is really called Pointer. As you come from C++ world - you know reference restrictions. But it doesn't mean that JVM doesn't use references =).
For further information take a look at Ordinary Object Pointer
So, as you have brief understanding of core JVM memory management, you can consider Java Reference term as Pointer at runtime.
No, there is no pass by reference at all. Only pass by value, since the real Java method argument is a copy of Java Reference, therefore - another Pointer.
So, in syntax usage way - Java Reference is closer to C++ Reference, but in real Runtime world it's close to Pointer.
P.s. for more clarification it's better to read more articles about Ordinary Object Pointer or take a loot at oop.hpp.
As Java does not support pointers. It may appear that references are special kind of pointers. But we must note the key difference: with a pointer, we can point any address (which is actually a number slot in a memory). So, it is quite possible that with a pointer, we can point an invalid address also and then we may face surprises issues during runtime. But reference types will always
point to valid addresses or they will point to null.
pointer only contain the address but Reference does not contains address if we say frankly then
address of the object is assigned to the index or we say can hash code and case code is given to the reference variable if we will see the content of the reference variable it starts with class Name # and after it some Hexadecimal Code. These nos are not address it is a index value or hash code.
second point
we can not perform any Arithmetic operations on values the Content of reference value

Categories