Difference between pointer and reference in java [duplicate] - java

This question already has answers here:
What is the difference between a pointer and a reference variable in Java?
(9 answers)
Closed 5 years ago.
Creating a reference in Java is same as the concept of pointer is exactly same, then why it is said that Java does not support pointers?

In C you can manipulate pointers to get somewhere else than the pointer points to. In Java references are atomic and only makes sense to get to an object in memory.

Related

Is Java using pointer to access Array by index? [duplicate]

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.

I need help setting the attributes of objects inside a List [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
NullPointerException Error using linked lists
(2 answers)
Closed 6 years ago.
Let's get to the point. I'm using a LinkedList to store some info about the objects it store. The thing is, I can't set all the attributes of the objects right when I create it, so I need to access them later. In fact, this is the piece of code where I access it.
listaNodos.get(esteNodo(nodoActual)).caminoVuelta.getLast().setFin(currentGrid); listaNodos.get(esteNodo(nodoActual)).caminoVuelta.getLast().setPath(caminoVuelta);
Just as a clarification: caminoVuelta is also a LinkedList.
So, can I modify the atributes of the instances stored in caminoVuelta by simply accessing them with getLast() method from LinkedList and then using the set() methods? Or will this change never happen to the original object?
For some reason, (and it only happens for the first node(Nodo) I create) it returns a NullPointerException.
I hope it's clear enough. Thanks.

Null Pointer Exception in Android , what does it say? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
I have faced many time the NullPointerException in android. Some times I understand why the exception is thrown.
My Question is: Android applications are developed in Java and Java does not support pointers, then why is the exception called NullPointerException?
NullPointerException is a situation in code where you try to access/ modify an object which has not been initialized yet. It essentially means that object reference variable is not pointing anywhere and refers to nothing or ‘null’.
A NullPointerException is thrown when you use a null reference. The name is a holdover from an early prototype of Java.

What is a reference in Java? [duplicate]

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."

dangling pointers java using arrays [duplicate]

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

Categories