-3

I know that pointer is a variable that refers to a memory address and Java does not support pointers because unsafe and complexity problem. Then how come one can access memory addressing directly in Java? But still Java uses Memory locations and manipulation part. Can't we access memory addressing directly in Java and why does it have no security while accessing? In how many ways can one access the memory manipulation directly /indirectly in Java?

If I want to write a code based on Memory management concepts then how can I do it in Java?

What is the practical reason for avoiding not to access memory locations directly or indirectly in Java (Other than complexity or insecure have any other reason)? I have looked on google regarding this but I could not be able to find good answer.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Achiever
  • 1,626
  • 10
  • 30
  • 54
  • As java is internet programming language for security reasons. – Saurabh Sharma Feb 11 '14 at 06:59
  • 1. To avoid hacking of servers where Java is implemented. 2. To avoid the menace of addresses and pointers. C-lang is how much difficult with pointers that too in DS. See Java DS how much simple is. - See more at: http://way2java.com/java-introduction/java-drawbacks/#sthash.kTUAPPCv.dpuf – Wanna Coffee Feb 11 '14 at 07:02
  • This is not a good answer? http://stackoverflow.com/questions/6924236/why-cant-we-use-pointers-in-java And you can always *emulate* memory management, in my OS class we had to build a VM in pure Java. Of course it's not the real thing, but it might be enough for what you have in mind. – William Gaul Feb 11 '14 at 07:03
  • here is your answer : http://stackoverflow.com/a/8080701/2764279 – earthmover Feb 11 '14 at 07:06
  • 1
    Because that's the way they designed it. There is plenty of material on this topic available. – user207421 Feb 11 '14 at 07:07
  • @EJP and Wanna coffee- Iam beginner in Java and know that Java is designed for security purpose but I have a doubt, so nobody can try to hack datas from server at any cost where java is implemented? Some hacking tutorials are available but not sure about Java. I am sorry if i have asked anything wrong here but thats my doubt! – Achiever Feb 11 '14 at 07:13
  • Of course you can. Java is just a harder platform to make critical mistakes in at the code level, but you can just as easily create insecure garbage with it. The source of that problem is between the keyboard and the chair. – Gimby Feb 11 '14 at 08:26

2 Answers2

2

A design goal of JVM is to prevent all sorts of memory corruption introduced by pointer manipulation mistakes (programming mistakes).

Manual memory management is supported by Java but in very limited ways:

  • Manipulation of byte memory buffers (safe)
  • Direct memory manipulation (unsafe)

Check out the following Javadocs:

http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html http://www.docjar.com/docs/api/sun/misc/Unsafe.html

0

You already mentioned the security reason yourself. Java program runs in container named Java Virtual Machine (JVM) that controls the memory management. This means that program cannot directly access memory for security and stability reasons: java program cannot damage memory of other process.

Other reason is garbage collections. Like C++ Java has new keyword that allocates memory for new object. However java does not have delete. The removing of object that cannot be used any more is done automatically by GC. I guess it is a good reason to avoid direct memory access.

BTW even if you are using pointers in C program you cannot easily access whole memory available for operating system. The pointers a localized for your memory space allocated for your process by OS.

Next comment is about direct access to memory in java. You have 2 ways to do this: JNI/JNA that integrates java and native code and sun.misc.Unsafe that allows access to memory outside the JVM managed heap.

And the last comment or even question: why do you need to access the memory directly?

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • People with C background tend to look for pointer arithmetics and especially "double pointers", which is usually the reason to look for "direct memory access" in Java - as we all know, Java isn't suitable for the job. –  Feb 11 '14 at 07:13
  • 2
    I am not sure of the garbage collection argument. Manual memory management does not have to require pointers to actual virtual memory locations, and pointers are used in many situations that are unrelated to memory management. Basically, C pointers can do too many things, which is good enough reason to do away with them. – juanchopanza Feb 11 '14 at 07:28
  • -1, you get at least half of it wrong. The reasons that you advance about the "security" are not valid at all. On a modern OS, no process can access the memory of another process and not process can access the "whole memory". Memory on modern systems is virtual addressing. – Jens Gustedt Feb 11 '14 at 07:52
  • @Jens Gustedt, I mentioned this point also. However security is still one of the design constraints of java. – AlexR Feb 11 '14 at 08:09