0

I'm a c#/java programmer and haven't really played around with pointers much. I'm just wondering if it's something that's useful enough to learn.

Could someone explain why and what exactly the pointers will help c#/java programmers with?

locoboy
  • 38,002
  • 70
  • 184
  • 260
  • 3
    See: http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome/5754#5754 –  Jan 29 '11 at 00:47
  • Pointers won't help Java programmers with anything... – Oliver Charlesworth Jan 29 '11 at 00:48
  • @sova, hardly -- it's analogous to saying pattern matching won't help Java programmers (or C# programmers, etc.) with anything. If the language doesn't support the construct, it's not going to be very useful *within* that language. – Kirk Woll Jan 29 '11 at 00:51
  • There are no pointers in Java, just NullPointerException :-) – 6502 Jan 29 '11 at 00:51

3 Answers3

3

You are already using pointers all the time. Every non-primitive variable in Java (and presumably also C#, though I am less familiar with it) is a pointer to an object.

Perhaps you want to ask about pointer arithmetic?

Community
  • 1
  • 1
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
  • 1
    +1 Yes, you're using pointers all the time in both Java and C#. Variables of the `Object` type are actually pointers to an object instance in memory. These languages just use clever abstractions to hide the gory details of "pointers" from you. Understanding *how* pointers work is likely to be very educational. As far as their practical usefulness in either Java or C# specifically? Limited. There's generally another way. – Cody Gray - on strike Jan 29 '11 at 07:01
  • Can either of you provide a bit more detail about what happens under the hood in a very basic sense? I'd like to know if it'll be worthwhile to take this 'educational' task... – locoboy Feb 01 '11 at 06:29
  • My point was that if you know how to manipulate objects in Java/C#, then at some level you *already know* about pointers, since pointers are how you refer to objects in these languages. It would help if you clarified in your question what exactly you feel you don't understand about pointers. – Ryan C. Thompson Feb 01 '11 at 09:14
2

Because pointers let you actually access the RAM manually (impossible in Java or in "safe" C#), and they let you have control over how things are allocated.

Variables that "hold" objects in C# and Java are really holding pointers (also loosely called references) to the objects in memory, hence why classes are called "reference types". By contrast, structs are "value types", so struct variables actually holds the data themselves. Pointers let you allocate and access memory dynamically, and without any safety checks on the compiler's part -- they give you some potential performance boosts, but they burden you with checking your code and ensuring its correctness.

The reason for this is a bit subtle: You need pointers since, in order to use memory, you need to know the size of the memory you need at compile time. But of course you can't ever be sure that a user won't try to load a 100-MB file into your program that was only designed to handle 20 MB, and, on the other hand, you can't reserve that much since the beginning (think about what would happen if everyone reserved all the resources). So you ask the operating system to allocate a block of memory for you and give you the address of its first byte, and that's why pointers are needed. (There's other uses for pointers, like memory-mapped files and devices, but that'd make the post longer.)

Performance-critical applications, as well as applications that are full of bugs, both often use pointers heavily. :)

user541686
  • 205,094
  • 128
  • 528
  • 886
1

I think that by understanding pointers you'll better understand what really happens within your computer, what's going on in there and how your programs can actually work.

If you don't care about it, and just want your java programs to work you could skip them; otherwise I think they'll help you with anything concerning programming.

peoro
  • 25,562
  • 20
  • 98
  • 150