C++ instead of Java - page 2

 
JD4:

Actually Enrico, they do not.  Not sure how to explain it properly, but here are a couple of links that can do a lot better job of it than I could.

http://www.programmerinterview.com/index.php/java-questions/does-java-have-pointers/

http://stackoverflow.com/questions/1750106/how-can-i-use-pointers-in-java

Edit: fixing it so links work like they are supposed to

In fact, they do. I do not code Java as often as other languages, but I know for a fact that there is an exception called java.lang.NullPointerException. Luckily, I found the exact JLS here, which is from Oracle itself:

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

 
Enrico Lambino:

In fact, they do. I do not code Java as often as other languages, but I know for a fact that there is an exception called java.lang.NullPointerException. Luckily, I found the exact JLS here, which is from Oracle itself:

I feel that was a poor choice of wording on their part (the JLS one).  It specifically says before that that it is a reference.  The pointers I am talking about refer to how C++ functions with them which is different than how Java works.  The NullPointerException you referred to is basically a way to catch an invalid object that was created but not yet given a value.

In code example;

int anyNumber;  //object created, has no value, trying to use this right here would throw NullPointerException

anyNumber=3; // (following the above line somewhere else in the code) object now has a value, would not throw NullPointerException if used in this state

int anyNumber=3; //object created and given a value immediately, also no NullPointerException

Now this might be a bad example from me because in Java, I believe the primitive types (such as int, etc.) are given a default value automatically by the Java language, but it explains the concept well enough I think.

"public class NullPointerException
extends RuntimeException
Thrown when an application attempts to use null in a case where an object is required. These include:
  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

  Null object in this case means an object that was created but not assigned a value, i.e. it has a null value."


Functionally, in C++, pointers refer to a specific memory location.  From http://www.cplusplus.com/doc/tutorial/pointers/

The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as address-of operator. For example:

foo=&myvar;

This would assign the address of variable myvar to foo; by preceding the name of the variable myvar with the address-of operator (&), we are no longer assigning the content of the variable itself to foo, but its address."

You can write something on a piece of paper.  A C++ pointer would refer to the spot of the piece of paper that someone wrote something on.  Java reference would refer to what was written on that piece of paper, no matter where on the paper someone wrote it, even if they moved it to another spot.  In this respect, C++ pointers do not exist in Java.

http://programmers.stackexchange.com/questions/141834/how-is-a-java-reference-different-from-a-c-pointer


 

JD4:

 In this respect, C++ pointers do not exist in Java.

Yes, this is true no doubt. One cannot manipulate a reference (reference value or pointer) in Java in the same way as in C and C++ pointers. And for the record, Java references do not work in exactly the same was as references in C++.

Enrico Lambino:

 Objects in Java are actually created through pointers (references), and those references are passed to functions by value.

Stanislav Korotky:
Although MQL may seem similar to C in syntax, it's also similar to Java in the aspect how it works with types (and further with classes in MQL5). For example, there are no conventional pointers (as something pointing to an accessible address in memory), but handles ("managed" objects, so to speak) instead.
JD4:
The phrase for how it is used in Java is "passed by reference" where in C++ (and C# I believe) was passing the actual pointer, where there was the possibility that the pointer would get trashed, and you would lose the ability to get to what the pointer was pointing at.

In Java, objects are not directly passed, only their references. Those objects are not passed by reference, but by value (the reference or handle pointing to the object), which is essentially how C++ pointers work. References (Java) and pointers (C++) point to a specific locations in memory. However, unlike C++ pointers, you cannot change where those Java references are pointing to, which can also lead to other issues such as references losing their context (see my code example above). Perhaps this is also the reason why they avoided using the term (pointers), preferring to use 'reference' or 'reference value'.

When a Java program encounters a null pointer exception with an object, it is accessing an object that does not exist. Or more explicitly, it is attempting to access an object through a reference which points to an object that is null or does not exist, for example:

JPanel panel;
foo(panel);

Let's assume that foo accesses a method or public member of panel. For this code, in Java, panel is null. In C++ (assuming there is already a JPanel class for it), panel is already an object (static). When foo works on this panel object in Java, it will get an exception.

 
Enrico Lambino:

Yes, this is true no doubt. One cannot manipulate a reference (reference value or pointer) in Java in the same way as in C and C++ pointers. And for the record, Java references do not work in exactly the same was as references in C++.

In Java, objects are not directly passed, only their references. Those objects are not passed by reference, but by value (the reference or handle pointing to the object), which is essentially how C++ pointers work. References (Java) and pointers (C++) point to a specific locations in memory. However, unlike C++ pointers, you cannot change where those Java references are pointing to, which can also lead to other issues such as references losing their context (see my code example above). Perhaps this is also the reason why they avoided using the term (pointers), preferring to use 'reference' or 'reference value'.

When a Java program encounters a null pointer exception with an object, it is accessing an object that does not exist. Or more explicitly, it is attempting to access an object through a reference which points to an object that is null or does not exist, for example:

Let's assume that foo accesses a method or public member of panel. For this code, in Java, panel is null. In C++ (assuming there is already a JPanel class for it), panel is already an object (static). When foo works on this panel object in Java, it will get an exception.

Well, yes and no.  I would need to check back to some of my college books on Java to be sure (but have no clue where they might be as this was well over 10 years ago), but I believe that you can "pass by reference" what I am referring to as the tag of the object, the reference, and from that new copy of the reference, you can then send that object (if it has any public methods) requests for information, or processing, or whatever, that could potentially change the state of that object, where C++ pointers while at the time they are put in place refer to the specific location an item was put, that item could potentially be moved, but that pointer would still point to that location unless something within the program was changed to reflect the move, whereas in Java, the copied from object reference and the copied to object reference both still apply to the object, no matter where in the memory it is, and both refer to it in it's modified state, if it was modified.

Oh, on your JLS link before, it shows on there was I was talking about before when I used int as an example (a poor choice, sorry). "New objects of the types Boolean, Byte, Short, Character, Integer, Long, Float, and Double may be implicitly created by boxing conversion"  So in the specific example I posted, the cases I specified might not actually happen as I posted them.

Reason: