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

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.

Related

Difference between pointer and reference in java [duplicate]

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.

Why Java supports arrays of arrays, rather than multidimensional arrays? [duplicate]

This question already has answers here:
Why doesn't Java have true multidimensional arrays?
(6 answers)
Closed 8 years ago.
What are the differences between multidimensional arrays and array-of-arrays?
Why Java supports arrays of arrays, rather than multidimensional arrays ?
Although your why question is probably unanswerable at this point (it would take one of the original creators of Java to answer it), you may note that a strong design principle of the original Java was simplicity. In that spirit all that Java supports is an array, which on its own gives you an array of arrays as just a special case: such an array whose component type is array.
About the only advantage of a true multidimensional array is the way its members are packed together, offering better cache locality. Such concerns were not high on the list of design priorities of original Java, although today they are getting a much greater share of the spotlight.
Refer to this topic for an in-depth review of pros and cons of multidimensional arrays.
Short answer: because the language was designed this way. But an array of arrays functions as a multidimensional arrays, so this is not really a limit on the language.
Probably the reason for this is that Java borrowed its array syntax from C and C++, and C and C++ multidimensional arrays are also accessed as though they are arrays of arrays. The difference is that in Java an array of arrays is an array of references to arrays (and thus the arrays in the array can have different lengths).

Does Java have pointers? if yes, how manipulate them? [duplicate]

This question already has answers here:
Does Java have pointers?
(12 answers)
Closed 9 years ago.
I am new to java and confused.
Does Java have pointers? if yes, how to manipulate them? how to perform operations like ptr++ etc?
Yes, java has pointers and they call them references.
But reference manipulation is not possible in java. That is, you can not do ref++ and things like that.
You can just allocate memory to an object and assign it to a reference, de-allocation too is done by garbage collector in JVM. So you are free of free.
Java doesn't have pointers, but you can make pointer manipulations with sun.misc.Unsafe: Java Magic. Part 4: sun.misc.Unsafe:
static Object shallowCopy(Object obj) {
long size = sizeOf(obj);
long start = toAddress(obj);
long address = getUnsafe().allocateMemory(size);
getUnsafe().copyMemory(start, address, size);
return fromAddress(address);
}
Though in my practice I have never wanted to do such things and they are considered a bad practice by community unless you're developing a super-fast library like Kryo.
You dont have pointers, or at least not how you are used to thed from C/C++/whatever. You have object references instead, but you cant ++ those.
The following examples are pointers set to reserved memory:
Object o = new Object();
int[] myInts = new int[32];
You can manipulate pointers like this:
Object myObject = otherObject;
...if both types match.
You cannot do pointer manipulation as you could in C, because these are usually dangerous operations. Java in general tries to reduce coding errors by disallowing dangerous operations as much as possible. In the beginning this feels restraining, but once you get to know Java and suddenly can write a whole page of code without a single bug, you understand why this is a core design of the language.

What is the motivation of retrieving the length of an array using a public instance variable, instead of calling a method? [duplicate]

This question already has answers here:
Where is array's length property defined?
(7 answers)
Closed 9 years ago.
Since most other classes seem to let the developer retrieve the length or size of its content by calling a method, usually length() or size(), how come the length of an array is retrieved by reading the instance variable length? It just seems inconsistent to me, especially since a String object is immutable as well and still uses a length() method.
It is inconsistent. I suppose the actual reason for the distinction can only be found in the early history of Java language development. Perhaps it was for what seemed at the time to be performance reasons. I suspect that if Java were being (re)designed from scratch today, the length field would disappear and instead there would be a java.lang.Array class* (similar to java.lang.Enum) from which all arrays would derive and which would include a length() method inherited by all arrays.
Actually, the lack of an Array class (in the above sense) may indicate why there's a length attribute: arrays are more "built in" to the language than, say, the collection classes (which are part of a class library).
* Java does have java.lang.reflect.Array, but that does something completely different from what I'm talking about.
Most of the collections have dynamic sizes, so they need a method to verify the length/size in that specific time. An array has a fixed length, so it doesn't need to be recalculated all the time.
An array is of fixed size - it will never change. As such, you don't need the overhead of a method.
The reason why is that the JLS says so:
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array. length may be positive or zero.
Regarding the underlying motivation: only the people who created the language could answer...
Interestingly, Oak, which is the language at the origin of Java, already had that notion. If you read its specifications, you will see that:
The length of any array can be found by using .length
So the best guess here is that it is the result of a decision made by a few guys more than 20 years ago.
A method is executed every time it's called.
Optimization exists, such as cache etc.. but the point is that an array never gets its size changed.
Thus, what could better fit the case than a "final" (not sure about the implementation under the hood but same concept) instance's field?
java.util.Collection describes some classes wrapping arrays like ArrayList, HashSet etc..
In their case, size is altered since the concept of collection is to have an extensible array.
Thus, the size must be calculated thanks to a method (size()), not a field in this case.

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