开发者

Why can't we use pointers in Java? [duplicate]

This question already has answers here: Does Java have pointers? (12 answers) Closed 9 years ago. 开发者_C百科

Why we are not using the pointer here?

Which concept is used instead of pointer in Java?


Why can't we use pointers in Java?

Because the language designers chose not to include pointers in the language.

Why we are not using the pointer here.

Because the designers of Java thought it was a tricky-to-use and error prone construct.

Which concept is used instead of pointer in Java?

References (which are quite similar to pointers if you disregard from pointer arithmetic).

Keep in mind that all objects you create, you create on the heap (using the new keyword). This fact, together with the fact that there is no "dereference operator" (* in C/C++) means that there's no way to get hold of an object! Since you can't get hold of an object, there's no way you can store an object in a variable. Therefor all variables (except the ones holding primitive types) are of reference-type.


It was a language design decision.

From the sun white paper The Java Language Environment:

Most studies agree that pointers are one of the primary features that enable programmers to inject bugs into their code. Given that structures are gone, and arrays and strings are objects, the need for pointers to these constructs goes away. Thus, Java has no pointer data types. Any task that would require arrays, structures, and pointers in C can be more easily and reliably performed by declaring objects and arrays of objects. Instead of complex pointer manipulation on array pointers, you access arrays by their arithmetic indices. The Java run-time system checks all array indexing to ensure indices are within the bounds of the array.

You no longer have dangling pointers and trashing of memory because of incorrect pointers, because there are no pointers in Java.


It's good isn't it? You don't have pointers in the C programming sense, the Virtual Machine looks after all that. However, that's not to say you don'f have have similar capabilities. For example, variables for objects are references, when you pass a variable for an object you are passing a reference which is sort of a pointer. You might also use a variable to store the index of an item in an array, that again is a kind of pointer in a programmatical sense.


Everything in Java is accessed only through pointers. Java references are pointers. The creator of java Dr.Gosling says this in his book, "The Java Language Specification" (somewhere around p.40 in one edition).

In fact another book "The Java Virtual Machine" says: "A reference is a pointer to a pair of pointers; one of theses two pointers points to another pointer [...]".

There are marked differences between Java references and C pointers but nevertheless references are pointers.


In Java any reference to a simple type is always by value and any reference to an object is always a pointer except in cases of special objects.


every object you create using new(); is actually pointer..

XXX a=new XXX();

a is actually a pointer.. It's just you cannot do fancy thing like pointer arithmetic to them like you can in C (for safety reasons).

so actually if you are passing an object, you are passing the pointer to the object (object created by new() resides in heap).


You can access pointer mechanics using java.misc.unsafe, but that's... mmm... unsafe.

Ah, about replacing concept. What aspect are you interested in? Variables are already represented by references to them - that's one point of view. If you need pointers to implement efficient data structures, you, most probably already have one, or there is a library providing that feature, or you can write some native code and call it from java wrapper. Anyway, please define aspects you are interested in and maybe community will give you more detailed answer.


Java follows the principle of call by value. So if you create an object, you will get back a copy of a reference to the created object in the heap space. This copy of a reference is what a variable gets assigned to. If you then use this variable as an argument for a method call, again a copy of that reference is created and used instead. Java never uses references or pointers! Everything in java is a copy. Hence Java follows the call by value principle.


The most difficult thing to manipulate in any programming language is with pointers. If u just miss something you get lots of errors namely segmentation faults. Java is one such language providing users the flexibility to do things using CALL BY VALUE .


If you want to change the value of a variable in a C function you pass the address (pointer) of the variable to the function. If you want to use the value of a variable in a function and do not wish to change its value, you pass the name (reference) of the variable to the function. Many programmers never used pointers for any thing else, like linked lists and b trees. C pointers and structures can make some applications infinitely easier. Structures unlike arrays can contain different types of variables and grouping them together makes life easier. Structures in a linked lists contain pointers (addresses to the next link in the list and also to the previous link if the list is double linked. This can make sorting a breeze.

A link in the list can also contain pointers to different structures which allow you to associate the linked lists of different materials or conditions with each link in the first list.

Since the memory is allocated where and as needed rather than being dimensioned as in an array, it gives you tremendous flexibility.The main application I have used this for is calculating the cost of projects to bid jobs.

This flexibility demands close attention to details and no holds barred debugging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜