This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Creating a dangling pointer using Java
how to create dangling pointers in java but this time using arrays as memory allocators?
There can't be dangling pointers in Java. The language is defined in a way that makes it impossible.
Object will only ever be removed by garbage collection when they are no longer reachable.
The closest you can get to a dangling pointer is a reference that holds null (i.e. doesn't point to any object). But that's still a defined value with defined behaviour.
Since Java uses a garbage collector, it's impossible to create a dangling pointer in Java. I guess if you misinterpret what a "dangling pointer" is, you could see the null pointers a newly created array (of a reference type) is filled with as "dangling pointers".
You cannot create a dangling pointer or reference in Java Creating a dangling pointer using Java
Related
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
This question already has answers here:
How can I use pointers in Java?
(16 answers)
Closed 2 years ago.
I am curious. is java using Pointer like C or C++ to access array by index? when use C languange, x[a] can be converted to *(x + a). What about Java languange? its same with c and c++ or it just use Sequential search to access the element?
If it is an array of objects, it's essentially an array of pointers that reference those objects, this is not the case for primitive values however. Unlike c++, you cannot do pointer arithmetic in Java.
OK, i found an answer from this https://softwareengineering.stackexchange.com/a/105919
In Java, plain pointer arithmetics (referencing and dereferencing) don't exist anymore. However pointers exist. They call them references, but it doesn't change what it is. And array access still is exactly the same thing: Look at the address, add the index and use that memory location. However in Java, it will check whether or not that index is within the bounds of the array you originally allocated. If not, it will throw an exception.
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
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".
This question already has answers here:
How is reference to java object is implemented?
(5 answers)
Closed 9 years ago.
Since java does not support pointer(memory address) , then how is the reference exists in memory and how a reference variable use it ??
Since java does not support pointer
Yes it does. Why do you think there is a NullPointerException? Pointers can either be null or contain a reference to an object.
(memory address),
Exactly. It doesn't support memory addresses, and it doesn't support C/C++ semantics on pointers as memory addresses. All you can do with a Java pointer is assign it or dereference it.
then how is the reference exists in memory and how a reference variable use it ??
See the JLS: "The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object."