This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is a Null Pointer Exception?
Java: Why aren't NullPointerExceptions called NullReferenceExceptions ?
Our of idle curiosity, does anyone know why the null reference exception in Java was called NullPointerException?
It seems counterintuitive that in a new language that officially has no pointers a choice would be made to use this name when using a null reference.
If anyone can point me to an authoritative explanation, that would be appreciated.
The statement that Java "officially has no pointers" is simply false. Just because you can't do arithmetic on it doesn't mean it's not a pointer. C is not the final authority for programming terminology.
The only people who get hung up over it are either C/C++ fans who want to disparage Java for its lack of power over the bare metal, or (unlikely, nowadays) marketing people who want to sell Java as a simpler, safer alternative to managers who've had bad experiences with C development.
From the Java language specification:
The reference values (often just
references) are pointers to these
objects, and a special null reference,
which refers to no object.
Related
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.
This question already has answers here:
Does java really have pointers or not? [closed]
(9 answers)
Closed 9 years ago.
As we know that in c/c++ pointer concept are present and pointer concept is a very good one.Then, Why the java developers remove it and if we can't use in java the what is it's pros and cons.
Pointers are forbidden in managed languages by default to allow memory management to be opaque - for example, you can have a compacting garbage collector that moves objects closer together to free up larger blocks of free space and improve cache coherency, if there are no pointers. But if there are pointers, then every pointer would either need to be updated or become valid every garbage collection, which is infeasible.
(This is just one reason of course :) but an example of how allowing pointers drastically changes what your programming language can do )
In some managed languages, like C#, you can declare unsafe {} blocks, where you can used unmanaged memory and pointers, but only there.
Yesterday I was attending a talk by a CTO of a reputed European Company, and he told until recently he did not know that java has pointers. On confronting him he said he is absolutely sure about existence of pointers/unsafe code in java.
There's a class called sun.misc.Unsafe, that much is true. But it doesn't use pointers in Java code (because Java has no pointers, although I agree that java references are similar in concept), most of it is implemented using native code.
As I mentioned in the comments, this is not part of the public API and shouldn't be used by client code. However, you can see it at work when you look at the sources of the Atomic* classes in the java.util.concurrent.atomic package.
There are no pointers in Java, only safe references. Unfortunately your highly reputed CTO was wrong.
Unsafe code is integrated through JNI.
http://en.wikipedia.org/wiki/Java_Native_Interface
Java has pointers.
The confusion whether Java has pointers or not is strongly related to the discussion whether Java is call by reference or call by value.
Uninformed people think that Java has no pointers, and since a method can change an object that's passed in with the effects of this change vissible to the caller, they reason it must be call by reference.
This is not correct however.
What happens is that in Java pointers are passed by value. There are no other kinds of variables in Java for objects than pointer variables and there is no call by reference.
The "unsafe" story is quite something else. This normally is distinct from the question of whether Java has pointers or not. Java's pointers are safe; they point to objects and using the normal language constructs they can not be manipulated to point to arbitrary memory locations.
There is however JNI, but then native code does potentially unsafe things, not Java code.
There is also Real-time Java (jsr-1), where you absolutely can get access to specific memory locations in your system. This however is a very specific and rather rare version of Java that's mostly used for embedded purposes. If this was meant I guess it would have been explicitly mentioned.
Java has no pointers, only references to objects. A reference is similar to a pointer, because it points to a variable, an object, but you cannot view or edit the address memory of this reference, which you can do in C.
Another thing. In C, you can manage pointers with the referencing/dereferencing operation, putting a * first of the name of pointer. In Java, this operation, referencing/dereferencing, is completely absent, because it's totally automatic, hidden to the user.
Other info on Wikipedia and Oracle.
Pointer is the concept of pointing to the reference. In C and C++ you could access the pointer explicitly but in java we use reference to point the objects. For example If you are passing the an instance to a method, Its reference passing we know but ultimately pointer is passed. when you change state of the instance within the method it reflects when that instance is used after the completing the method call.
We use reference in java which is pointer with respect to JVM.
When we use new operator it create the instance in heap and returns the address to the reference its pointing.
I believe this would answer your question if not you could comment on my answer.
You can call native functions in Java (JNI), so in this sense you can use pointers. But other than that - no, there are no pointers in Java.
He may have a slight confusion with call-by-value and call-by-reference. If he comes from C, he may think that call-by-reference is equal to a pointer. Just a guess.
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
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Does Java pass by reference?
Is Java pass by reference?
Hey,
Is it true that Java passes everything by value, and we can't pass something by reference ?
Thanks.
Good God, it can't be asked again. Java is pass by value - always, in all cases. That's it.
Here's a reference that quotes James Gosling, who should be authoritative enough for anyone:
From the authors of Java: "There is
exactly one parameter passing mode in
Java - pass by value - and that helps
keep things simple." The Java
Programming Language, 2nd ed. by Ken
Arnold and James Gosling, section
2.6.1, page 40, 3rd paragraph.
Java passes value of references of the objects you are passing and simple value for primitive types.
See following discussion on this:
Is Java pass by reference?
Java passes primitives by value by default, and all types have their object references passed by value. See http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.1 for details.