i ask my teacher why java doesn't support pointers my teacher reply me that it is because pointer's are very hard to understand so java doesn't support the pointers.

but i don't think that it is true.

please help me by giving the right reason.

Since java has this error: NullPointerException, I assume that is has pointers.

The question is what is the behavior of java pointers and how can you use them.
They have very little in common with c pointers.

Edited 5 Years Ago by NormR1: n/a

Pointers are only useful if you are working with memory locations. Java memory management is completely automatic, there's no context in which you need to manipulate the address of anything.
However
All Java variables of type Object or array are what Java calls "reference" variables. They contain a reference to an object or array, or are null. You can't do arithmetic on them, but references are otherwize much like pointers. If you try to use a reference variable that is null you get an exception called a NullPointerException - a very interesting choice of name.

So, short answer: Java has references which are just like pointers except that they are maintained entirely by the JRE, not the programmer.

Edited 5 Years Ago by JamesCherrill: n/a

Since java has this error: NullPointerException, I assume that is has pointers.

The question is what is the behavior of java pointers and how can you use them.
They have very little in common with c pointers.

So Java supports pointer or not???

coz if it is not then how the Object of any class works in java..

It depends on exactly how you define "pointer". If you define it as a reference to where an object is on the heap then Java has them. If you define it as a thing that supports pointer arithmetic, like a C pointer, then no it hasn't.

Java has pointers BEHIND the scenes. They are not accessible to the programmer using the language. Remember that Java is built upon C/C++. Java is a managed language, hence no pointers. When you instantiate an object using the "new" keyword the C function "malloc" is used to allocate memory on the heap for that object. All memory for user defined types is allocated on the heap (the same place a pointer is placed in an un-managed environment). The only thing in Java that is located on the stack are primitive types.

Java has pointers, if the definition of a pointer is that it holds the address of a block of memory and you can use it to get to that memory.
The definition of a pointer does not include the ability to change its value arbitrarily.
The new statement and the assignment statement are the two ways to give values to pointers.

The only thing in Java that is located on the stack are primitive types.

Not so, especially considering the context of this thread. Reference variables (AKA pointers) can be allocated on the stack.

Java has pointers BEHIND the scenes. They are not accessible to the programmer using the language.

Then how would you describe reference variables?

Then how would you describe reference variables?

I don't know what you mean by this. Java does not have "reference variables" like C++. Reference in Java refers more to Polymorphism, where a declared Base class can be instantiated as any of it's super classes. When you pass an object to a method in Java they are always passed by reference. The only way to get around this is to instantiate a new object, calling it's copy constructor and passing the new object to the method.

My senior said me that java doesn't support pointers due to Security reasons. Because we make client-server program on java so the pointer may be used to break the security so we don't use pointers in java....

Is he right???

It depends on the definition.
Java has pointers, if the definition of a pointer is that it holds the address of a block of memory and you can use it to get to that memory.
The definition of a pointer does not include the ability to change its value arbitrarily.
The new statement and the assignment statement are the two ways to give values to pointers.

Edited 5 Years Ago by NormR1: n/a

My senior said me that java doesn't support pointers due to Security reasons. Because we make client-server program on java so the pointer may be used to break the security so we don't use pointers in java....

Is he right???

Seems like a borderline troll question....

This article has been dead for over six months. Start a new discussion instead.