C++ instead of Java

 
Does anyone know why MQL in general, and MetaQuotes specifically (being the "provider" of the software/language) went with a C++ styled programming language (along with all the related issues) instead of Java (which does have it's own issues, but has reportedly fixed alot of the inherent issues that were designed into C++)?
 
JD4:
Does anyone know why MQL in general, and MetaQuotes specifically (being the "provider" of the software/language) went with a C++ styled programming language (along with all the related issues) instead of Java (which does have it's own issues, but has reportedly fixed alot of the inherent issues that were designed into C++)?

Which issues ?

Java is a OOP language, not really accessible to traders who are trying to automate their strategy. mql4 at least was designed as a very simple and easy to learn language. Just guessing.

 
Alain Verleyen:

Which issues ?

Java is a OOP language, not really accessible to traders who are trying to automate their strategy. mql4 at least was designed as a very simple and easy to learn language. Just guessing.

Well, not saying that the MQL language itself has the same issues, but from the course books I had for Java when I was taking it in college had some notes in it about some of the issues that C++ (which Java is based on) had.  Java supposedly fixed some of the more glaring C++ errors, if I remember correctly, the pointer method used.  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.  There was also the possibility for memory issues as well, which Java took care of automatically.

Since Java is an open source programming language, there would be no issues of usage on that end.  I am just curious why the choice is all.  Maybe the programmers who set it up knew C++ better because that is what they programmed in before.  And for the record, C++ is also an OOP language, and one of the comments they made on bring in MQL was that it was able to make use of the OO design concept.

 
JD4:

Well, not saying that the MQL language itself has the same issues, but from the course books I had for Java when I was taking it in college had some notes in it about some of the issues that C++ (which Java is based on) had.  Java supposedly fixed some of the more glaring C++ errors, if I remember correctly, the pointer method used.  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.  There was also the possibility for memory issues as well, which Java took care of automatically.

Since Java is an open source programming language, there would be no issues of usage on that end.  I am just curious why the choice is all.  Maybe the programmers who set it up knew C++ better because that is what they programmed in before.  And for the record, C++ is also an OOP language, and one of the comments they made on bring in MQL was that it was able to make use of the OO design concept.

Well, actually you can adopt your style close to JAVA when coding. And there are no real pointers in MQL, but rather descriptors like JAVA has. What you may miss is the garbage collector (painful experience), exceptions handling, interfaces and entire JAVA system package. Nevertheless, if you have time, you may have source code resembling more JAVA than C++ finally.

 
JD4:

Well, not saying that the MQL language itself has the same issues, but from the course books I had for Java when I was taking it in college had some notes in it about some of the issues that C++ (which Java is based on) had.  Java supposedly fixed some of the more glaring C++ errors, if I remember correctly, the pointer method used.  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.  There was also the possibility for memory issues as well, which Java took care of automatically.

Since Java is an open source programming language, there would be no issues of usage on that end.  I am just curious why the choice is all.  Maybe the programmers who set it up knew C++ better because that is what they programmed in before.  And for the record, C++ is also an OOP language, and one of the comments they made on bring in MQL was that it was able to make use of the OO design concept.

Java is always pass-by-value. All instances of abstract data types in Java are not really objects, but rather, pointers to objects. So for example, when you pass a pointer of an object to a function:

JObject obj = new JObject();
foo(obj);
//obj still points to original object



public void foo(JObject o)
{
   o = new JObject(); //this is an entirely different object assigned to the same pointer
}

You are not really passing a reference to an object to function foo(), but the value of a pointer.

Java being newer than C++ does not make the former superior. Think of it as trade-offs. The Java example above sacrificed convenience for flexibility. In Java, you can get away with checking pointers that you need to do with C++, but suppose you reassign obj within foo() to a new object. Now Java would simply create a new object for the same pointer inside foo(), but there is no way it can change where the original pointer is pointing to through the said function. 

 
Enrico Lambino:

Java is always pass-by-value. All instances of abstract data types in Java are not really objects, but rather, pointers to objects. So for example, when you pass a pointer of an object to a function:

You are not really passing a reference to an object to function foo(), but the value of a pointer.

Java being newer than C++ does not make the former superior. Think of it as trade-offs. The Java example above sacrificed convenience for flexibility. In Java, you can get away with checking pointers that you need to do with C++, but suppose you reassign obj within foo() to a new object. Now Java would simply create a new object for the same pointer inside foo(), but there is no way it can change where the original pointer is pointing to through the said function. 

Though I haven't been coding in Java for many years, I do not think it has changed as much. Could you please explain, what you mean by "abstract data types" and "pointer" in Java? As far as I know, only primitive types are passed by value, while arrays and objects are passed by reference.

In your example, there is no difference in functionality between C++ and Java, just a very tiny difference in syntax:

Java:
public foo(JObject o) {
   o = new JObject(); //this is an entirely different object assigned to the same pointer local variable
}

C++
public: void foo(JObject *o) 
{
   o = new JObject(); //this is an entirely different object assigned to the same pointer local variable
}

Besides the code does nothing useful (it assigns new object/pointer to a variable originally holding a parameter), you will run in memory leak in C++, while Java would destruct the lost object automatically.

 
Ovo Cz:

Though I haven't been coding in Java for many years, I do not think it has changed as much. Could you please explain, what you mean by "abstract data types" and "pointer" in Java? As far as I know, only primitive types are passed by value, while arrays and objects are passed by reference.

In your example, there is no difference in functionality between C++ and Java, just a very tiny difference in syntax:

Besides the code does nothing useful (it assigns new object/pointer to a variable originally holding a parameter), you will run in memory leak in C++, while Java would destruct the lost object automatically.

Pointers exist in Java. Objects in Java are actually created through pointers (references), and those references are passed to functions by value. Though I am not sure about arrays since I no longer use them in Java.

There is a huge functional difference, see full example below:

Java:

public class Test {
    static class JObject {
        public String name;
    }
    public static void main(String[] args) {
        JObject obj = new JObject();
        obj.name = "object1";
        foo(obj);
        System.out.println(obj.name);    //prints "object1"                          
    }
    public static void foo(JObject o) {
        o = new JObject();
        o.name = "object2";
    }
}

C++:

#include <iostream>
using namespace std;
class JObject
{
public:
    string name;
};
void foo(JObject *o);

int main()
{
    JObject *obj = new JObject();
    obj->name = "object1";
    foo(obj);
    cout << obj->name << endl; //prints "object2"
    delete obj;
    return 0;
}

void foo(JObject *o)
{
    delete o;
    o = new JObject();
    o->name = "object2" ;
}

If Java passes objects by reference, it would print the same result as the C++ version. 

Deleting object o is necessary though in C++ (memory leak), but not in Java (destroyed automatically), but C++ also gives the possibility to reuse the same pointer as originally passed to foo(). Java's automatic memory management lazily creates a new pointer for the new object (different from the one originally created in main()), whether you like it or not. It's convenient, but can be impractical in some cases.

 
Ovo Cz:

Though I haven't been coding in Java for many years, I do not think it has changed as much. Could you please explain, what you mean by "abstract data types" and "pointer" in Java? As far as I know, only primitive types are passed by value, while arrays and objects are passed by reference.

In your example, there is no difference in functionality between C++ and Java, just a very tiny difference in syntax:

Besides the code does nothing useful (it assigns new object/pointer to a variable originally holding a parameter), you will run in memory leak in C++, while Java would destruct the lost object automatically.

From what I remember, you are right on this one.  The reference that Enrico was talking about is where the terminology gets confused.  How he is saying what is happening is this, the pointer IS the reference.  I am not sure I am explaining the rest of it clearly, as I do not claim to be an expert Java programmer by any stretch of the imagination.  To use an OOP style example, within the scope of the object itself, only the functions within that object can change the object's states.  Any external functions or other objects, when they are referring to the object in question, are where the passed by reference part comes in.  They cannot change the state of the object except through the public methods that the object itself is in control of.  And in reality, they are technically not changing the object.  That is the entire reason for stronger controls in Java than those in C++.
 
Enrico Lambino:

Pointers exist in Java. Objects in Java are actually created through pointers (references), and those references are passed to functions by value. Though I am not sure about arrays since I no longer use them in Java.

There is a huge functional difference, see full example below:

Java:

C++:

If Java passes objects by reference, it would print the same result as the C++ version. 

Deleting object o is necessary though in C++ (memory leak), but not in Java (destroyed automatically), but C++ also gives the possibility to reuse the same pointer as originally passed to foo(). Java's automatic memory management lazily creates a new pointer for the new object (different from the one originally created in main()), whether you like it or not. It's convenient, but can be impractical in some cases.

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

Does Java have pointers?
Does Java have pointers?
  • About
  • www.programmerinterview.com
No, Java does not have pointers. This was an intentional decision by the creators of Java, because most people would agree that having pointers creates a lot of potential for bugs in the code – pointers can be quite confusing, especially to new programmers. Because arrays and strings are provided as class types in Java, there is no need for...
 
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. I don't think it's possible to actually say which part of MQL is larger - C++-ish, Java-ish, or C#-ish.
 

I am currently doing a search for it, but I think I remember reading an official MQL5 article on how they based MQL on either C# or C++, which was what set me on this line of query.


Edit.  Found it, here, at https://www.mql5.com/en/docs/basis.


Alain Verleyen:

Which issues ?

Java is a OOP language, not really accessible to traders who are trying to automate their strategy. mql4 at least was designed as a very simple and easy to learn language. Just guessing.

Judging from the above link, they also consider MQL5, as the C++ it is based on, to also be an OOP language.  "The MetaQuotes Language 5 (MQL5) is an object-oriented high-level programming language..."  I am not sure what grouping, if any, they consider MQL4 to fall under.


Edit: fixing it so link works like it is supposed to.

Documentation on MQL5: Language Basics
Documentation on MQL5: Language Basics
  • www.mql5.com
Language Basics - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: